LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 09-17-2005, 07:36 PM   #1
sirpelidor
Member
 
Registered: Oct 2003
Location: Madison
Distribution: mdk 8.2, 9.0, 9.2, slack 9.1
Posts: 403

Rep: Reputation: 30
BASH USAGE: mass files rename


howdy, i'm trying to use bash commands to rename many files at once because renaming one file by one is very time cosuming and easy to make errors. Sure using "tab" save alot of typing, but i think there's gotta way to make my job a little easier.

here's the example of some of the rename i wanna accomplish:
[apple@localhost test]$ ls
kuku2comic\hunterXhunter\hunter212\212\01.jpg
kuku2comic\hunterXhunter\hunter212\212\02.jpg
kuku2comic\hunterXhunter\hunter212\212\03.jpg
kuku2comic\hunterXhunter\hunter212\212\04.jpg
kuku2comic\hunterXhunter\hunter212\212\05.jpg
kuku2comic\hunterXhunter\hunter212\212\06.jpg
kuku2comic\hunterXhunter\hunter212\212\07.jpg
kuku2comic\hunterXhunter\hunter212\212\08.jpg
kuku2comic\hunterXhunter\hunter212\212\09.jpg
kuku2comic\hunterXhunter\hunter212\212\10.jpg
kuku2comic\hunterXhunter\hunter212\212\11.jpg
kuku2comic\hunterXhunter\hunter212\212\12.jpg
kuku2comic\hunterXhunter\hunter212\212\13.jpg
kuku2comic\hunterXhunter\hunter212\212\14.jpg
kuku2comic\hunterXhunter\hunter212\212\15.jpg

i wanna rename all of 'em from 01.jpg all the way 15.jpg
so it'll become something like:
[apple@localhost test]$ ls
01.jpg 02.jpg 03.jpg 04.jpg 05.jpg 06.jpg 07.jpg 08.jpg 09.jpg
10.jpg 11.jpg 12.jpg 13.jpg 14.jpg 15.jpg

so i don't have to type this 15 times....
any suggestions?
TIA
 
Old 09-17-2005, 08:24 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
They all seem to already have that name. Buf if you want to move them to the current directory, you could do 'mv kuku2comic/hunterXhunter/hunter212/212/* .'
 
Old 09-17-2005, 08:29 PM   #3
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 327Reputation: 327Reputation: 327Reputation: 327
This will do it (among the dozens of possible methods):

Code:
ls -1 kuku2comic\\hunterXhunter\\hunter212\\212\\* | awk -F\\ '{print "mv \"" $0 "\"",$5}' | bash
 
Old 09-17-2005, 08:31 PM   #4
wpyh
Member
 
Registered: Jun 2004
Location: Beijing
Distribution: Slackware 9.1 but FUBAR with packages I compile myself, and OpenBSD (not exactly a distro) on QEMU
Posts: 153

Rep: Reputation: 35
No, they are all in the current directory (notice the backslashes instead of slashes). What I would do if I were sirpelidor is:

$ rename <tab> '' *

the <tab> will give me kuku2comic\hunterXhunter\hunter212\212\, then rename will replace it with ''.

I don't think this is BASH USAGE, since rename is not a bash command.

edit: But <tab> usage is readline usage, which is used by bash. And macemoneta is faster than me

Last edited by wpyh; 09-17-2005 at 08:36 PM.
 
Old 09-17-2005, 08:33 PM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Suppose that you wanted to add the prefix 'hunter212_' to all of the jpg files. This is how I would do it:
for file in *.jpg; do mv "$file" "hunter_$file"; done

The double quotes are only needed if the file names contain a whitespace character, and prevent a name from being split into two arguments by the shell.
 
Old 09-19-2005, 01:42 AM   #6
sirpelidor
Member
 
Registered: Oct 2003
Location: Madison
Distribution: mdk 8.2, 9.0, 9.2, slack 9.1
Posts: 403

Original Poster
Rep: Reputation: 30
Thanks for all the replies:
Matir: they are in the same directory, xx\xxx is a window way to put a white space within filename, and i need to rename them so i can write my own program to access those files.

macemoneta: when tried your method:
[apple@localhost test]$ ls -l kuku2comic\\hunterXhunter\\hunter212\\212\\* | awk -F\\ '{print "mv \"" $0 "\"",$5}' | bash
i got this:
mv: invalid option -- r
did i type something wrong?

wpyh: your idea is awsome, it really did saved me a lot of time, thank you

jschiwal: the programming method you've showed is very clean, what language is that, is it perl? what if i just wanna take off the whole "kuku2comic\hunterXhunter\hunter212\212\" and only keep something like: hunter212_1.jpg? thank you very much

thanks for all the help bash is awsome
 
Old 09-19-2005, 05:30 AM   #7
Earthwings
LQ Newbie
 
Registered: Aug 2005
Location: Karlsruhe, Germany
Distribution: Gentoo, Kubuntu
Posts: 20

