mv
It's a basic command line designed to do one thing and do it well (Unix philosophy) : move file(s) or directorie(s).
You can hack STDOUT
& STDIN
¹ to modify on the fly the destination string, but it's just not a smart hack
rename (Perl's one)
There are other tools with the same name which may or may not be able to do this, so be careful.
When people talk about rename
, we think about this one, not the ELF
one, less powerful (magic?).
It's not basic, it's Perl. You can pass some Perl's functions inside, and it's extremely powerful.
Consider this example :
You want to rename a bunch of files like
foobar_1.txt
foobar_2.txt
foobar_3.txt
You can prepend zeros to the digits with sprintf()
like this (using regex, heh, it's Perl :D ) :
rename 's/(\d+)/sprintf("%04d", $1)/e' foobar_*.txt
Now you have :
foobar_0001.txt
foobar_0002.txt
foobar_0003.txt
Not really a basic command, isn't it ?
rename is not really designed to move dir(s), but it can do it :
$ mkdir -p /tmp/foo/bar/base
$ touch /tmp/foo/bar/base/file
$ rename 's!/tmp/foo/bar/base/file!/tmp/file!' /tmp/foo/bar/base/file
The moved file
/tmp/file
¹ some code we see on *.stackexchange.*
websites
for FILE in `ls *.txt`
do
mv ${FILE} `echo ${FILE} | sed 's/anything_ugly/anything_still_ugly/'`
done
It's not the way to go, it's plain buggy, just to explain why to use the right tool at the right moment
type -a rename
andrename --version
? – Jeff Schaller Apr 04 '19 at 17:57rename is /usr/bin/rename
andrename (util-linux-ng 2.17.2)
respectively. – Urda Apr 04 '19 at 17:58rename
can mean different things in different distros. – Urda Apr 04 '19 at 18:05