I recently had the need to copy a directory to another portion of the file system, but wanted to skip some sub-directories. Normally what one would do is just copy the whole directory and delete the sub-directories that you don’t want after the copy operation has completed (and not make a big deal about it). However that wasn’t feasible this time, because each of these sub-directories had GBs of data and I did not have enough disk space to accomodate those extra files. I went through the
cp man page and did a few google searches but did not find anything that would work for me. However I realized that I had just used the “exclude” feature of tar a few days back which lets you skip portions of a directory while tarring and I knew tar could be used to copy over files as well (tar pipe). So thats what I did.
Lets say you have a directory structure like the one below. Main directory test1 with few files and few sub-directories and the sub-directories also have one file each.
@tmp $ ls test1 -R test1: a a.html b b.html c c.html d test1/a: 1.html test1/b: 2.html test1/c: 3.html test1/d: 4.html
Now you want everything to be copied over to a
test2 directory except for the sub-directories
So first go ahead and create your
Now the commands to copy over the required files would be
cd /tmp/test1 tar -Sc . --exclude a --exclude b | tar -C /tmp/test2 -xv
This will tar up your directory while excluding the two subdirectories
b and pipe it to the second tar command while will untar it into the desired directory
After the copy is done, the directory tree of
@tmp $ ls -R test2/ test2/: a.html b.html c.html d test2/d: 4.html
So we can see that the directories
b haven’t been copied over.
It sounds like a very simple problem, and cp should ideally have the
--exclude feature that tar does.