Solved

Bulk find and rename files in Linux

Posted on 2011-03-25
6
898 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

685 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