skip to Main Content

I have a list of files to copy from smb server to my centos7 hard drive from csv

missing.csv

Filepath
./2019_06_27_094411_Season_5/Locked_Approved/Master_JPG/501_00001.jpg
./2019_06_27_094411_Season_5/Locked_Approved/Master_JPG/501_00002.jpg
./2019_06_27_094411_Season_5/Locked_Approved/Master_JPG/501_00003.jpg
./2019_06_27_094411_Season_5/Locked_Approved/Master_JPG/501_00004.jpg
./2019_06_27_094411_Season_5/Locked_Approved/Master_JPG/501_00005.jpg
./2019_06_27_094411_Season_5/Locked_Approved/Master_JPG/501_00006.jpg
./2019_06_27_094411_Season_5/Locked_Approved/Master_JPG/501_00007.jpg

Now i need to copy the files from the csv list (i.e)filepath column and past it in my local drive with the same directory structure.

I have tried the following script and able to copy only the files, i need the file same as source directory structure.
– I have installed and mounted the smbclient in my centos machine

script.sh

#!/bin/bash

while read path; do
  cp -v "$path" "$1"
done

CMD: ./script.sh /home/test1 < missing.csv

2

Answers


  1. For simple cases (no spaces/special characters in file names, …), possible to combine the ‘–parents’ (sugested by cyrus above) with the full list of files. Potentially using xargs to support large number of files

    cp --parents -t "$path" $(cat)
    # Using xargs to allow larger number of files
    xargs cp --parents -t "$path"
    
    Login or Signup to reply.
  2. Even if missing.csv contains spaces, this does well.

    script.sh

    #!/bin/bash
    dest="${1}"
    while read l
    do
        f="${dest}/${l}"
        d=$(dirname "${f}")
        mkdir -p "${d}"
        cp "${l}" "${f}"                                                            
    done
    
    $ cat missing.csv 
    ./2019 06 27 094411 Season 5/Locked Approved/Master JPG/501 00001.jpg
    ./2019 06 27 094411 Season 5/Locked Approved/Master JPG/501 00002.jpg
    ./2019 06 27 094411 Season 5/Locked Approved/Master JPG/501 00003.jpg
    ./2019 06 27 094411 Season 5/Locked Approved/Master JPG/501 00004.jpg
    ./2019 06 27 094411 Season 5/Locked Approved/Master JPG/501 00005.jpg
    ./2019 06 27 094411 Season 5/Locked Approved/Master JPG/501 00006.jpg
    ./2019 06 27 094411 Season 5/Locked Approved/Master JPG/501 00007.jpg
    
    $ ./script.sh ./dest < missing.csv
    
    $ find dest -type f
    dest/2019 06 27 094411 Season 5/Locked Approved/Master JPG/501 00007.jpg
    dest/2019 06 27 094411 Season 5/Locked Approved/Master JPG/501 00006.jpg
    dest/2019 06 27 094411 Season 5/Locked Approved/Master JPG/501 00004.jpg
    dest/2019 06 27 094411 Season 5/Locked Approved/Master JPG/501 00005.jpg
    dest/2019 06 27 094411 Season 5/Locked Approved/Master JPG/501 00001.jpg
    dest/2019 06 27 094411 Season 5/Locked Approved/Master JPG/501 00002.jpg
    dest/2019 06 27 094411 Season 5/Locked Approved/Master JPG/501 00003.jpg
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search