我如何编写一个bash脚本,遍历parent_directory内的每个目录,并在每个目录中执行命令。
目录结构如下:
Parent_directory(名称可以是任何东西-不遵循模式) 001(目录名称遵循此模式) 0001.txt(文件名遵循此模式) 0002.三种 0003.三种 002 0001.三种 0002.三种 0003.三种 0004.三种 003 0001.三种 目录数量未知。
我如何编写一个bash脚本,遍历parent_directory内的每个目录,并在每个目录中执行命令。
目录结构如下:
Parent_directory(名称可以是任何东西-不遵循模式) 001(目录名称遵循此模式) 0001.txt(文件名遵循此模式) 0002.三种 0003.三种 002 0001.三种 0002.三种 0003.三种 0004.三种 003 0001.三种 目录数量未知。
当前回答
for p in [0-9][0-9][0-9];do
(
cd $p
for f in [0-9][0-9][0-9][0-9]*.txt;do
ls $f; # Your operands
done
)
done
其他回答
for p in [0-9][0-9][0-9];do
(
cd $p
for f in [0-9][0-9][0-9][0-9]*.txt;do
ls $f; # Your operands
done
)
done
当当前目录为parent_directory时,可以执行以下操作:
for d in [0-9][0-9][0-9]
do
( cd "$d" && your-command-here )
done
(和)创建一个子shell,因此当前目录不会在主脚本中更改。
for dir in PARENT/*
do
test -d "$dir" || continue
# Do something with $dir...
done
#!/bin.bash
for folder_to_go in $(find . -mindepth 1 -maxdepth 1 -type d \( -name "*" \) ) ;
# you can add pattern insted of * , here it goes to any folder
#-mindepth / maxdepth 1 means one folder depth
do
cd $folder_to_go
echo $folder_to_go "########################################## "
whatever you want to do is here
cd ../ # if maxdepth/mindepath = 2, cd ../../
done
#you can try adding many internal for loops with many patterns, this will sneak anywhere you want
如果顶层文件夹是已知的,你可以这样写:
for dir in `ls $YOUR_TOP_LEVEL_FOLDER`;
do
for subdir in `ls $YOUR_TOP_LEVEL_FOLDER/$dir`;
do
$(PLAY AS MUCH AS YOU WANT);
done
done
在$上(想玩多少就玩多少);你可以放尽可能多的代码。
注意,我没有在任何目录上使用“cd”。
欢呼,