For example, we have in the current folder directory 001,002,003 .

At the output, you must have three archives 001.tar.gz, 002.tar.gz, 003.tar.gz

  • $ for d in ...; do tar -czf $d.tar.gz $d; done $ for d in ...; do tar -czf $d.tar.gz $d; done - aleksandr barakin

2 answers 2

may not be the most elegant, but well-working solution:

 find . -type d -maxdepth 1 -mindepth 1| xargs -i tar -cvzf {}.tar.gz {} 

I explain what is happening:

1) find. -type d -maxdepth 1 -mindepth 1

find - a utility for searching and crawling files and directories

. - current directory

-type d - key to bypass only directories in the current directory

-maxdepth 1 -mindepth 1 - depth of the detour then after that on stdout we give the addresses of the directories that need to be archived

2) | xargs -i tar -cvzf {}.tar.gz {} | xargs -i tar -cvzf {}.tar.gz {}

| - conveyor. transfers the output of the command line to the next command.

xargs is a utility that helps generate an argument list.

-i - the key that says that we will substitute something

tar -cvzf {} .tar.gz {} is a command for archiving a directory by gz (instead of {} is the name of the directory formed by the find utility).

- {}. tar.gz - archive name (directory + "tar.gz") second {} - that is the directory for archiving

after that, in the current directory, you will have the necessary archives for you, which you can easily move to the directory you need with the command:

 find . -type f| grep tar.gz | xargs -i mv {} нуТная/ΠΏΠ°ΠΏΠΊΠ° 
  • Sorry for the design. I won't do any better - Andrey Yurevich
  • one
    tar missed ... - Fat-Zer
  • too many mistakes. Please check the proposed commands before posting. - aleksandr barakin
  • @alexanderbarakin where are the errors? the script works the same - Andrey Yurevich
  • 2
    1. I do not see the script in your answer, I see the command. 2. yes, the command is launched, yes, it does not hang, yes, it terminates, yes, it displays an error message in stderr . 3. yes, if you correct the most flagrant error, then even files with archives are created, however, one more than necessary. 4. if all of this is the purpose of your answer, then yes, you don’t need to change anything. // ps find . -type f| grep tar.gz find . -type f| grep tar.gz find . -type f| grep tar.gz - I recommend reading $ man find for the option -name - aleksandr barakin

Obviously, it is necessary to β€œgo through” in a loop through all directory names, and for each of them call the tar program with the appropriate arguments.

  • cycle. something like:

     $ for n in *; do test -d $n && ΠΊΠΎΠΌΠ°Π½Π΄Π°; done 

    here test -d имя - returns true if the имя is a directory.

  • the corresponding arguments are something like:

     tar -czf имя.tgz имя 

total:

 $ for n in *; do test -d $n && tar -czf $n.tgz $n; done 

Possible disadvantage: under the mask * (by default) files / directories whose names begin with the symbol will not fall . (point).


An alternative method of looping can be a call to the find program, or in conjunction with the xargs program:

 $ find -mindepth 1 -maxdepth 1 -type d | xargs -I '{}' tar -czf '{}'.tgz '{}' 

or by placing the necessary command in the "body" of the -exec option:

 $ find -mindepth 1 -maxdepth 1 -type d -exec tar -czf '{}'.tgz '{}' \; 
  • Mask * - evil, stop using it already in scripts. Especially not an alternative, but the only true path through find is mentioned. - 0andriy