我正在研究一个scp
调用以下载远程系统上存在的文件夹。下载的文件夹有子文件夹,在这些子文件夹中有一堆文件,我想将它们作为参数传递给 python 脚本,如下所示:
scp -r [email protected]:SomeName/SomeNameElse/$folder_name/ $folder_name/
echo "File downloaded successfully"
echo "Running BD scanner"
for d in $folder_name/*; do
if [[ -d $d ]]; then
echo "It is a directory"
elif [[ -f $d ]]; then
echo "It is a file"
echo "Running the scanner :"
python bd_scanner_new.py /home/nsadmin/Some/bash_script_run_files/$d
else
echo "$d is invalid file"
exit 1
fi
done
我添加了逻辑来查找是否有任何目录并排除它们。但是,我不会递归地遍历这些目录。
部分结果如下:
File downloaded succesfully
Running BD scanner
It is a directory
It is a directory
It is a directory
Exiting
我想改进这段代码,以便它遍历所有目录并获取所有文件。请帮助我提出任何建议。
您可以使用shopt -s globstar
在 Bash 4.0+ 中:
#!/bin/bash
shopt -s globstar nullglob
cd _your_base_dir
for file in **/*; do
# will loop for all the regular files across the entire tree
# files with white spaces or other special characters are gracefully handled
python bd_scanner_new.py "$file"
done
Bash 手册提到了这一点globstar
:
如果设置,文件名扩展上下文中使用的模式“**”将
匹配所有文件以及零个或多个目录和子目录。如果
该模式后跟“/”,仅目录和子目录
匹配。
More globstar
这里的讨论:https://unix.stackexchange.com/questions/117826/bash-globstar-matching
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)