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
- for file in *.loaded ; – for file loops through all the files with the “.loaded” suffix in that directory
- 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.
- 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.