简体   繁体   English

linux命令行以递归方式检查目录,查找至少1个与目录同名的文件

[英]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.

相关问题 在目录中按名称查找文件的重复项-Linux - Find duplicates of a file by name in a directory recursively - Linux Python或Linux命令行检测.tar.gz中的目录名称 - Python or linux command line detect directory name in .tar.gz 检查是否存在与目录同名的文件 - Check if a file exists with the same name as a directory 在linux中,将多个目录中的同名文件复制到一个以路径不同为名称的新目录中 - In linux, copy files with the same name in multiple directories, to a new directory with the path difference as name Linux命令在目录中查找具有相同名称但扩展名不同的所有文件? - Linux command to find all the files with same name but different extension in a directory? 如何在Linux上裁剪和转换目录和子目录中的所有图像(命令行)? - How to Crop and Convert all images in the directory and sub directories on Linux (command line)? 如何在Linux中以名称Bouvet-Island.png递归复制同一目录中的所有文件Norway.png - How to copy all files Norway.png in the same directory recursively with the name Bouvet-Island.png in Linux 使用linux命令行将文件从多个目录复制到另一个目录 - Copying files from multiple directories to another directory using linux command line 递归删除同级Linux上的文件夹内的目录 - Recursively remove directories inside folder on same level Linux 如何使用cp linux将多个目录中的同名文件复制到新目录中 - How to copy a file with the same name from multiple directories into new directories using cp linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM