View Full Version : Simple shell script?
ashleysnix
21st December 2004, 06:05 AM
Hi
I need to create a simple shell script that will rename batches of files.
All of the files to be renamed contain single dashes (-) which need to be converted to underscores (_).
Can someone please give me a pointer on which linux command I could use to achieve this?
Thanks
Ashley
rogue
21st December 2004, 06:24 AM
You'll probably want to use sed (http://www.google.com/search?q=sed+rename) in a loop.
h4d
21st December 2004, 06:44 AM
Wanted to take a break from my real work, so here it goes. Here is your script:
$ cat test.sh
for filename in * # Traverse all files in directory.
do
fname=`basename $filename`
n=`echo $fname | tr '~' '-'`
echo $n
if [ "$fname" != "$n" ]
then
mv $fname $n
fi
done
exit 0
$ touch file~1
$ touch file~2
$ touch file~3
$ touch file~4
$ touch file~5
$ touch file~6
$ touch file~7
$ touch file~8
$ touch file~9
$ ./test.sh
file-1
file-2
file-3
file-4
file-5
file-6
file-7
file-8
file-9
test.sh
$ ls -l
total 44
-rw-rw-r-- 1 harold harold 0 Dec 21 01:33 file-1
-rw-rw-r-- 1 harold harold 0 Dec 21 01:33 file-2
-rw-rw-r-- 1 harold harold 0 Dec 21 01:33 file-3
-rw-rw-r-- 1 harold harold 0 Dec 21 01:33 file-4
-rw-rw-r-- 1 harold harold 0 Dec 21 01:33 file-5
-rw-rw-r-- 1 harold harold 0 Dec 21 01:33 file-6
-rw-rw-r-- 1 harold harold 0 Dec 21 01:33 file-7
-rw-rw-r-- 1 harold harold 0 Dec 21 01:33 file-8
-rw-rw-r-- 1 harold harold 0 Dec 21 01:33 file-9
-rwxrwxr-x 1 harold harold 283 Dec 21 01:32 test.sh
The most important part is the "tr". Do a man tr to learn more about it!
h4d
21st December 2004, 06:47 AM
You'll probably want to use sed (http://www.google.com/search?q=sed+rename) in a loop.
I agree, sed is another good option!
brahms
21st December 2004, 08:38 AM
Or, of course, you could just do
rename "-" "_" *
h4d
21st December 2004, 09:12 AM
LOL!!!!!!!!
That is so cool, I didn't know that command. Makes my script look a bit...errr...dumb, unless I get payed by line of code!
ashleysnix
21st December 2004, 10:18 AM
hi all
thanks for the help... loved the script h4d!
i tried the " rename "-" "_" * " first, and it worked a treat... 1213 jpegs renamed instantly - thanks brahms.
cheers
ashley
piedamaro
21st December 2004, 02:55 PM
Yeah, it happens...
'sed' is great at processing the contents of text files or a piped streams (after all it's the Stream EDitor), but for renaming, all you want is 'rename' :)
rogue
21st December 2004, 05:22 PM
Alright, now I feel better... according to the copyrights found in the man page and source code, rename didn't exist prior to 2000-01-01. :D
/*
* rename.c - aeb 2000-01-01
*
--------------------------------------------------------------
#!/bin/sh
if [ $# -le 2 ]; then echo call: rename from to files; exit; fi
FROM="$1"
TO="$2"
shift
shift
for i in $@; do N=`echo "$i" | sed "s/$FROM/$TO/g"`; mv "$i" "$N"; done
--------------------------------------------------------------
* This shell script will do renames of files, but may fail
* in cases involving special characters. Here a C version.
*/
vBulletin® v3.8.7, Copyright ©2000-2013, vBulletin Solutions, Inc.