In bash, I have list of files all named the same (in different sub directories) and I want to order them by creation/modified time, something like this:
ls -1t /tmp/tmp-*/my-file.txt | xargs ...
I would like to rename those files with some sort of index or something so I can move them all into the same folder. My result would ideally be something like:
my-file0.txt
my-file1.txt
my-file2.txt
Something like that. How would I go about doing this?
You can just loop through these files and keep appending an incrementing counter to desired file name:
EDIT: In order to sort listed files my modification time as is the case with
ls -1tyou can use this script:This handles filenames with all special characters like white spaces, newlines, glob characters etc since we are ending each filename with NUL or
\0character in-printfoption. Note that we are also usingsort -zto handle NUL terminated data.