Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tar certain file types in all subdirectories?

Tags:

linux

tar

I want to tar and all .php and .html files in a directory and its subdirectories. If I use

tar -cf my_archive *

it tars all the files, which I don't want. If I use

tar -cf my_archive *.php *.html

it ignores subdirectories. How can I make it tar recursively but include only two types of files?

like image 526
user1566515 Avatar asked Sep 11 '13 02:09

user1566515


People also ask

How do I tar a specific file?

To compress a single file by running tar -zcvf file. tar. gz /path/to/filename command on Linux. Tar and compress multiple directories file by running tar -zcvf file.

Can you tar multiple files?

If your system uses GNU tar , you can use tar in conjunction with the gzip file compression utility to combine multiple files into a compressed archive file. Note: In the above examples, the -z option tells tar to use gzip to compress the archive as it is created. The file extensions .


2 Answers

find ./someDir -name "*.php" -o -name "*.html" | tar -cf my_archive -T -

like image 64
DeeDee Avatar answered Sep 21 '22 17:09

DeeDee


If you're using bash version > 4.0, you can exploit shopt -s globstar to make short work of this:

shopt -s globstar; tar -czvf deploy.tar.gz **/Alice*.yml **/Bob*.json 

this will add all .yml files that starts with Alice from any sub-directory and add all .json files that starts with Bob from any sub-directory.

like image 22
Sairam Krish Avatar answered Sep 24 '22 17:09

Sairam Krish