-1

I have files I would like to rename it using the date they created. How can I do this automatically? Looking at the properties for every file then renaming it is a real pain. For example

In a folder let's say I have:

file1
file2
file3
file4
file5       
    . . . . . etc

How can I rename the files by the day they created

file1_2018-10-06
file2_2013-10-12
file3_2014-09-26
file4_2018-10-06
file5_2017-11-13
    . . . . . etc

3 Answers 3

2

Assuming "created" means mtime1, and (ab)using the Perl-based rename:

$ rename -n 'BEGIN{use POSIX;} $_ .= strftime("_%x", localtime( (stat($_))[9] ))' file?
rename(file1, file1_2018-09-23)
rename(file2, file2_2018-09-15)
rename(file3, file3_2018-10-01)
rename(file4, file4_2018-09-15)
rename(file5, file5_2018-10-06)

(-n is a dry-run mode - remove it once you are happy with the proposed re-namings).


1 for the reasons discussed here: How to find creation date of file?

1

You can do something like this:

#!/bin/bash
cd folder
ls -l --time-style=long-iso | awk '{print $8 " " $6}' | while IFS= read line; do

filename=$(echo $line | awk '{print $1}')
date=$(echo $line | awk '{print $2}')

mv ${filename} ${filename}_${date}

done
0
1

stat available? Try

stat -c"mv %n %n_%y" file* | sed 's/ [^ ]* [^ ]*$//'
mv file file_2018-10-04
mv file~ file~_2018-09-30
mv file1 file1_2018-10-05
mv file1~ file1~_2018-10-04

and pipe through sh if happy with the result. Or source it by "process substitution".

You must log in to answer this question.