Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to write a shell script in which I recursively copy a directory to a USB stick. I need the return value of the copy operation for error-checking purposes. I've tried

cp -a /var/mydir /media/usbdrive


cp -r /var/mydir /media/usbdrive

as well as a few others, but the problem is that I always get errors such as:

Cannot create fifo: /path/to/fifo Operation not permitted

Whilst these are warnings and the copy operation continues, I get a non-successful error code returned from the operation which messes up the error handling in my script. How can I copy recursively and ignore fifos, symlinks, and other entities that are not valid on a FAT32 file system?

share|improve this question
up vote 2 down vote accepted

You should be able to do this using the find command. The -type option allows you to restrict the kinds of files you find, so you can ignore symlinks and pipes etc., and the -exec option allows you to run a command for each of the files you find.

First create all the directories on the USB stick:

cd /var/mydir
find * -type d -exec mkdir /media/usbdrive/{} \;

Then copy all the plain files:

cd /var/mydir
find . -type f -exec cp {} /media/usbdrive/{} \;
share|improve this answer
Worked a treat, thanks. – user17867 Nov 13 '09 at 12:15
Also works well on Kingston USB stick in this location + usefull to batch test these damned small storage devices with default FAT 32 FS, on Linux. Thank you @Dave Webb – lliseil Sep 5 '15 at 19:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .