Rename File Command in Unix with Timestamp

Linux Rename file with only time/date stamp

mv myfile.txt `date +%Y_%m_%d_%H:%M:%S`.txt

Bash: Rename file name to unix timestamp of modification date

find . -type f -exec \
sh -c '
for i do
d=$(dirname "$i")
[ "$d" = / ] && d=
n=${i##*/}
echo mv "$i" "$d/u-$(stat -c %Y "$i") $n"
done' _ {} +
  • This operates recursively in the current directory (.). It only targets regular files (not directories etc). Modify -type f and other flags if needed.

  • It just prints the mv commands, so you can review them. Remove the echo to run for real.

  • We use find to list the target files, and its -exec flag to pass this list to a shell loop where we can parse and modify the filenames, including stat to get the modification time.

  • I don't know your use case, but a better solution may be to just save the output of: find . -type f -printf '%p u-%T@\n' in a file, for later reference (this prints the file path and modification time on the same line). Also, maybe a snapshot (if possible).

Renaming files in unix of particular date

You can modify a.sh like this :

find . -maxdepth 1 -type f -iname "*.temp" -exec bash -c 'mv $0 ${0//.temp/}' {} \;

newermt is for the date the file is modified last time.

and can execute it as

./a.sh 2017-09-27

To test if this command works on command line, you can try this outside your script:

find . -maxdepth 1 -type f -iname "*.temp" -exec bash -c 'mv $0 ${0//.temp/}' {} \;

linux-shell: renaming files to creation time

Naming based on file system date

In the linux shell:

for f in *.jpg
do
mv -n "$f" "$(date -r "$f" +"%Y%m%d_%H%M%S").jpg"
done

Explanation:

  • for f in *.jpg
    do

    This starts the loop over all jpeg files. A feature of this is that it will work with all file names, even ones with spaces, tabs or other difficult characters in the names.

  • mv -n "$f" "$(date -r "$f" +"%Y%m%d_%H%M%S").jpg"

    This renames the file. It uses the -r option which tells date to display the date of the file rather than the current date. The specification +"%Y%m%d_%H%M%S" tells date to format it as you specified.

    The file name, $f, is placed in double quotes where ever it is used. This assures that odd file names will not cause errors.

    The -n option to mv tells move never to overwrite an existing file.

  • done

    This completes the loop.

For interactive use, you may prefer that the command is all on one line. In that case, use:

for f in *.jpg; do mv -n "$f" "$(date -r "$f" +"%Y%m%d_%H%M%S").jpg"; done

Naming based on EXIF Create Date

To name the file based on the EXIF Create Date (instead of the file system date), we need exiftool or equivalent:

for f in *.jpg
do
mv -n "$f" "$(exiftool -d "%Y%m%d_%H%M%S" -CreateDate "$f" | awk '{print $4".jpg"}')"
done

Explanation:

The above is quite similar to the commands for the file date but with the use of exiftool and awk to extract the EXIF image Create Date.

  • The exiftool command provides the date in a format like:

    $ exiftool -d "%Y%m%d_%H%M%S"  -CreateDate sample.jpg
    Create Date : 20121027_181338

    The actual date that we want is the fourth field in the output.

  • We pass the exiftool output to awk so that it can extract the field that we want:

    awk '{print $4".jpg"}'

    This selects the date field and also adds on the .jpg extension.

how to rename an existing file with today's date

This is because a file name cannot contain slashes /. When you indicate

mv foo.c foo`date +%D`.c

It internally does:

mv foo.c foo01/10/14.c

so it tries to move the file foo.c into foo01/10 directory.


Instead, you can do:

date "+%F"

Which returns the date in a 2014-01-10 format and will expand into

mv foo.c foo2014-01-10.c


Related Topics



Leave a reply



Submit