我有一个包含大量目录的目录。每个目录包含一些文件,在某些情况下还包含另一个目录。
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 ./*/;做(在这里命令);完成
但我不知道如何访问 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/name.txt 对。与所有目录名称进行比较以找到丢失的目录。
更新
这可能更简单,而不是扫描你可以检查文件是否存在
for f in $(find -maxdepth 1 -type d -printf "%f\n");
do if [ ! -e "$f/$f.txt" ];
then echo "$f not found";
fi; done
关于linux命令行递归检查目录中至少有1个与目录同名的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43191677/