How to Tar Certain File Types in All Subdirectories

How to tar certain file types in all subdirectories?

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

How to tar specific file extension files?


 find -name "*.h" -o -name "*.cpp" | xargs tar -czvf build.tar.gz

How do I tar a directory of files and folders without including the directory itself?


cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd - 

should do the job in one line. It works well for hidden files as well. "*" doesn't expand hidden files by path name expansion at least in bash. Below is my experiment:

$ mkdir my_directory
$ touch my_directory/file1
$ touch my_directory/file2
$ touch my_directory/.hiddenfile1
$ touch my_directory/.hiddenfile2
$ cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd ..
./
./file1
./file2
./.hiddenfile1
./.hiddenfile2
$ tar ztf my_dir.tgz
./
./file1
./file2
./.hiddenfile1
./.hiddenfile2

tar folder and exclude all subfolders, then tar to specific path

first, use find to find the files meeting your criteria:

find ~/Desktop -type f -maxdepth 1

then pipe it to tar, using -T ( or --files-from) to tell tar to get the list of files from stdin:

 find ~/Desktop -type f -maxdepth 1 | \
tar -T - cvf r.tar

Shell command to tar directory excluding certain files/folders

You can have multiple exclude options for tar so

$ tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz .

etc will work. Make sure to put --exclude before the source and destination items.

How to create tar file with only certain extensions but omitting server generated files with similar extension?


tar -tf file.tar --wildcards '*.jpg' --exclude '*.*.jpg'

Output:

filetwo.jpg
imagethree.jpg
original.jpg

Just change -t to -x to extract instead.

To create the archive:

tar -cf file.tar *.jpg --wildcards --exclude '*.*.jpg'


Related Topics



Leave a reply



Submit