bash ls 目录中的所有文件以及下一级目录中的所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13733688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:56:43  来源:igfitidea点击:

ls all files in directory plus all files in directories one level down

linuxbashcommand-linels

提问by MrBrightside

Ok I have the following situation.

好的,我有以下情况。

Caps are directories, lowercase are files.

大写是目录,小写是文件。

A/aa
B/bb
C/cc
D/dd
D/E/ddd
D/F/G/dddd
a
b
c
d

I want to do a ls that lists

我想做一个 ls 列出

a
b
c
d
A/aa
B/bb
C/cc
D/dd

but not either

但也不是

D/E/ddd
D/F/G/dddd

回答by Chris Seymour

Using findto find only files in the current directory or one directory down:

使用find发现只在当前目录或一个目录下的文件:

$ find . -maxdepth 2  -type f

Demo:

演示:

# Show whole directory structure, digits are files, letters are folders. 
$  find .
.
./1
./2
./3
./4
./A
./A/11
./B
./B/22
./C
./C/33
./D
./D/44
./D/E
./D/F
./D/F/444
./D/F/G
./D/F/G/4444

# Find only files at a maximum depth of 2
$  find . -maxdepth 2  -type f
./1
./2
./3
./4
./A/11
./B/22
./C/33
./D/44

回答by Jarmund

This one lists everything inside directories in your current working dir: ls -l */

这个列出了当前工作目录中目录中的所有内容: ls -l */

A combination of two commands will include files in your current directory as well: ls -l */; ls -l

两个命令的组合也将包含当前目录中的文件: ls -l */; ls -l

回答by Jim Stewart

You can do this with find:

你可以用 find 来做到这一点:

find . -maxdepth 2