LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-23-2005, 07:09 AM   #1
Tap-Out
Member
 
Registered: Oct 2002
Location: Halifax, NS
Distribution: Ubuntu, Mepis, Debian
Posts: 130

Rep: Reputation: 15
A simple Linux script question I'm sure


Hello all,

I'm not sure if I'm in the right forum, but I'll try here first anyway.

I'm looking to create a simple script that will automatically rename a number of files. Basically I want file xxxxxxxx01.jpg renamed to yyyyyyyy01.jpg. I've looked at the man pages for mv and cp and coulden't find anything in there to help. I'm just trying to get my digital camera photos to have file names that make some sence.

Any suggestions or solutions?

Cheers

Jeff
 
Old 07-23-2005, 07:31 AM   #2
mad4linux
Member
 
Registered: Aug 2003
Distribution: Debian Testing / kubuntu
Posts: 88

Rep: Reputation: 21
hi tap-out
you will have to use bash scripting to get to your goal.
it`s quite simple and learned in a few days. I have wrote a script some time ago, which is copying on file to multiple files, each having a running number (see code below) it is not renaming the first letters but the last, but it would be a similar script for your purpose.

Code:
#! /bin/bash
declare -i i
declare TARGET2
if [ "$1" == "-m" ] ; then
    echo -n "source file path: "
    read SOURCE
    echo -n "target path with file name constants : "
    read TARGET1
    echo -n "start count number: "
    read TARGETMIN
    echo -n "end count number: "
    read TARGETMAX
    echo -n "target file ending (eg. .jpg): "
    read TARGET3;
elif [ "$1" == "-c" ] ; then
    SOURCE=$2
    TARGET1=$3
    TARGETMIN=$5
    TARGETMAX=$6
    TARGET3=$4;
elif [ -r "$1" ] ; then
 echo "ja jetzt bist du in der if-Bedingung";
else 
    echo "-mcp: copies source file to multiple target files"
    echo " mcp: usage: mcp [OPTION] SOURCE TARGET ENDING MINCOUNT MAXCOUNT"
    echo " mcp: usage: SOURCE is the source file path"
    echo " mcp: usage: TARGET target file path including file name prefix"
    echo " mcp: usage: ENDING is the file name ending added to the copies"
    echo " mcp: usage: MINCOUNT start count being added to file name"
    echo " mcp: usage: MAXCOUNT is the end count number (zeros added accordingly)"
    echo " mcp: option: -m menu guided input"
    echo " mcp: option: -c (optional) command line input"
    echo " mcp: option: -h this help text"
    exit 1;
fi
while [ "1" -gt "0" ]; do
    if [ "$TARGETMAX" -gt "9999" ] ; then
	echo "ERROR: end count number should be less then 10000"
	echo -n "end count number ( < 10'000): "
	read TARGETMAX;
    else break;
    fi
    if [ "$TARGETMIN" -gt "$TARGETMAX" ] ; then
    echo "ERROR: start count number should be less than end count number";
    echo -n "start count number:"
    read TARGETMIN;
    echo -n "end count number ( < 10'000): "
    read TARGETMAX;
    else break;
    fi
done
i=$TARGETMIN
while [ "$i" -le "$TARGETMAX" ] ; do 
    if [ "$i" -lt "10" ] ; then
     TARGET2="000"$i ;
    elif [ "$i" -lt "100" ] ; then
	 TARGET2="00"$i ;
	elif [ "$i" -lt "1000" ] ; then
	 TARGET2="0"$i ;
	elif [ "$i" -lt "10000" ]; then
	 TARGET2=$i ;
    fi
TARGET="$TARGET1$TARGET2$TARGET3"
cp $SOURCE $TARGET
if [ "$?" == "0" ] ; then
echo "$TARGET written";
fi
i=i+1;
done
to use this script, just copy it to a text editor, save it as mcp. Then open a terminal. go to the directory where mcp is. Write
Code:
chmod u+x mcp
to make it executable for the owner (g+x and o+x for group and others). Then
Code:
mcp
to get an explanation on how it works. Feel free to change them script for your needs.
 
Old 07-23-2005, 11:10 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
There's also a utility availabkle called "mmv" (for: "Multiple MoVe"). Depending on your distribution, it may not be installed by default, but most distributions do provide it.
 
Old 07-23-2005, 12:39 PM   #4
hlyrad
Member
 
Registered: Jul 2005
Location: Ab Ca
Distribution: Redhat EL Sun Mac OSX FC 3.0 & 4.0
Posts: 44

Rep: Reputation: 15
This looks like a Perl job for sure.

Heres what I would do. Make sure all the files you want to rename are in a separate directory from ones you don't want renamed.


#!/usr/bin/perl

use Cwd;
my ($exp, $file, $ext, $ted, @ted);


$file = shift; #Takes as first argument new name for files
$ext = shift; #Second argument is extension. All files with this extension will be renamed
exit(0) if !$file or !$ext;#Safety first
my $div = "/";
my $cdir = getcwd;
opendir(TD, $cdir) or die ($!);
@ted = grep { ! /^\./ && /$ext$/} readdir TD;
closedir(TD);
$inc = 0;
foreach $ted(@ted)
{
my @sed = split(/\./, $ted);
$sed[0] = $file.$inc;
my $sed = join("\.", @sed);
my $old = $cdir.$div.$ted;
my $new = $cdir.$div.$sed;
print "OLD: $old, NEW: $new\n";
#$cmd = `mv $old $new`; #uncomment this line when you are sure that this is what you want
$inc++;
}
########################
Copy the text above into a text editor and save it as rename.pl
next do a chmod 755 rename.pl
Now say you have a directory called jpegs that holds all the files you want to rename
cd to the jpegs directory
run the rename.pl
/path/to/rename.pl NEWNAME EXTENSION (where NEWNAME is the new descriptive name and EXTENSION is the file name extension)
example;
cd /home/bubba/jpegs
/home/bubba/rename.pl picnic jpg
renames all *.jpg files in /home/bubba/jpegs to picnic[n].jpg
This will change all the files in jpegs directory to NEWNAME0.EXTENSION NEWNAME1.EXTENSION and so forth
Hope this is what you wanted.

Last edited by hlyrad; 07-23-2005 at 04:30 PM.
 
Old 07-23-2005, 02:11 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Or in a bash version

Code:
find /path/to/jpg/dir -name '*.jpg' | while read file; do
    dir=$(dirname "$file")
    suffix=${file: -6}
    new="$dir/yyyyyyyyy$suffix"
    [ ! -f "$new" ] && mv "$file" "$new"
done
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple script question Elvisclone Programming 2 12-01-2004 09:13 AM
simple bash script question bullium Programming 3 11-01-2004 02:42 PM
simple shell script question. strifel Programming 7 09-13-2003 01:44 PM
Simple VI/Script Question shaggz Linux - General 2 07-05-2003 06:55 PM
simple date script question Cynthia Blue Programming 3 01-13-2003 03:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration