繁体   English   中英

Shell,For循环到目录和子目录中无法正常工作

[英]Shell, For loop into directories and sub directory not working as intended

当试图做一个简单的脚本来压缩文件并将它们移动到另一个目录时,我在实现递归循环时遇到了问题。

#!/bin/bash
for directorio in $1; do #my thought process: start for loop goes into dir
    for file in $directorio; do #enter for loop for files
        fwe="${file%.*}" #file_without_extension    
        tar -czPf "$fwe.tar.bz2" "$(pwd)" && mv "$fwe.tar.bz2" /home/cunha/teste
    done
done

该脚本似乎无所作为...当脚本被调用为:./script.sh / home / blablabla

我该如何解决?

您可以更好地遵循以下选项。 如何运作? 首先,它将在short_list.txt列出所有目录。 在while循环中,它将读取每个目录名称并将其压缩在/home/cunha/teste

find $1 -type d -name "*" > short_list.txt
cdr=$(pwd)

while read line
do
   cd $line
   base=$(basename $PWD)
   cd ..
   tar -cf /home/cunha/teste/$base.tar.gz  $base
done < short_list.txt
cd $cdr
rm -rf short_list.txt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM