linux cp command script into one file

Craig Lambie
Craig Lambie used Ask the Experts™
Hi Experts,

I am a bit of Newb when it comes to Linux, so forgive me for a stupid question.

I have a NetGear ReadyNAS running Linux.
I have plugged in a USB drive to it, and am trying to copy some files across to the USB using a script.
I am using Putty to access the NAS from my Windows Vista laptop.

I wrote the script and saved it as a .sh file (attached) in Notepadd++
I have tried a few different combinations of the parameters, and also to add an extra "/" to the end of the destination directory (as you see it now).

I am not getting anywhere with the copying of these files, would really appreciate your help.

The Output from the Putty Terminal:
NASShared:/media# ./
`/media/Battlestar Galactica/Battlestar Galactica 1x05 - You Can\'t Go Home Again.avi' -> `/USB_HDD_6/TV Shows/\r'
`/media/Battlestar Galactica/Battlestar Galactica 1x06 - Litmus.avi' -> `/USB_HDD_6/TV Shows/\r'
NASShared:/media# cd /USB_HDD_6
NASShared:/USB_HDD_6# ls
MG45_1.5.2_ALTECH_ENG_20080428_1506.upgrade  Movies  $RECYCLE.BIN  System Volume Information  TV Shows  VProRecovery
NASShared:/USB_HDD_6# cd TV\ Shows/
NASShared:/USB_HDD_6/TV Shows# ls
?                Carnivale  Entourage    Heros     Man Stroke Woman  Prison Break    Rome  The Big Bang Theory         Torchwood
Californication  Dexter     Gossip Girl  House MD  NCIS              Pushing Daises  SATC  The.IT.Crowd Season 1and 2  Underbelly - Complete
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

cp -r copies a whole directory recursively, ie. you don't have to specify each file.

For example the following command

cp -rv /media/movies/ripped/from/dvds/i/bought /usb/movies

would copy all files under the path /media/movies/ripped/from/dvds/i/bought onto the filesystem mounted at /usb/movies.
Deepak KosarajuDevOps Engineer

You can debug shell script by using -x option.
Try to run the script as:
sh -x <script name>
Ex:sh -x
Monis MontherSystem Architect

Actually you dont need this script, all you need is a one line command

cp -rv /path/of/source /path/of/destination

This will copy all files dirs and subdirs to your destination

A better approach would be rsync because it only copies new files, for example next time you copy it will only take the new things without copying the old stuff again

rsync -rvP /path/of/source /path/of/destination

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!


1. didn't you get any error messgaes?
2. what are the file access permissions? and what is your user parmissions?
3. what is the usb mount permission? ( send the mount command output )
    if it is mounted as a readonly you will have to change it. mount -remount rw /USB_HDD_6
    and then run the script


Thanks for all your comments.
I am using a script as I have a large library that won't all fit on my USB_HDD so I use Excel to select the files I want, output to a txt file, and then converted to a .sh script file in Notepad++

I used -r as an option as an experiment, as I was getting the below problem without it.

I will try running it with the -x parameter, but as I was running the script with Verbose on, it didn't appear to have any errors, in fact it appears to copy them all across with no issues.
However.. the result is a a file on my USB_HDD_6 "TV Shows?" instead of the individual files... it took 12 hours to run through the script, with no result... not happy.

As above, I only want selected files.

1. didn't you get any error messgaes?
2. what are the file access permissions? and what is your user parmissions?
I am logged in as Root, and have full permissions to rw the files and dir's
3. what is the usb mount permission? ( send the mount command output )
    if it is mounted as a readonly you will have to change it. mount -remount rw /USB_HDD_6
    and then run the script
I have full rw permissions on the USB mount, I don't have to use the mount command on the USB as it does it automatically on plugging it in.  The fact that it is copying across to it without an error suggests the permissions is not the issue, it is something to do with the cp command I think.

1. the attached file is in DOS format there for you have hidden \r and \n rather then just \r - the cp "sees" the last \r as a file name, that is why you have the ? as afile name - it a palceholder for a not printable letter.
2. use dos2unix to conver the file first (i've done that for you so you can check it - see attached file) or tr -d '\r' < dos_file > unix_file
3. i guess the notepad++ didn't converted for you as you assumed.
4. i suggest testing your script with dummy files rather then real ones ~12 hours - that is painful
5. why not to leave the list as a csv file (access common output format) and build a generic script on the remote machine to get the csv file as input. translate the dos2unix and replace non printable chars if exists


Damn Notepad++ would of thought save as .sh would of worked.
The other option you spoke of sounds interesting, might learn how to do that another time.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial