Adam Mierzwiak
Adam Mierzwiak

Reputation: 420

Change of directories names according to pattern in Bash

I need to change with one-liner (best option) directories name, more precise - to remove some prefix that has variable part at the beginning TO_BE_REMOVED_W._

I can find dirs to modification with:

find ./ -type d -iname 'TO_BE_REMOVED_W*' -exec mv {} _____ \;

How should I form execution part to rename directories to remove pattern from whole name?

Upvotes: 0

Views: 267

Answers (2)

tdy
tdy

Reputation: 41407

Most distros come with rename which accepts substitution patterns, so I usually use -execdir with rename.

For example, to remove PREFIX_:

$ find . -execdir rename 's/^PREFIX_//' '{}' +

Note that for Arch Linux, this is called perl-rename (Arch's default rename is not the standard rename found on most distros).

Upvotes: 2

pynexj
pynexj

Reputation: 20798

You can use -exec bash -c '...' like this:

$ find -name 'foo*'
./foo-01
./foo-05
./foo-02
./foo-03
./foo-04
$ find -name 'foo*' -exec bash -c 'file=$0; echo Now you can do anything with $file ...' {} \;
Now you can do anything with ./foo-01 ...
Now you can do anything with ./foo-05 ...
Now you can do anything with ./foo-02 ...
Now you can do anything with ./foo-03 ...
Now you can do anything with ./foo-04 ...
$

Upvotes: 1

Related Questions