Find the answer to your Linux question:
Results 1 to 7 of 7
Hi all, I am new to this forum,so please ignore any kind of incognizance. I have a situation wherein i will have to copy 1000+ files or may be move ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2012
    Posts
    3

    Copy Multiple files


    Hi all,

    I am new to this forum,so please ignore any kind of incognizance.

    I have a situation wherein i will have to copy 1000+ files or may be move them from one location to another.I am trying to put all the file names in a text format and then read the file and copy the file from required location.But it does not seem to work.
    The code is here -

    FILE="cat /xyz/abc/test.txt"
    while read LINE
    do
    cp /xyz/abc/hjfdhj/"$LINE" $Target
    echo "file copied:"$LINE"">>/xyz/abc/FileCopyLog_`date +%d%m%Y_%H%M%S`.txt
    done < $FILE


    Please help me with it.


    Thanks!

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    you do not need to cat the file, just do:
    FILE="/xyz/abc/test.txt"

    now i take it you want to copy each file listed in file (one per line, i assume?) to some directory, or alternate filename. what is that directory/filename? i.e., what is $Target supposed to be? maybe you just left that part out of the script, and the actual copy works, but if $Target is not defined, it wont.

  3. #3
    Linux Engineer
    Join Date
    Apr 2012
    Location
    Virginia, USA
    Posts
    883
    The key to bash (or any) scripting is to work on each piece, test, then add another feature.

    Anyway, I wrote this for you. You can see the changes you need to make

    Code:
    #!/bin/bash
    exec < listfile
    while read line
    do
    cp $line new.$line
    done

  4. #4
    Just Joined!
    Join Date
    May 2012
    Posts
    3
    Thanks for your response, this is the whole thing:-

    cd /xyz/abc

    FILE="/xyz/abc/test.txt"

    Target=aaabbb/test

    while read LINE
    do
    cp "$LINE" $Target ----please note the filename i am reading from test.txt is placed in the same folder.
    echo "file copied:"$LINE"">>/xyz/abc/log/FileCopyLog_`date +%d%m%Y_%H%M%S`.txt
    done < $FILE

    error :-

    cp: cannot stat 'abcd.pdf \r': No such file or directory

  5. #5
    Linux Engineer
    Join Date
    Apr 2012
    Location
    Virginia, USA
    Posts
    883
    Try removing the quotes around "$LINE"

  6. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by S123 View Post
    Thanks for your response, this is the whole thing:-

    cd /xyz/abc

    FILE="/xyz/abc/test.txt"

    Target=aaabbb/test

    while read LINE
    do
    cp "$LINE" $Target ----please note the filename i am reading from test.txt is placed in the same folder.
    echo "file copied:"$LINE"">>/xyz/abc/log/FileCopyLog_`date +%d%m%Y_%H%M%S`.txt
    done < $FILE

    error :-

    cp: cannot stat 'abcd.pdf \r': No such file or directory
    your file "test.txt" probably was created in Windows, yes? Anyway, it seems that there are carriage returns (\r) in it. remove them with:
    Code:
    dos2unix test.txt
    you many need to install dos2unix first, but it should be in your repos.

  7. #7
    Just Joined!
    Join Date
    May 2012
    Posts
    3
    Thanks for replying back atreyu and mizzle...it worked!!!!

    i recreated the file in linux, as atreyu mentioned it had carriage return and yes i created the initial file in windows.

Posting Permissions

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