Solved how to cp -a /* /mnt/

balanga

Son of Beastie

Reaction score: 217
Messages: 4,024

I'm trying to copy all the local files to a remote partition - /mnt/ but when running cp -a /* /mnt/ I get recursive copying of /mnt/. Looking a cp() I though '-x' was my solution but it wasn't.

Any suggestions?
 

mark_j

Daemon

Reaction score: 578
Messages: 1,053

Or without package installation:

cd /; tar --exclude mnt -cvf - . | (cd /mnt; tar -xpf -)

or the more 'modern way':

cd /; tar --exclude mnt -cvf - . | tar -fpx - -C /mnt
 

memreflect

Active Member

Reaction score: 177
Messages: 209

You might also want to keep in mind that certain directories are special like /dev, /media, /net, and /tmp. For example, if you have an 8 GiB USB drive that was auto-mounted at /media/usb8G, you probably don't want to copy that directory at all, nevermind its contents!
 
OP
B

balanga

Son of Beastie

Reaction score: 217
Messages: 4,024

I thought there might be something like an -x (exclude) option but don't see one.
 

mark_j

Daemon

Reaction score: 578
Messages: 1,053

If you don't want to throw them all on the command line, use a text file and specify tar -X file to parse the file and skip the directories/files listed in it.

Or, if you're set on using cp etc. why not just a little shell script:

Code:
#!/bin/sh

exempt="mnt/ dev/ media/"

cd /
for dir in `ls -d */` ; do
        skip=0
        for skipdir in $exempt; do
                if [ $skipdir = $dir ]; then
                        skip=1
                        break
                fi
        done
        if [ $skip -eq 0 ]; then
                cp -a $dir /mnt/
        fi

done
 

Matlib

Member

Reaction score: 30
Messages: 27

With bash and extglob turned on it's cp -a !(mnt) mnt/. But as memreflect said, you probably want to exclude dev|tmp|proc too.
 
Top