The Code
find . -name "2013-*" -type d | xargs tar cvpzf "2013 logs.tar.gz"
Explanation
We use the find command with “.” as the current directory we’re in, so I used “cd” – change directory to where I wanted to zip up files. The -name parameter is then finding any thing starting with “2013-“ followed by anything else via the “*” wildcard. The “-type d” restricts the search to only directories. We then use the pipe | over to xargs and run our tar command with the options c-create, v-verbose (See the process as it happens), p-preserve-permissions (great for taking backups if you ever have to restore) , z-gzip the more zipping the merrier, f-file allows you to specify the output file name.
References
- http://linuxcommand.org/man_pages/tar1.html
- http://content.hccfl.edu/pollock/unix/findcmd.htm
- http://ubuntuforums.org/showthread.php?t=782714
- Old code I’ve been using to backup our website. Hopefully should cover this in a later article
One Trackback
[…] « Linux Tar, Zip, Gzip Matching Files In Current Directory […]