简体   繁体   English

在Linux的父目录中递归搜索文件

[英]Search recursively for files in a parent directory in Linux

I am trying to list all the files in a parent directory and its subdirectories. 我正在尝试列出父目录及其子目录中的所有文件。 However, I am running this command from another location. 但是,我正在从另一个位置运行此命令。 So, at first, I need to traverse to the directory (from where I want to run this command). 因此,首先,我需要遍历目录(要从中运行此命令)。

Please note that I am using the find command instead of ls because I also want to list down the absolute path for each file in front of it. 请注意,我使用的是find命令而不是ls,因为我也想在它前面列出每个文件的绝对路径。 This is not possible with the ls command. 使用ls命令是不可能的。

here is what I am doing: 这是我在做什么:

cd ../../../;cd level1_dir1;find $(pwd) . -name *.* -printf "%TY-%Tm-%Td\t%p\n"

This command does not show any output. 该命令不显示任何输出。

Here is the directory structure: 这是目录结构:

level1_dir1

this has multiple subdirectories: 它具有多个子目录:

level2_dir1
level2_dir2

....

level2_dir10

each of these subdirectories again have subdirectories and files inside them. 这些子目录中的每一个都在其中又有子目录和文件。

however, now if I do: 但是,现在,如果我这样做:

cd ../../../;cd level1_dir1/level2_dir1;find $(pwd) . -name *.* -printf "%TY-%Tm-%Td\t%p\n"

it will do the recursion properly for all the subdirectories in level2_dir1 and show output like: 它将对level2_dir1中的所有子目录正确执行递归,并显示如下输出:

date level1_dir1/level2_dir1/path/to/file/filename

so, I wanted to print out for all the level2 directories, this way (by using the wild character): 因此,我想以这种方式(通过使用通配符)打印出所有level2目录:

cd ../../../;cd level1_dir1/*;find $(pwd) . -name *.* -printf "%TY-%Tm-%Td\t%p\n"

but it prints out the results only for the first directory in level2 (that is level2_dir1) 但它仅打印出level2中第一个目录(即level2_dir1)的结果

how can I make it list down the files for all the subdirectories? 如何使其列出所有子目录的文件?

thanks. 谢谢。

How about this? 这个怎么样?

find ../../../level1_dir1 -printf "%TY-%Tm-%Td\t%p\n"

If you want all the files, you don't even need -name in the find command. 如果需要所有文件,在find命令中甚至不需要-name。 If you don't want to see the directories and only the files, just add "-type f" before -printf. 如果您不想仅查看目录,而仅查看文件,只需在-printf之前添加“ -type f”。

Hope this helps... 希望这可以帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM