[英]linux command line recursively check directories for at least 1 file with the same name as the directory
I have a directory containing a large number of directories. 我有一个包含大量目录的目录。 Each directory contains some files and in some cases another 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
I need to check that each sub_dir contains at least one file with the exact same name. 我需要检查每个sub_dir是否包含至少一个具有完全相同名称的文件。 I don' need to check any further down if there are sub directories within the sub_dirs. 如果sub_dirs中有子目录,我不需要再查看。
I was thinking of using 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 d in ./*/ ; do (command here); done
but I dont know how to get access to the sub_dir name inside the for loop 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
What is the best way to do this or is there a simpler way? 这样做的最佳方式是什么?还是有更简单的方法?
from the parent directory 从父目录
find -maxdepth 1 -type d -printf "%f\n" |
xargs -I {} find {} -maxdepth 1 -type f -name {}.txt
will give you the name/name.txt pair. 会给你名字/ name.txt对。 Compare with the all dir names to find the missing ones. 与所有目录名称比较以找到缺失的名称。
UPDATE UPDATE
this might be simpler, instead of scanning you can check whether file exists or not 这可能更简单,而不是扫描,您可以检查文件是否存在
for f in $(find -maxdepth 1 -type d -printf "%f\n");
do if [ ! -e "$f/$f.txt" ];
then echo "$f not found";
fi; done
Maybe not understand fully, but 也许不完全明白,但是
find . -print | grep -P '/(.*?)/\1\.txt'
this will print any file which is inside of the same-named directory, eg: 这将打印同名目录中的任何文件,例如:
./a/b/b.txt
./a/c/d/d.txt
etc... 等等...
Similarly 同样
find . -print | sed -n '/\(.*\)\/\1\.txt/p'
this 这个
find . -print | grep -P '/(.*?)/\1\.'
will list all files regardless of the extension in same-named dirs. 将列出所有文件,无论同名dirs中的扩展名如何。
You can craft other regexes following the backreference
logic. 您可以按照backreference
逻辑制作其他正则表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.