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
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 {} Π½ΡΠΆΠ½Π°Ρ/ΠΏΠ°ΠΏΠΊΠ° tar missed ... - Fat-Zerfind . -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 barakinObviously, 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 '{}' \; Source: https://ru.stackoverflow.com/questions/805728/
All Articles
$ for d in ...; do tar -czf $d.tar.gz $d; done$ for d in ...; do tar -czf $d.tar.gz $d; done- aleksandr barakin