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нιη
  • 40,939
  • 15
  • 71
  • 114
  • 3
    Possibly related [Renaming a bunch of files with date modified timestamp at the end of the filename?](https://unix.stackexchange.com/questions/43338/renaming-a-bunch-of-files-with-date-modified-timestamp-at-the-end-of-the-filenam). – Paulo Tomé Feb 01 '20 at 15:57
  • Does this answer your question? [Renaming a bunch of files with date modified timestamp at the end of the filename?](https://unix.stackexchange.com/questions/43338/renaming-a-bunch-of-files-with-date-modified-timestamp-at-the-end-of-the-filenam) – U880D Feb 01 '20 at 16:24

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.

  • [Why *not* parse `ls` (and what to do instead)?](https://unix.stackexchange.com/questions/128985/why-not-parse-ls-and-what-to-do-instead) – αғsнιη Feb 01 '20 at 16:13
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
  • 78,509
  • 12
  • 109
  • 152