Solved

Bulk find and rename files in Linux

Posted on 2011-03-25
6
878 Views
Last Modified: 2013-11-22
Hi,

I wonder if someone can help me out with a command to accomplish a bulk rename.

I have hundreds of files within multiple sub directory's that contain a name I like to replace.

lets say the word is "thomanji"

I have files like:

something_thomanji_something.extension
thomanji_something.extension
something_thomanji.extension
thomanji.extension
somethingthomanjisomething.extension
somethingthomanji_something.extension
etc.

so this means the word could be anywhere in the filename.

now I try to use find to search through the directory and find filenames that contain "thomanji" anywhere withing the filename and replace this word with something else.

Is this possible, maybe with a find command?

I would appreciate your assistance on this.

Best wishes,
Thom
0
Comment
Question by:Thomanji
  • 3
  • 2
6 Comments
 
LVL 2

Accepted Solution

by:
NickGermi earned 500 total points
ID: 35213463
well the command to rename multiple files within a directory is:
rename "the perfix you want to get rid of" "" *.ext

Open in new window


now to run it within each directory you would need to create a ssh script, first list all directories and sub directories and export the output into a text file and then edit the text file and put the rename command there

to list all directories and sub directories and to export them into a text file you can run this command:
find <startdirectory> -type d > YourList.txt

Open in new window


To do all above in a single line of code, try the code bellow, (experimental, I haven't tested it)

find <startdirectory> -type d -exec rename "the perfix you want to get rid of" "" *.ext {} \;

Open in new window

0
 
LVL 3

Expert Comment

by:MicMatic
ID: 35213465
0
 

Author Comment

by:Thomanji
ID: 35213680
Hi,

Thanks for the quick answers.

find <startdirectory> -type d -exec rename "the perfix you want to get rid of" "" *.ext {} \;



so i could run something like:

find <startdirectory> -type d -exec rename "thomanji" "thomas" *.ext {} \;

so that it would find the files that have thomanji anywhere in the file name and replace this with thomas

filethomanij.jpg to filethomas.jpg

Is this correct

Best wishes,
Thomas
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 2

Expert Comment

by:NickGermi
ID: 35213717
In theory yes but as I said I didn't have the time to test it, the command rename "thomanji" "thomas" *.ext alone will for sure rename all files within the directory you are running the command from however I'm not sure about the long find command
0
 

Author Comment

by:Thomanji
ID: 35214330
Thanks Nick,

It helped me on the right track. It did not work but with tweaking the find command all worked out.

I used

find /path/ -name "*.ext" -type f -exec rename "old value" "new falue" *.ext {} \;

Best wishes,
thom
0
 

Author Closing Comment

by:Thomanji
ID: 35214336
Thanks for the help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now