user3780835 user3780835 - 11 months ago 37
Linux Question

Rename a file in linux which has date appended to it

There is a single file in a folder which has date and time appended to it. I would like to rename it to something else so that I can access it easily. I know the starting word of this file name. Is they there a way I can rename this (using wildcard or something)? . Can't use Tab since I am trying to write a script to automate something.

Also, I would like to access the lexicographical last element and rename it if there are multiple files.


You want to find the last changed file in a directory? Which matches a pattern?

find . -maxdepth 1 -mindepth 1 -type f -name "prefix*" \
  -printf "%TY%Tm%TdTH%TM %p\n" | sort -nr | read -r _ file

printf "%s" "$file"

Assuming no filename contains newlines. And that you actually just want to get the last changed file in a directory.

Alternative you can sort do something like this:

find . -maxdepth 1 -mindepth 1 -type f -name "prefix*" | sort -nr -t- -k2

Which will sort files like this: