Hi,
Is there an easy way to reverse filenames? Example: abcdef.jpg becomes fedcba.jpg.
Hi,
Is there an easy way to reverse filenames? Example: abcdef.jpg becomes fedcba.jpg.
You can use the rev command
If you don't want to reverse the suffix as well, you will need to remove and replace it e.g.Code:DESCRIPTION The rev utility copies the specified files to standard output, reversing the order of characters in every line. If no files are specified, stan‐ dard input is read.
Code:$ f="abcdef.jpg" $ $ r="$(rev <<< "${f%.*}").${f##*.}" $ $ echo "$r" fedcba.jpg
Thanks for mentioning the rev command. Does it actually reverse the filename on disk (this it what I want), or does it simply make a string of the reverse characters?
it's a string that can be plugged into mv command, thus solving your problem
in steeldriver's example it would be
Code:mv "$f" "$r"
if your question is answered, mark the thread as [SOLVED]. Thx.
To post code or command output, use [code] tags.
Check your bash script here // BashFAQ // BashPitfalls
Thanks for explaining, vaphell.
Is there a way to combine the rev and mv command in one command that I could reuse, while simply replacing the filename?
Well you could just do a "one-liner" to loop over the list of files, like
However I thought about this some more in the meantime - since Ubuntu provides the perl-based rename command, and perl has its own string reverse function, you should be able to do something likeCode:for f in *.jpg; do echo mv "$f" "$(rev <<< "${f%.*}").${f##*.}"; done
The -n option is only for testing purposes.Code:rename -vn -- 's/(.*)[.]([^.]*)$/join(".",my $rev=reverse($1),$2)/e' *.jpg abcdef.jpg renamed as fedcba.jpg pqrstu.jpg renamed as utsrqp.jpg
Last edited by steeldriver; June 29th, 2014 at 06:43 PM.
Bookmarks