Solved

Bulk find and rename files in Linux

Posted on 2011-03-25
6
871 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

759 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

20 Experts available now in Live!

Get 1:1 Help Now