Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recursive find some 'foo' and tar

Tags:

bash

tar

Consider the following folder structure

 root/dirA/the_folder
 root/dirA/dir2/the_folder
 root/dirB/the_folder
 root/dirB/dir2/the_folder

I want to recursively find and tar the dirA/the_folder and dirB/the_folder. However when I use

find root/ -name 'the_folder' -type d | xargs tar cvf myTar.tar

It will pack all folders (containing dir2/the_folder) and I don't want that. What is the solution?

like image 369
mahmood Avatar asked Jul 03 '26 13:07

mahmood


2 Answers

In your case, wouldn't just this be enough?

tar cfv mytar.tar root/*/the_folder/
like image 152
gniourf_gniourf Avatar answered Jul 05 '26 03:07

gniourf_gniourf


Use the -maxdepth option of find to limit the recursion depth:

find root/ -maxdepth 2 -name 'the_folder' -type d

Try man find for lots of useful options that find offers. You will be surprised. For example, you can do away with the | xargs by using find's -exec option:

find root/ -maxdepth 2 -name 'the_folder' -type d -exec tar cvf myTar.tar {} +
like image 32
DevSolar Avatar answered Jul 05 '26 02:07

DevSolar