• info@bizmate.biz

Bulk rename files in linux

Bulk rename files in linux

I had to rename about 200 files that were renamed by an application from MyFile to MyFile.loaded

After reading a few tutorials and forums articles online this is the solution that worked for me on my Ubuntu machine

For file approach

for file in *.loaded ; do mv “$file” “`echo $file | sed ‘s/\.loaded//’`” ; done

Just to quickly explain what he command does
  1. for file in *.loaded ; –  for file loops through all the files with the “.loaded” suffix in that directory
  2. do mv “$file” “`echo $file | sed ‘s/\.loaded//’`”; – this line executes the renaming itself through the MV command. The mv source target command in this line is executed with the original file name ($file)  with the new name generated with the help of SED. In this case for instance if you wanted to replace .loaded with .html the sed command would be executed as sed ‘s/\.loaded/\.html/’` , please note the escaping backslashes when modifying this line.
  3. done; – this just ends the execution of the for loop.

Using Find

Using find to remove spaces can be done with this chain of commands…

find . -depth -name “* *” | while read name; do mv “$name” “$( echo $name | sed ‘s/ /_/g’)”;done

I hope the above helps and if you would like to leave a comment please use the contact link on this site.