ca9163d9
ca9163d9

Reputation: 29159

Find files in some directories with particular name pattern and tar them?

There are many subdirectories in a directory. For example,

/tmp/dir/00/
/tmp/dir/01/
....
/tmp/dir/99/

How to tar all the subdirectories which name is less than 50?

find /tmp/dir/ -name "???" -exec tar -rzf file1.tgz {} \;
tar -czf file1.tgz ???

Upvotes: 1

Views: 519

Answers (2)

anubhava
anubhava

Reputation: 784918

Since all the directories are just one level down from dir/ you can do this in bash:

cd dir/
for d in [0-9][0-9]; do
   ((10#$d < 50)) && echo "$d"
done | tar czf file1.tgz -T -

Details:

  • for d in [0-9][0-9]: Will match entries with 2 digits in current directoery
  • ((10#$d < 50)) will compare each (base 10) directory name to 50 and will return success of number is less than 50
  • tar czf file1.tgz -T - will read directory names from stdin and create a single gzip tar file file1.tgz.

Upvotes: 1

Neil M
Neil M

Reputation: 81

Howdi :)

This is how i would do it, given the info you supplied

Create a list of your directory paths, all 51 of them

echo > dirlist.txt ; 
for x in $(seq -w 00 50)
do
echo "/tmp/$x" >> dirlist.txt
done

Then use the tar command with the switch -T to load a list from a file:

tar -czf myarchive.tar.gz -T dirlist.txt

Test your archive , check first 3 lines:

tar -tvzf myarchive.tar.gz | head -3
drwxr-xr-x  0 neil   wheel       0 18 Jun 19:36 tmp/00/
drwxr-xr-x  0 neil   wheel       0 18 Jun 19:36 tmp/01/
drwxr-xr-x  0 neil   wheel       0 18 Jun 19:36 tmp/02/

Upvotes: 1

Related Questions