how do I copy Doc files to thumb drive?


Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: how do I copy Doc files to thumb drive?

  1. #1
    Join Date
    Mar 2008
    Posts
    22

    how do I copy Doc files to thumb drive?

    Good evening,
    How would I use the command line to copy file#1 to a usb thumb drive?
    Ive tried " $ cp ~/Documents/file#1 /media/USB_DISK " to no avail. I can drag it in ,but can't seem to use the command line. Thanks..Rich

  2. #2
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,936
    Um, maybe it would be a good idea to post the error you get when you try that command?

    It may be because the shell treats # characters as the start of a comment, so it might be passing incorrect arguments to cp -- if this is the problem (and we can only know if we see the error message you're getting), then the fix would be to escape the # character. (There are several ways to do this.)

    Or, it may be that your file manager isn't telling you the whole story about the filename: if it's leaving bits of the name off, you will still have to provide them to cp.

    Or, it may be that you don't have permission to write to the target location.

    The error should tell us which of these is the issue (or if the issue is something else).

  3. #3
    Join Date
    Mar 2008
    Posts
    22
    This is what I did, and its results:

    kaykav@dittydiner2:~$ cp /home/kaykav/Documents/save\ this /media/USB_DISK
    cp: cannot create regular file `/media/USB_DISK': Permission denied
    kaykav@dittydiner2:~$
    Im trying to copy the file 'save this' to my thumb drive. In the previous message I was using 'file#1' as an example. sorry..Rich

  4. #4
    Join Date
    Jan 2007
    Posts
    280
    Try this (I'm assuming your file, save this, is located at /home/kaykav/Documents)

    Code:
    $ cp /home/kaykav/Documents/save\ this /media/USB_DISK/
    Note the trailing slash in the command-line. This tells the shell that you wish to place save this within /media/USB_DISK. Without the trailing slash the command would signify that you wish to overwrite /media/USB_DISK (a folder) with save this (a regular file).

    Linux won't allow you to overwrite an existing folder with a regular file. Hence, the error message: cp: cannot create regular file `/media/USB_DISK': Permission denied

    On certain distros (mostly ones based on Debian), you might need to prepend your command with sudo.

    --------------------------------------------------


    If you plan to work extensively on the shell, avoid using spaces and special characters while naming files.

    The trailing slash theory mentioned above might not be completely correct. For instance, the copy operation works fine on my Kubuntu 8.04 box, with, or without the trailing slash. Just try it out, and inform us if it worked.
    Last edited by i845_; 10-25-2008 at 03:55 AM.

  5. #5
    Join Date
    Mar 2008
    Posts
    22

    copy file to flash drive

    i845,
    kaykav@dittydiner2:~$ cp /home/kaykav/Documents/save\ this /media/USB_DISK/
    cp: cannot create regular file `/media/USB_DISK/': Is a directory
    kaykav@dittydiner2:~$

    I tried your suggestion, same thing. I wonder what it means by 'cannot create regular file' ? I can drag it into the icon, that works,or I can copy the file to the Desktop that works too. This media thing always seems to annoy me. Im going to do a complete research on this 'mounting-media-copying conundrum. Thank you for your interest..appreciated....Rich

  6. #6
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,936
    You do realize that your distro is (apparently) creating and removing the /media/USB_DISK directory from time to time? Here's how cp works when deciding what to name the copy of the file, and where to put it:

    1) If the target name exists, then:
    1a) If the target name is a file, then it gets overwritten with the copy.
    1b) If the target name is a directory, then the copy gets put into that directory, with the same filename as it had in its old location. (This may involve overwriting an existing file as well.)
    2) If the target name does not exist, then cp creates the target name (as a file), and puts the copy in that file.

    The first time you ran that command, it tried to create USB_DISK as a file, but couldn't (because you don't have write permission to /media -- but this is correct; you shouldn't). USB_DISK didn't exist at that point. But the second time you ran that command, it failed because the directory... wait, what the heck? Your cp isn't following the rules that I've seen cp follow ever since I've used it! USB_DISK existed as a directory, but it tried to create it as a file?

    I suppose there are two possible reasons for this. First, if it didn't exist when you first ran cp, it saw that it didn't exist, but it did get created before cp tried to do the copy, so the copy failed. (I.e. a race condition.) The other possible reason is that cp might have flags to modify this behavior, and your distro may have set up an alias to cp that sets some of these flags.

    So, do an alias | grep cp and see if it says anything. If not, then there isn't any alias and this isn't the problem. If it does, then see if /bin/cp shows up in the list as well, and if not, try the command again but with /bin/cp instead of cp. (This will skip all the aliased options.) If that works, then complain to your distro that they've set stupid options in their cp alias. (Also, it may work to set the filename after /media/USB_DISK/ yourself. But this is a lot more typing, especially when the file doesn't exist yet, so you can't use tab completion.)

    If there is no alias, then I would suspect that the issue might be the race condition above. If your disk is constantly disappearing and reappearing (e.g. because of a really crappy USB connection that keeps getting broken, or because it's a real disk with platters and is trying to suck too much power off the USB hub, which means it needs both of its USB cables plugged in, not just one), then this might cause strangeness like you're seeing. The way to find out is to check your kernel logs: if you're seeing "usb disconnect" messages when you aren't unplugging anything, then this is probably at least part of the issue. (To find out, find the kernel log file in /var/log, and tail -f it. Then watch to see if you get any new messages related to USB while you aren't doing anything to the hardware. Then verify that you do see new messages when you unplug and re-plug the device.)

  7. #7
    Join Date
    Jan 2007
    Posts
    280
    Quote Originally Posted by bwkaz
    If your disk is constantly disappearing and reappearing...
    Happens very often with crappy extension cables.

    Out of sheer curiosity kaykav, which distro are you currently using...?

  8. #8
    Join Date
    Mar 2008
    Posts
    22

    copy file to /media/USB_DISK

    i386,
    Im running 8.04(hardy heron)

  9. #9
    Join Date
    Jan 2007
    Posts
    280
    Did you try running the command with sudo prepended to it...? (unnecessary, but still worth a try...)

    Quote Originally Posted by kaykav
    i386,...
    /rotfl...

  10. #10
    Join Date
    Mar 2008
    Posts
    22

    copy to flash drive

    Good morning,
    Well people I got it to invoke my command. I m running ubuntu 8.04
    (hardy heron).
    The problem was my syntax. When I type : cp save\ this /media/USB_DISK
    I got the error message 'cannot create regular file', and consequently couldn't copy
    the file. But when I type: cp save\ this /media/USB\ DISK, it copied.!
    Note the 'back slash' after USB . If I put the device name in quotes, single or
    double, it also works. Its when I use the under_score character ,that it does not
    work. Im not sure why. Anybody?..Thank you all for your knowledgable help....
    Rich

  11. #11
    Join Date
    Jan 2007
    Posts
    280
    Phew!

    Actually your flash drive is getting mounted to /media/USB DISK. There is a space between the words USB and DISK in the name of the folder.

    Take a look at your first post:

    Quote Originally Posted by kaykav
    Ive tried " $ cp ~/Documents/file#1 /media/USB_DISK " to no avail.
    This led us to believe that your drive was getting mounted to /media/USB_DISK when it was actually getting mounted to /media/USB DISK.
    Our solutions were based on the above assumption.

    Quote Originally Posted by kaykav
    Note the 'back slash' after USB . If I put the device name in quotes, single or double, it also works.
    That's because they serve to escape the space character present between the words USB and DISK in /media/USB DISK (the same also holds true for save and this in save this). You'll find that in one of his previous posts, bwkaz talks about escaping the # character. The same thing is at play here.

    Quote Originally Posted by kaykav
    Im not sure why...
    Read on.
    Last edited by i845_; 10-27-2008 at 05:58 PM.

  12. #12
    Join Date
    Jan 2007
    Posts
    280
    Special characters

    There exist a number of characters that are treated by the shell in a special manner. These are known as special characters or meta-characters. The purpose of their existence is to make life simpler for the user. Let's consider the asterisk (*) as an example.

    In a command-line, the asterisk acts as a wild card. I'll illustrate it using an example. Consider a folder containing the following files:

    Code:
    kaykav01
    kaykav02
    kav08
    kaykav96
    kaysbirthday
    jayhawk
    thesis
    todo
    Suppose you need to copy all the files beginning with kay to a folder named Work. How would you do it? Let me guess:

    Code:
    $ cp kaykav01 Work/
    $ cp kaykav02 Work/
    $ cp kaykav96 Work/
    $ cp kaysbirthday Work/
    That approach gets the job done all right, but it's a pain in the you-know-where to issue a command for every single file. Besides, it becomes completely unfeasible when you're dealing with large numbers of files. Fortunately, the shell provides us with an easier way out:

    Code:
    $ cp kay* Work/
    The asterisk gets special treatment from the shell. Within a command-line, it expands to match any character or group of characters.

    Similarly, if you were to change to a directory and issue:

    Code:
    $ rm *
    Poof! In one fell swoop, you would've deleted all the files contained within the directory. Again, the asterisk is at work: since it expands to match any character or group of characters, rm * is basically commanding the shell to delete everything!

    Warning: Don't fool around with the command mentioned above. You might end up hosing your system.
    Last edited by i845_; 03-24-2009 at 03:30 PM.

  13. #13
    Join Date
    Jan 2007
    Posts
    280
    Quoting, and the case at hand

    Let's get back to our first example. Now, suppose you have a folder containing the files:

    Code:
    kaykav01
    kaykav02
    kaykav96
    kay*
    kaysbirthday
    How do you delete just the file named kay*?

    Issuing

    Code:
    $ rm kay*
    would have disastrous consequences: it would delete every file that starts with kay. That's not what we want. But then, how do you make the shell treat * like a literal *, and not like a wildcard?

    Note: It is to prevent situations like this that I mentioned in one of my posts: if you plan to work extensively on the shell, avoid using spaces and special characters while naming files.

    Here's where quoting comes in. Quoting is a feature provided by the shell, whereby, you can cause meta-characters to lose their special meaning in a command-line. There are many ways to do so. One of these ways is to prepend the offending character with a backslash (\). In this case, the command becomes:

    Code:
    $ rm kay\*
    The backslash, when used within a command-line, causes the shell to ignore the special meaning of the (meta-)character just following it (and of that (meta-)character alone). To cause the shell to ignore the special meaning of a (meta-)character within a command-line, is called escaping that (meta-)character.

    Another method is to enclose the entire argument(s) in single quotes

    Code:
    $ rm 'kay*'
    Note: Double quotes (") can also be used, but there are certain subtle differences; these are not discussed here.

    Fix: Back quotes (`) are used for command substitution, and not for quoting. Thanks bwkaz for pointing that out!

    -----------------------------------------------------------------------------------------------

    The space is also an example of a special character. The basic structure of a Linux command is:

    command [option(s)...] [argument(s)...]

    e.g.

    Code:
    $ cal 03 2006
    $ ls -l Work
    $ ls -al Work Play
    In each of these command-lines, the commands, options and arguments are separated from each other by spaces. Thus, spaces serve the purpose of separators within a command-line. Now, consider this:

    Code:
    $ cp save this /media/USB DISK
    The shell treats each of save, this, /media/USB and DISK as a separate argument, when in reality, save this and /media/USB DISK are distinct arguments of their own. This results in erratic behaviour. To prevent this, one can use:

    Code:
    $ cp save\ this /media/USB\ DISK
    or

    Code:
    $ cp 'save this' '/media/USB DISK'
    Hope this helps.
    Last edited by i845_; 10-28-2008 at 08:30 AM.

  14. #14
    Join Date
    Mar 2008
    Posts
    22

    copy file to flash drive

    i845,
    Well taught. I was under the impression that an under_slash was as good

    as a back\slash. that was my continuing mistake. Thanks for your input .
    Rich

  15. #15
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,936
    Minor nitpick:

    Quote Originally Posted by i845_
    Note: Double quotes (") and back quotes (`) can also be used, but there are certain subtle differences; these are not discussed here.
    There are subtle differences between double quotes and single quotes, yes.

    However, there are huge differences between (double or single) quotes and backquotes. Never ever use backquotes to try to escape anything.

    (What will probably happen is that you'll get some form of syntax error. The shell takes text between backquotes and tries to run it on its own as a command, then takes that command's output and substitutes it in place of the backquotes. This is nothing at all close to escaping anything. )

Posting Permissions

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