0

I have these files, which were modified today and yesterday, respectively:

a.txt
b.png

And I want them renamed to:

2020-02-01.a.txt
2020-01-31.b.png

How to do that in a single Bash command?

αғsнιη
  • 41,407
awvalenti
  • 191

2 Answers2

1
ls | while read FILE; do mv "$FILE" "$(stat "$FILE" -c '%y' | cut -b -10). $FILE"; done

Tested on Git Bash on Windows and Linux Mint.

awvalenti
  • 191
0

Using the Perl-based rename utility:

$ rename -n 'use POSIX qw(strftime); s/^/strftime("%Y-%m-%d.",localtime((stat $_)[9]))/e' *.*
rename(a.txt, 2020-02-01.a.txt)
rename(b.png, 2020-02-01.b.png)

(On some systems, rename may be provided as prename).

steeldriver
  • 81,074