Rep: Reputation: 0
Quote:
Originally posted by sirpelidor
jschiwal: the programming method you've showed is very clean, what language is that, is it perl? what if i just wanna take off the whole "kuku2comic\hunterXhunter\hunter212\212\" and only keep something like: hunter212_1.jpg? thank you very much
That was bash as well. Use ${variable#strip-this-string-from-front} for simple renaming:
Code:
for file in *.jpg; do mv -i $file ${file#kuku2comichunterXhunterhunter212212}; done
Using the "-i" parameter for mv makes sure you don't overwrite something accidently.

If all you want is renaming the files with increasing numbers, then this is a nice way as well:
Code:
for file in *.jpg; do mv -i $file $((++i)).jpg; done
$((++i)) increases the variable $i in each turn.

@jschiwal: Quoting is not needed as bash escapes filenames automatically if you use bash filename patterns. This is not true of course if you would replace the *.jpg by something like 'ls -1 *.jpg'
 
Old 09-19-2005, 10:57 AM   #8
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 327Reputation: 327Reputation: 327Reputation: 327
Quote:
Originally posted by sirpelidor
macemoneta: when tried your method:
[apple@localhost test]$ ls -l kuku2comic\\hunterXhunter\\hunter212\\212\\* | awk -F\\ '{print "mv \"" $0 "\"",$5}' | bash
i got this:
mv: invalid option -- r
did i type something wrong?
Yes; the option after the first command (ls) is -1 (one) not -l (lowercase L). The -1 option insures that only a single file is listed per line. If you break the stages down (with a few sample files I created):

$ ls -1 kuku2comic\\hunterXhunter\\hunter212\\212\\*

kuku2comic\hunterXhunter\hunter212\212\01.jpg
kuku2comic\hunterXhunter\hunter212\212\02.jpg
kuku2comic\hunterXhunter\hunter212\212\03.jpg
kuku2comic\hunterXhunter\hunter212\212\10.jpg
kuku2comic\hunterXhunter\hunter212\212\11.jpg

The first stage, above, lists the files that will be manipulated.

$ ls -1 kuku2comic\\hunterXhunter\\hunter212\\212\\* | awk -F\\ '{print "mv \"" $0 "\"",$5}'

mv "kuku2comic\hunterXhunter\hunter212\212\01.jpg" 01.jpg
mv "kuku2comic\hunterXhunter\hunter212\212\02.jpg" 02.jpg
mv "kuku2comic\hunterXhunter\hunter212\212\03.jpg" 03.jpg
mv "kuku2comic\hunterXhunter\hunter212\212\10.jpg" 10.jpg
mv "kuku2comic\hunterXhunter\hunter212\212\11.jpg" 11.jpg

Adding the second stage, above, lists the files to be manipulated to awk, which converts them into the commands that will perform the renames. These are just displayed to standard out.

By adding the pipe to bash, the commands will get executed:

$ ls -1 kuku2comic\\hunterXhunter\\hunter212\\212\\* | awk -F\\ '{print "mv \"" $0 "\"",$5}' | bash

$ ls

01.jpg 02.jpg 03.jpg 10.jpg 11.jpg

Last edited by macemoneta; 09-19-2005 at 10:59 AM.
 
Old 09-20-2005, 12:36 AM   #9
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Quoting is needed if the filename might contain whitespace.

One thing to consider is producing a bashref.dvi or bashref.ps document for printing.
These versions of the bash reference are much better looking that reading through the
info pages. You could produce it from the bashref.texi file by installing the bash source package.

If you have an rpm based system, then install the source rpm like:
Code:
rpm -Uhv bash-3.0.15.src.rpm
Your particular version may very.
Next cd to the SPEC dirctory and apply the patches.
Code:
# cd /usr/src/packages/SPECS   # this is the location for SuSE linux
su
<password>
rpmbuild -bp     # this applies the patches which may include the documentation
Now cd to the BUILD directory and enter the directory for the patched source:
Code:
cd ../BUILD
tar xvjf bash-3.0.tar.bz2
cd bash-3.0
./configure        # this produced the "Makefile"
make dvi            # this produces the .dvi document, bashref.dvi.  ~150 pages.
make ps             # this produces the bashref.ps documents as well as others.
Now there will be print worthy documents in the ./doc/ directory.

Last edited by jschiwal; 10-02-2005 at 09:19 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash - rename directory to overwrite existing LarsWestergren Linux - Newbie 18 04-04-2013 02:07 PM
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
Rename/VI/Awstats Reports Usage nistelrooy Linux - General 7 07-16-2005 09:02 PM
Perl or Bash Mass File Edit redneon Programming 1 10-14-2004 11:43 AM
Mass Rename wickdgin Linux - Newbie 2 04-13-2003 02:29 PM


All times are GMT -5. The time now is 12:07 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration