Something I have never known how to do in Linux, is rename a directory full of files. (Crazy right?!) Fortunately for a noob like msyelf, Sam Bisbee does know how to do this, and showed me today. In the following example, I am renaming a directory of files with no file-extension, and moving them to the same location with “.png” appended.
for file in `ls` ; do mv $file $file.png; done
Simple. Useful. Documented.
Comments
We moved off of Disqus for data privacy and consent concerns, and are currently searching for a new commenting tool.
Thanks for the handy shell tip guys!
A few comments: Use
*
instead of`ls`
and surround filenames in double quotes in case any of your files have spaces in them:Also, another handy tip: ALWAYS test first with echo.
– Ben
Thanks for the tips @Cowboym that will def. come in handy!!
… or you could just use `rename` http://www.markus-gattol.na… … or even better … some Python one-liner magic :-]
and to remove something from the filenames? (I converted a .nrg to an iso, and all the files in the iso have a ‘;1’ in the end, eg ‘setup.exe;1’)
to remove something (in my case \”;1\”) from files in the current directory:
for file in *; do mv \”$file\” \”$(echo \”$file\” | sed ‘s/;1//’)\”; done