Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 need a script (may it be bash, ksh, or whatever) which copies, for backup purposes, all the sub-directories and files of a directory: .znc - but .log files.

The structure is like this:


Note that this is only part of the structure, but this is the part I am having trouble with.

I need to copy all the content of .znc/ but files which extension is "log". I haven't been able to do much with find or grep, because there is a variable, username.

share|improve this question
Why not grep for .log? It might have issues if that is part of a filename, but that should not generally be the case. – soandos Jun 13 '12 at 18:19
For backups, you can use rsnapshot which is built around rsync – user49740 Jun 13 '12 at 18:22
up vote 1 down vote accepted

You can use rsync. It also has the nice feature that it preserves owner/group/perms etc

rsync -avi --exclude '*.log' .znc/ /path/to/destination/directory/
share|improve this answer
thanks, that made the job! "You can accept an answer in 3 minutes" – user139944 Jun 13 '12 at 18:23
You're welcome. You should also look at rsnapshot, which uses rsync and which is very useful for backups. It's also easy to set up – user49740 Jun 13 '12 at 18:25
In this case I use dropbox for backups, since it's a very little amount of data and I don't need frequent backups because of the nature of the program, it's useful to have around different servers live. I will look into rsnapshot, thank you! – user139944 Jun 13 '12 at 18:29

Off the top of my head:

cd /to/top/of/dir/structure
tar -cvf /path/to/tarfile.tar $(find . -type f ! -name '*.log')

This won't work if files have spaces though.

This may work if the filenames have spaces

cd /to/top/of/dir/structure
tar -cf /path/to/tarfile.tar --files-from /dev/null # trick to create empty tar file
find . -type f ! -name '*.log' -print0 | xargs -0 tar -uvf /path/to/tarfile.tar
share|improve this answer
Isn't there a command line length limit? If the number of files is very large, I think it might exceed that limit – user49740 Jun 13 '12 at 18:29
@user49740: xargs takes care of that – it will split up the argument list to multiple tar -uvf invocations. (The maximum argument length is 32 pages on recent Linux kernels.) – grawity Jun 13 '12 at 18:41
Correction: s/argument length/argument+environment length/ and s/32 pages/¼ of the maximum stack size (ulimit -s)/ – my system shows ARG_MAX as 2 MB with the default ulimits. – grawity Jun 13 '12 at 18:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.