Skip to content
Advertisement

Linux – add creation Date or DateTime in the name of the file

I have a file on a Linux server which is created automatically. I would like to create a script which will automatically rename the file after it is created on the server, so it will add date or datetime information

For example:

filename.pdf --->210807filename.pdf

Thank you!

Advertisement

Answer

Try this:

mv filename.pdf $(date +"%y%m%d")filename.pdf

Here is the manpage of date where you can find the different output formats: http://linuxcommand.org/man_pages/date1.html

If you like to rename all the files with a pattern using a script, you could do something like this:

for file in $(ls -1 filename???.pdf); do
    mv $file $(date +"%y%m%d")$file
done

This will find all the files with a name like filename???.pdf (??? is a wildcard for 3 characters) and add the timestamp in front of their names.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement