So I have a list of files that have numbers similar to the example below:
/list/CAP_0045.dat
/list/CAP_0044.dat
/list/CAP_0046.dat
I want to rename the file with the largest number to add a prefix to it. How do I do this?
So I have a list of files that have numbers similar to the example below:
/list/CAP_0045.dat
/list/CAP_0044.dat
/list/CAP_0046.dat
I want to rename the file with the largest number to add a prefix to it. How do I do this?
You can use command substitution for this (read man sh and look for it).
If ls /list | tail -n 1 prints the correct file you can do this:
file=$(ls /list | tail -n 1)
mv "/list/$file" "/list/PREFIX$file"
EDIT: As pointed out by @Wildcard this can fail if the file names contain newlines.
A solution that should work even with newlines in file names uses find -print0 and {sort,head,tail} -z (seems not all versions support the -z/--zero-terminated option, GNU does):
file=$(find /list -print0 | sort -z | tail -n 1 -z)
mv "$file" "$(dirname "$file")/PREFIX$(basename "$file")"