4

I am trying to rename a file with the mv command and I would like to add the last modified date of this file (with YYYYMMDD format) on the file name and move it to other folder. I got the current system date like this:

mv /home/user/Desktop/a.txt "/home/user/Desktop/testfolder/$(date +%Y%m%d)-a.txt"

I used other combinations of date but I didn't get it. I need to know if it is possible do it directly by command line or I need to create a script.

3 Answers 3

6

You can try this command:

mv /dir_path/example.txt /new_dir_path/$(date +%Y-%m-%d -r example.txt)example.txt
1
  • 2
    Yes the command "date +%Y-%m-%d -r example.txt" gives last modified date of a file. Jan 1, 2017 at 13:20
1

It is probably easiest to get things correct with a small script file.

-o-

I think you want to use the modification date instead of the current date, see the output from

ls -l a.txt

or

ls -l --time-style=long-iso a.txt
0
0

Thanks, I got it! Only one detail. If you are using the command from a path where the original file is not, you need to use this:

mv /dir_path/example.txt /new_dir_path/$(date +%Y-%m-%d -r /dir_path/example.txt)example.txt

0

You must log in to answer this question.

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