1

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
...
3
  • 1
    Look up the rename or prename commands (not the rename() function). If it exists on your system, use it. If not, find it; it will be simpler. (The problem is that the backquoted command is executed before find is even started!) Commented Apr 6, 2012 at 16:29
  • How does it fail? If you remove the trailing |sh, you should see if the command looks right, or what's wrong with it. Commented Apr 6, 2012 at 16:30
  • 1
    Here are already some good answers. Just some tips. You can't pipe shell commnds to sh. You may write them to the temporary file and execute that for instance. When working with find and xargs, use find -print0 and xargs -0. Then it works also with filenames that consist spaces. You could mess up a lot otherwise. Commented Apr 6, 2012 at 16:44

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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/"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.