如何仅列出目录 Bash 的文件而不列出目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10574794/
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
How to list only files and not directories of a directory Bash?
提问by Rodrigo
How can I list all the files of one folder but not their folders or subfiles. In other words: How can I list only the files?
如何列出一个文件夹的所有文件而不是它们的文件夹或子文件。换句话说:我怎样才能只列出文件?
回答by carlpett
Using find
:
使用find
:
find . -maxdepth 1 -type f
Using the -maxdepth 1
option ensures that you only look in the current directory (or, if you replace the .
with some path, that directory). If you want a full recursive listing of all files in that and subdirectories, just remove that option.
使用该-maxdepth 1
选项可确保您只查看当前目录(或者,如果您将 替换为.
某个路径,则是该目录)。如果您想要该目录和子目录中所有文件的完整递归列表,只需删除该选项。
回答by Dr_Hope
ls -p | grep -v /
ls -p lets you show / after the folder name, which acts as a tag for you to remove.
ls -p 允许您在文件夹名称后显示 / ,它充当您要删除的标签。
回答by mklement0
carlpett's
find
-based answer(find . -maxdepth 1 -type f
) works in principle, but is not quite the same as usingls
: you get a potentially unsortedlist of filenames all prefixed with./
, and you lose the ability to applyls
's many options;
alsofind
invariablyfinds hiddenitems too, whereasls
' behavior depends on the presence or absence of the-a
or-A
options.An improvement, suggested by Alex Hallin a comment on the question is to combine shell globbingwith
find
:find * -maxdepth 0 -type f # find -L * ... includes symlinks to files
- However, while this addresses the prefix problem and gives you alphabetically sorted output, you still have neither (inline) control over inclusion of hidden items nor access to
ls
's many other sorting / output-format options.
- However, while this addresses the prefix problem and gives you alphabetically sorted output, you still have neither (inline) control over inclusion of hidden items nor access to
Hans Roggeman's
ls
+grep
answeris pragmatic, but locks you into using long (-l
) output format.
carlpett 的
find
基于答案(find . -maxdepth 1 -type f
) 原则上有效,但与 using 不太一样ls
:您会得到一个潜在的未排序的文件名列表,所有文件名都以 为前缀./
,并且您无法应用ls
的许多选项;
也find
总是会发现隐藏的项目,而ls
' 行为取决于-a
或-A
选项的存在与否。Alex Hall在对该问题的评论中建议的一项改进是将shell globbing与:
find
find * -maxdepth 0 -type f # find -L * ... includes symlinks to files
- 然而,虽然这解决了前缀问题并为您提供了按字母顺序排序的输出,但您仍然无法(内联)控制包含隐藏项目,也无法访问
ls
许多其他排序/输出格式选项。
- 然而,虽然这解决了前缀问题并为您提供了按字母顺序排序的输出,但您仍然无法(内联)控制包含隐藏项目,也无法访问
Hans Roggeman 的
ls
+grep
答案是务实的,但会将您锁定为使用 long (-l
) 输出格式。
To address these limitations I wrote the fls
(filtering ls) utility,
为了解决这些局限性,所以我写的fls
(˚Filtering LS)实用程序,
- a utility that provides the output flexibility of
ls
while also providing type-filtering capability, - simply by placing type-filtering characterssuch as
f
for files,d
for directories, andl
for symlinksbefore a list ofls
arguments (runfls --help
orfls --man
to learn more).
- 提供输出灵活性的
ls
实用程序,同时还提供类型过滤功能, - 只需在参数列表之前放置类型过滤字符,例如
f
文件、d
目录和l
符号链接ls
(运行fls --help
或fls --man
了解更多信息)。
Examples:
例子:
fls f # list all files in current dir.
fls d -tA ~ # list dirs. in home dir., including hidden ones, most recent first
fls f^l /usr/local/bin/c* # List matches that are files, but not (^) symlinks (l)
Installation
安装
Supported platforms
支持的平台
- When installing from the npm registry: Linuxand macOS
- When installing manually: any Unix-likeplatform with Bash
- 从npm 注册表安装时:Linux和macOS
- 手动安装时:任何带有Bash 的类 Unix平台
From the npm registry
从npm 注册表
Note: Even if you don't use Node.js, its package manager, npm
, works across platforms and is easy to install; trycurl -L https://git.io/n-install | bash
注意:即使您不使用 Node.js,它的包管理器 也npm
可以跨平台工作并且易于安装;尝试curl -L https://git.io/n-install | bash
With Node.jsinstalled, install as follows:
随着Node.js的安装,安装如下:
[sudo] npm install fls -g
Note:
注意:
Whether you need
sudo
depends on how you installed Node.js / io.js and whether you've changed permissions later; if you get anEACCES
error, try again withsudo
.The
-g
ensures globalinstallationand is needed to putfls
in your system's$PATH
.
Manual installation
手动安装
- Download this
bash
scriptasfls
. - Make it executable with
chmod +x fls
. - Move it or symlink it to a folder in your
$PATH
, such as/usr/local/bin
(macOS) or/usr/bin
(Linux).
- 将此
bash
脚本下载为fls
. - 使用
chmod +x fls
. - 将其移动或符号链接到您的 中的文件夹
$PATH
,例如/usr/local/bin
(macOS) 或/usr/bin
(Linux)。
回答by Hans Roggeman
You can also use ls
with grep
or egrep
and put it in your profile as an alias:
您还可以使用ls
withgrep
或egrep
并将其作为别名放在您的个人资料中:
ls -l | egrep -v '^d'
ls -l | grep -v '^d'
回答by Richard
find files: ls -l /home | grep "^-" | tr -s ' ' | cut -d ' ' -f 9
查找文件:ls -l /home | grep "^-" | tr -s ' ' | 剪切 -d ' ' -f 9
find directories: ls -l /home | grep "^d" | tr -s ' ' | cut -d ' ' -f 9
查找目录: ls -l /home | grep "^d" | tr -s ' ' | 剪切 -d ' ' -f 9
find links: ls -l /home | grep "^l" | tr -s ' ' | cut -d ' ' -f 9
查找链接:ls -l /home | grep "^l" | tr -s ' ' | 剪切 -d ' ' -f 9
tr -s ' ' turns the output into a space-delimited file the cut command says the delimiter is a space, and return the 9th field (always the filename/directory name/linkname).
tr -s ' ' 将输出转换为以空格分隔的文件 cut 命令表示分隔符是一个空格,并返回第 9 个字段(始终是文件名/目录名/链接名)。
I use this all the time!
我经常用这个!
回答by F. Hauri
Listing content of some directory, without subdirectories
列出某个目录的内容,没有子目录
I like using ls
options, for sample:
我喜欢使用ls
选项,例如:
-l
use a long listing format-t
sort by modification time, newest first-r
reverse order while sorting-F
,--classify
append indicator (one of */=>@|) to entries-h
,--human-readable
with -l and -s, print sizes like 1K 234M 2G etc...
-l
使用长列表格式-t
按修改时间排序,最新的在前-r
排序时倒序-F
,--classify
将指示符(*/=>@| 之一)附加到条目-h
,--human-readable
使用 -l 和 -s,打印大小如 1K 234M 2G 等...
Sometime --color
and all others. (See ls --help
)
有时--color
和所有其他人。(见ls --help
)
Listing everything but folders
列出除文件夹之外的所有内容
This will show files, symlinks, devices, pipe, sockets etc.
这将显示文件、符号链接、设备、管道、套接字等。
so
所以
find /some/path -maxdepth 1 ! -type d
could be sorted by date easily:
可以很容易地按日期排序:
find /some/path -maxdepth 1 ! -type d -exec ls -hltrF {} +
Listing filesonly:
仅列出文件:
or
或者
find /some/path -maxdepth 1 -type f
sorted by size:
按尺寸排序:
find /some/path -maxdepth 1 -type f -exec ls -lSF --color {} +
Prevent listing of hidden entries:
防止列出隐藏条目:
To not show hidden entries, where name begin by a dot, you could add ! -name '.*'
:
要不显示名称以点开头的隐藏条目,您可以添加! -name '.*'
:
find /some/path -maxdepth 1 ! -type d ! -name '.*' -exec ls -hltrF {} +
Then
然后
You could replace /some/path
by .
to list for current directoryor ..
for parent directory.
您可以取代/some/path
通过.
以列表当前目录或..
为父目录。
回答by Anabioz
{ find . -maxdepth 1 -type f | xargs ls -1t | less; }
added xargs
to make it works, and used -1
instead of -l
to show only filenames without additional ls
info
添加xargs
以使其正常工作,并用于-1
代替-l
仅显示没有附加ls
信息的文件名
回答by Abhay
Just adding on to carlpett's answer. For a much useful view of the files, you could pipe the output to ls.
只是添加到卡尔佩特的答案。要获得非常有用的文件视图,您可以将输出通过管道传输到 ls。
find . -maxdepth 1 -type f|ls -lt|less
Shows the most recently modified files in a list format, quite useful when you have downloaded a lot of files, and want to see a non-cluttered version of the recent ones.
以列表格式显示最近修改的文件,当您下载了大量文件并希望查看最近文件的整洁版本时非常有用。
回答by Gill Davison
"find '-maxdepth' " does not work with my old version of bash, therefore I use:
“find '-maxdepth'”不适用于我的旧版 bash,因此我使用:
for f in $(ls) ; do if [ -f $f ] ; then echo $f ; fi ; done
对于 $(ls) 中的 f ;如果 [ -f $f ] ; 然后回声 $f ; 菲; 完毕