user1269298
user1269298

Reputation: 737

change file names using bash

Data structure is like following, and I would like to change "lane-7" to "lane-5". I am thinking of a command like this, but it does not work.

    find PATH -name "lane-7*" | xargs -i echo mv {} `echo {}|sed 's/lane-7/lane-5/'` | sh

Anyidea? Thanks

PATH/28/lane-7-22.fq
PATH/28/lane-7-21.fq
PATH/28/lane-7-18.fq
PATH/28/lane-7-24.fq
PATH/28/lane-7-23.fq
PATH/28/lane-7-19.fq
PATH/28/27/lane-7-22.fq
PATH/28/27/lane-7-21.fq
PATH/28/27/lane-7-18.fq
PATH/28/27/lane-7-24.fq
PATH/28/27/lane-7-23.fq
PATH/28/27/lane-7-19.fq
PATH/28/27/26/lane-7-22.fq
PATH/28/27/26/lane-7-21.fq
PATH/28/27/26/lane-7-18.fq
PATH/28/27/26/lane-7-24.fq
PATH/28/27/26/lane-7-23.fq
PATH/28/27/26/lane-7-19.fq
PATH/28/27/26/25/lane-7-22.fq
PATH/28/27/26/25/lane-7-21.fq
PATH/28/27/26/25/lane-7-18.fq
PATH/28/27/26/25/lane-7-24.fq
PATH/28/27/26/25/lane-7-23.fq
PATH/28/27/26/25/lane-7-19.fq
...

Upvotes: 1

Views: 382

Answers (2)

Martin
Martin

Reputation: 38329

Use rename, it was made for this:

find PATH -name "lane-7*" | xargs rename "lane-7" "lane-5"

You might have the perl version of rename instead (Debian installs it by default). In that case, just use a perl expression instead:

find PATH -name "lane-7*" | xargs rename "s/^lane-7/lane-5/"

Upvotes: 2

Mat
Mat

Reputation: 206909

You could do this with a while loop and a bash string substitution:

find PATH -name "lane-7*" | while read -r file ; do
  echo mv $file ${file/lane-7/lane-8}
done

Remove the echo if that appears good.

Upvotes: 2

Related Questions