我想获取目录的名称来为每个目录创建一个 tar.gz 文件
例如,所有以 A 开头的目录:
count = 10
for ((k = 1 ; k < $count ; k++));
do
tar -cf ADIRNAME.tar.gz ADIRNAME
sleep 1
done
我怎样才能做到这一点?
这将为每个A*
目录创建一个 tarball。
for dir in A*/; do
tar -cf "${dir%/}".tar.gz "$dir"
done
通配符扩展为要循环的目录名称列表。将匹配项设置为目录的尾部斜杠将包含在扩展中;我们使用简单的变量替换从 tarball 的文件名中修剪它(${variable%suffix}
扩展为变量的值,suffix
如果存在,则从末尾修剪;还有相应${variable#prefix}
的和许多其他替换;请参阅 shell 的手册页。)
双引号是强制性的,尽管只要没有包含空格的文件名,脚本就可以在没有它们的情况下工作。即使在许多 shell 脚本教程中,这也是一个常见的疏忽。
it's not exactly clear what you need to do, but here are some bits to build upon:
find all directories:
find -maxdepth 1 -type d
find all directories starting with a certain letter:
find -maxdepth 1 -type d -name 'X*'
now you can get the list of the first letters:
for n in `find -maxdepth 1 -type d`; do echo ${n:2:1}; done | sort -u
and finally, perform tar'ing with those letters.