In some dir I have many files named


I want rename/renumerate files


Maby, I will need begin numeration NOT from 0001


3 Answers 3


Let's say the directory that contains these files is /path/to/dir
the script you need will look like this:

cd "/path/to/dir"
for file in ./*;do
  mv "${file}" "FILE_$(printf "%04d" $start)"
  • 1
    Warning: 1. you will move the files in the remote path to the directory from where you are running the script and not renaming where they are. 2. If in the path there are some spaces an you did not protect that value in the script, it will try to move the split directory name in FILE_0001 ... Example /path/to/dir old/* it will try to move mv /path/to/dir FILE_0001 and mv old FILE_0002
    – Hastur
    Commented Sep 28, 2015 at 7:24
  • 1
    An song one time said ops I did it again :)) cd "/path to my/dir". If there is a space in the path it will fail to change and it will split rename all the current directories. 8-O Of course there is no problem if there are no spaces as in /path/to/dir.
    – Hastur
    Commented Sep 28, 2015 at 8:23
  • You're welcome, it is not so much about script writing, it's more about to extend their use once that you write. I mean today you write it for a purpose, and it works fine; tomorrow you will use to another purpose expecting it works fine too. Each time you have to think about all strange problem that can arise, because if they exist they will come :-) and of course even more when you move or erase files or you execute commands with sudo. :-)
    – Hastur
    Commented Sep 28, 2015 at 9:31
  • This is file from camera, names NOT contain spaces (DSC_{number}.jpg) Some files was be deleted. I do not want to have gaps in the numbering. The above script is enough. Thank you. Commented Sep 28, 2015 at 18:14
  • @YuchimenkoIgor: I leave the comments and we correct the scripts, because the question you did will be useful for other people too (at least we hope). We have to remember that in many camera types it's possible to decide the prefix for the filenames and to store them in subdirectories: in both cases the spaces are allowed characters.
    – Hastur
    Commented Sep 30, 2015 at 10:50


ls | awk '{printf "mv %s FILE_%04d\n",$0,NR ;} ' | bash
  • make sure no file have funny names.
  • remove | bash part to have a preview.
  • use NR+666 to start at 667.
  • use %02d or %06d to genrate 01 or 000001 like figure.
  • 1
    Warning in general it is not safe to parse the output of ls... that means check if there are not funny names as the ones with a space in the middle :-) At least the space problem you can fix with some gym inside the brackets: instead of printf "mv %s FILE_%04d\n" you can use printf "mv \"%s\" \"FILE_%04d\"\n"
    – Hastur
    Commented Sep 28, 2015 at 7:10

You can do something like:

Counter=${1:-1}   # It will take the starting number as 1st parameter
WhichDir=${2:-.}  # It will take the path as 2nd parameter

                  # We try to see if the directory exists and if you can go there
(cd "$WhichDir" 2>/dev/null) || { echo "# $WhichDir NOT FOUND " ; exit 1; }

cd "$WhichDir"                            # it exists and you can go there
shopt -s nullglob                         # see notes
for f in DSC_*                            # for all the files in this dir
  New_Name=$(printf "FILE_%04d.jpg" $Counter)
                                          # change .jpg in what you need
  Counter=$(($Counter +1))                # increase the counter
  echo "mv -i \"$f\" \"$New_Name\" "      # the -i will prompt before overwrite
  # mv -i "$f" "$New_Name"                # uncomment this line to make it works
exit 0


  • When you play with mv can be dangerous. It can overwrite existing files. It can move/rename directory. So it's better to have a visual check before to execute a script. Then you can pipe its output in a shell:
    e.g. ./Myscript.sh, it's all ok? If yes then you can write ./Myscript.sh | /bin/bash (or you can modify it wiping out the "echo" or the comment in the following line).
  • It's better to write mv -i: it will prompt before overwrite
  • If there are no DSC_* files in current directory the shopt -s nullglob will avoid expansion errors.

  • In general it is not safe to parse the output of ls even if in your case it should be not a real problem, because you should have for the files the standard name from camera DSC_nnnn.jpg.

  • Usually there is an extension (maybe .jpg or .tif or .raw)... change it or erase it in the script to fit your needs.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .