Want to use the find command with a number of rename commands. How can I do it.
This is the sequence I want to execute with the find
command
rename "s/ /-/g" * ; rename 'y/A-Z/a-z/' * ; rename 's/[.]-/-/g' * ; rename 's/--/-/g' *
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityYou don't need separate rename
's here. The first argument
to rename can be any perl
code that does the modifications you want on the file path (as stored in $_
), you don't have to use s/pattern/replacement/flags
let alone only one of them.
Here, you could just do:
find . -depth -exec rename -n -d '
$_ = lc $_; # lowercase
s/[ .-]*[ -]+/-/g' {} +
Or if your rename
doesn't support -d
:
find . -depth -mindepth 1 -exec rename -n '
my ($dirname, $basename) = m{(.*)/(.*)}s;
$basename = lc $basename; # lowercase
$basename =~ s/[ .-]*[ -]+/-/g;
$_ = "$dirname/$basename"' {} +
here turning all <0-or-more-spaces-or-dot-or-dash><1-or-more-spaces-or-dash>
to a single dash, but you could do all your s/.../.../
s instead, separated with ;
s:
find . -depth -exec rename -n -d '
s/ /-/g;
y/A-Z/a-z/;
s/[.]-/-/g;
s/--/-/g' {} +
In any case, you want to make sure the substitutions are only applied to the basename (hence the -d
) and that leaves are renamed before the branches they're on (hence the -depth
).
(-n
s above are for dry-run).