我正在使用 scp
调用来下载远程系统上存在的文件夹。下载的文件夹有子文件夹,在这些子文件夹中有一堆文件,我想将它们作为参数传递给 python 脚本,如下所示:
scp -r researcher@192.168.150.4: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
我想改进这段代码,让它遍历所有目录并获取所有文件。请帮助我提出任何建议。
最佳答案
您可以在 Bash 4.0+ 中使用 shopt -s globstar
:
#!/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
是这样说的:
If set, the pattern ‘**’ used in a filename expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a ‘/’, only directories and subdirectories match.
更多 globstar
讨论在这里:https://unix.stackexchange.com/questions/117826/bash-globstar-matching
关于bash - 如何递归遍历目录树并仅查找文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41799938/