Find the answer to your Linux question:
Results 1 to 6 of 6
I have a list of files from different directories that I would like to copy into one new directory. The catch is, all the files have the same name (e.g., ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2012
    Posts
    3

    copy a list of files and rename them


    I have a list of files from different directories that I would like to copy into one new directory. The catch is, all the files have the same name (e.g., /home/pat/foo.txt, /home/ned/foo.txt). I'd like to rename them with part of the filepath of the directory I'm copying them (e.g., pat_foo.txt, ned_foo.txt). Does anyone have any suggestions? I would really appreciate it!

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,318
    I suggest you use a combination of find and sed in a bash loop. pseudo-code:

    for file in (find $DIR -name $NAME); do
    newfile=$(echo $file|sed "/replace_this/with_this/")
    copy $file $newfile
    done

  3. #3
    Just Joined!
    Join Date
    Sep 2012
    Posts
    3
    for file in (find $DIR -name $NAME); do
    newfile=$(echo $file|sed "/replace_this/with_this/")
    copy $file $newfile
    done[/QUOTE]

    Thanks! The part I am not clear on is the /replace_this/with_this". I want to prepend part of the original filepath as the new filename. Since I have a text list of files that includes the filepaths, it seems like there should be some way to script taking part of that string and prepending it to the new file name. I am trying to avoid having to type in the new name for each file individually.

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,318
    Quote Originally Posted by ChristinaH View Post
    Thanks! The part I am not clear on is the /replace_this/with_this". I want to prepend part of the original filepath as the new filename. Since I have a text list of files that includes the filepaths, it seems like there should be some way to script taking part of that string and prepending it to the new file name. I am trying to avoid having to type in the new name for each file individually.
    okay, here's a working example of what I was thinking:

    Code:
    #!/bin/bash
    
    # create some dirs and a file
    dir1=/tmp/foo/bar/
    mkdir -p $dir1
    touch $dir1/file.txt
    
    # use find to list the file
    file=$(find /tmp/foo/bar -type f)
    echo file: $file
    
    # use dirname to get the file path
    pdir=$(dirname $file)
    echo pdir: $pdir
    
    # use sed to strip everything from the first char to the last "/"
    subdir=$(echo $pdir|sed -e 's|^.*/||')
    echo subdir: $subdir
    
    # use basename to get the filename and prepend the subdir to it
    newfile="${subdir}_$(basename $file)"
    echo new file: $newfile
    
    echo copying $file to $newfile ...

  5. #5
    Just Joined!
    Join Date
    Sep 2012
    Posts
    3
    Quote Originally Posted by atreyu View Post
    okay, here's a working example of what I was thinking:

    Code:
    #!/bin/bash
    
    # create some dirs and a file
    dir1=/tmp/foo/bar/
    mkdir -p $dir1
    touch $dir1/file.txt
    
    # use find to list the file
    file=$(find /tmp/foo/bar -type f)
    echo file: $file
    
    # use dirname to get the file path
    pdir=$(dirname $file)
    echo pdir: $pdir
    
    # use sed to strip everything from the first char to the last "/"
    subdir=$(echo $pdir|sed -e 's|^.*/||')
    echo subdir: $subdir
    
    # use basename to get the filename and prepend the subdir to it
    newfile="${subdir}_$(basename $file)"
    echo new file: $newfile
    
    echo copying $file to $newfile ...
    I'm sorry - this project got bumped back for a couple of days. OK, if I have not already done so, let me establish myself as a newbie with this question. This code is really helpful, well-annotated and works. Right now when I run it, it very nicely gets the basename from the file defined as $file. What I am now stuck on is that I need to do this for a list of files. I have a text file and each line is a filepath. I need to iteratively read each line, copy the file on that line to a new directory, and then rename it like this script does. All the files I am copying have the same name (but different source directories), so I need to rename them before the next file is written to the new directory. Any suggestions for how to take the next step of reading in the file and going through it line by line to do this?

  6. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,318
    Quote Originally Posted by ChristinaH View Post
    What I am now stuck on is that I need to do this for a list of files. I have a text file and each line is a filepath. I need to iteratively read each line, copy the file on that line to a new directory, and then rename it like this script does. All the files I am copying have the same name (but different source directories), so I need to rename them before the next file is written to the new directory. Any suggestions for how to take the next step of reading in the file and going through it line by line to do this?
    There are many ways to do this. A while loop in bash is quite common and reliable. Read up on it here:

    How to read a file line by line

    then try your hand at a script. If you have problems, post back!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •