Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I replace a string in all filenames in a directory? (specifically I need to remove "\#015" from all file names in a directory

Many files in a directory (/root/path/) have a strange character string appended to them (\#015). Help me replace them with regular names without the strange string.

I need:

/root/path/img1.png\#015
/root/path/img2.jpg
/root/path/img3.png\#015

To be:

/root/path/img1.png
/root/path/img2.jpg
/root/path/img3.png

Can you help?

like image 330
Ryan Avatar asked Nov 16 '12 16:11

Ryan


People also ask

How do I replace text in all files in a folder?

Remove all the files you don't want to edit by selecting them and pressing DEL, then right-click the remaining files and choose Open all. Now go to Search > Replace or press CTRL+H, which will launch the Replace menu. Here you'll find an option to Replace All in All Opened Documents.


1 Answers

for file in *\#015
do
   mv -- "$file" "${file%\#015}"
done

You may need to escape the "\"s. Try it in a tmp directory first.

like image 53
Ed Morton Avatar answered Nov 11 '22 20:11

Ed Morton