Making a .tgz of multiple types of files in a directory

Took me a few attempts to figure out how to get this to work, so am making a note for next time:

Use -o to do an OR with the find command. Use -print0 to replace newlines in the output with the null character and use the --null argument to tar so it recognises null-terminated filenames (otherwise whitespace in filenames can cause problems).


find . -print0 -name '*.mat' -o -name '*.set' -o -name '*.fdt' | xargs tar --null -cvzf archive.tgz

Advertisements

2 responses to “Making a .tgz of multiple types of files in a directory

  1. Is there some reason why you can’t just do this:

    tar -cvzf archive.tgz *.mat *.set *.fdt

    I’d think bash would do autocompletion of them all and escape the whitespace correctly. (Or do you have subdirectories that you need to pull from?)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s