Rename files numerically (date/time order)
Solution 1
The following will break on files containing newlines, but should work the rest of the time. It will sort the files based on the time they were last modified, rather than their actual creation time, because Ubuntu doesn't store the creation time of files. So if you've modified the files since you downloaded them, you won't get an accurate ordering.
n=0; ls -tr | while read i; do n=$((n+1)); mv -- "$i" "$(printf '%03d' "$n")"_"$i"; done
ls -tr
sorts files by modification time, oldest first (and when you pipe the output of ls it automatically lists files one-per-line rather than the standard way of doing things -- it should be noted that this is a GNU-ism, if you have to work on another *nix with a different version of ls
, this might not be the case). while read i
takes that list and goes over each item one at a time, and the rest of it does the actual renaming.
n=$((n+1))
increments the variable $n
by one. There could be problems if this had been set beforehand, so to be on the safe side you should set it to 0 at the beginning of the line.
$(printf '%03d' "$n")
prints the number contained in the variable $n, padded to three zeros (so 001, 002 ... 087 ... 999). I hope that the mv
command is fairly obvious.
Solution 2
The following script will do the job:
#!/bin/bash
if [ $# -ne 1 ];then
echo "Usage: `basename $0` DIRECTORY"
exit 1
fi
count=1
ls -tr $@ | while read file; do
if [ $count -lt 10 ]; then
mv -v $file '00'$count'_'$file
elif [ $count -lt 100 ]; then
mv -v $file '0'$count'_'$file
else
mv -v $file '0'$count'_'$file
fi
count=$(($count+1))
done
This I just test it and it's worked for me.
Related videos on Youtube
Kian
Updated on September 18, 2022Comments
-
Kian over 1 year
I have 200 files in a folder. I downloaded them in a certain order -- often only a few seconds apart. I would like to append a number to the beginning of each of the files.
So the first file I downloaded (the oldest one) would need to change from
name.txt
to001_name.txt
.All the way up to the last file (most recently downloaded) changing from
name.txt
to200_name.txt
.How can I do it using the command-line?
-
Radu Rădeanu over 10 yearsDon't use
sort -r
. It will mess upls -t
. In factls -t | sort -r
is equivalent withls | sort -r
. Uselt -tr
only! -
Kian over 10 yearsThank you for your reply. For some reason it doesn't put it in the right order. However I tried it without sort -r and that does put it in the correct reverse order. Perhaps there's something unusual about sort -r.
-
Kian over 10 years@RaduRădeanu so what should the correct statement be?
-
Kian over 10 yearsI think it should be ls -tr | while read i; do n=$((n+1)); mv -- "$i" "$(printf '%02d' "$n")"_"$i"; done
-
evilsoup over 10 yearsThank you @RaduRădeanu -- I should have checked to see if
ls
has its own reversing, that saves a pipe. Looking at thesort
man page, I can see what you mean, that was a silly mistake. @fushilatitude you are correct, that is the right way of doing it. -
Kian over 10 yearsThank you both very much for all your help. It is highly appreciated. Indeed, that solution has been tested and is successful for me.