Bash Rename All Files in Dir

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.

  1. A few comments: Use * instead of `ls` and surround filenames in double quotes in case any of your files have spaces in them:

    for file in *; do mv \"$file\" \"$file.png\"; done

    Also, another handy tip: ALWAYS test first with echo.

    for file in *; do echo mv \"$file\" \"$file.png\"; done

    – Ben

  2. 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

Contact Us

We'd love to hear from you. Get in touch!

Mail

P.O. Box 961436
Boston, MA 02196