Linux: copy folders only

I needed a simple script that would copy the entire folder structure of certain users (under Linux) to a backup destination. This is how I did it.
You would need a file call users.txt with the names of the users, divided by linebreaks.

users.txt:
user1
user2
user3

And this little script:

createfolders.sh:

#!/bin/sh
dest='/backup'
cat users.txt | while read user;
do
base="/users/$user"
find $base -type d -print | cpio -pvdum $dest
done

3 thoughts on “Linux: copy folders only

  1. It was the only option that successfully copied all folders, even with spaces or special characters in their name. Tried rsync and cc but it always bombed out.

  2. I see, and I can’t “nitpick” over anything since any other method I can think of involves a nested while loop. And, IIRC, “tar” syntax is more verbose than cpio’s… Hence: you win. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *