[英]linux command line recursively check directories for at least 1 file with the same name as the directory
我有一個包含大量目錄的目錄。 每個目錄包含一些文件,在某些情況下包含另一個目錄
parent_directory
sub_dir_1
sub_dir_1.txt
sub_dir_1_1.txt
sub_dir_2
sub_dir_2.txt
sub_dir_2_1.txt
sub_dir_3
sub_dir_3.txt
sub_dir_3_1.txt
sub_dir_4
sub_dir_4.txt
sub_dir_4_1.txt
sub_dir_5
sub_dir_5.txt
sub_dir_5_1.txt
我需要檢查每個sub_dir是否包含至少一個具有完全相同名稱的文件。 如果sub_dirs中有子目錄,我不需要再查看。
我想for d in ./*/ ; do (command here); done
中使用for d in ./*/ ; do (command here); done
for d in ./*/ ; do (command here); done
for d in ./*/ ; do (command here); done
但我不知道如何訪問for循環中的sub_dir名稱
for d in ./*/ ;
do
(if directory does not contain 1 file that is the same name as the directory then echo directory name );
done
這樣做的最佳方式是什么?還是有更簡單的方法?
從父目錄
find -maxdepth 1 -type d -printf "%f\n" |
xargs -I {} find {} -maxdepth 1 -type f -name {}.txt
會給你名字/ name.txt對。 與所有目錄名稱比較以找到缺失的名稱。
UPDATE
這可能更簡單,而不是掃描,您可以檢查文件是否存在
for f in $(find -maxdepth 1 -type d -printf "%f\n");
do if [ ! -e "$f/$f.txt" ];
then echo "$f not found";
fi; done
也許不完全明白,但是
find . -print | grep -P '/(.*?)/\1\.txt'
這將打印同名目錄中的任何文件,例如:
./a/b/b.txt
./a/c/d/d.txt
等等...
同樣
find . -print | sed -n '/\(.*\)\/\1\.txt/p'
這個
find . -print | grep -P '/(.*?)/\1\.'
將列出所有文件,無論同名dirs中的擴展名如何。
您可以按照backreference
邏輯制作其他正則表達式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.