I am going to rename image files and pdf files in a folder in Linux.
It should be recursively as some files are in sub-sub folder.
Current file name pattern is like:
an-yt-h-in-g-123word-123456-anything.jpg (.png .pdf)
My goal pattern is
123456-anything.jpg (.png .pdf)
In a short, I want to remove everything before -NumberString-
, and keep everything afterwards (includes the NumberString
).
Can anyone help? Thanks a lot
Advertisement
Answer
This command will check position of last “number string” and output the sub string from this position to the end. You can change ls
command to something else.
ls | awk -F '-' '{ for(i=NF; i>=0; i--) { if (match($i, /^[0-9]+$/)) { print $i; for (j=i+1; j<=NF; j++){ printf "-%s" ,$j }; print "n" } } }' $1 ORS=""