如何仅列出目录 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

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

How to list only files and not directories of a directory Bash?

bashfile-listing

提问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 1option 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 using ls: you get a potentially unsortedlist of filenames all prefixed with ./, and you lose the ability to apply ls's many options;
    also findinvariablyfinds hiddenitems too, whereas ls' behavior depends on the presence or absence of the -aor -Aoptions.

    • 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.
  • Hans Roggeman's ls+ grepansweris 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 globbingfind

          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 lswhile also providing type-filtering capability,
  • simply by placing type-filtering characterssuch as ffor files, dfor directories, and lfor symlinksbefore a list of lsarguments (run fls --helpor fls --manto learn more).
  • 提供输出灵活性的ls实用程序,同时还提供类型过滤功能
  • 只需在参数列表之前放置类型过滤字符,例如f文件、d目录和l符号链接ls(运行fls --helpfls --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 注册表安装时:LinuxmacOS
  • 手动安装时:任何带有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; try
curl -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 sudodepends on how you installed Node.js / io.js and whether you've changed permissions later; if you get an EACCESerror, try again with sudo.

  • The -gensures globalinstallationand is needed to put flsin your system's $PATH.

  • 是否需要sudo取决于你是如何安装 Node.js/io.js 的,以及你后来是否更改了权限;如果出现EACCES错误,请使用 重试sudo

  • -g确保全球安装,并需要把fls你的系统$PATH

Manual installation

手动安装

  • Download this bashscriptas fls.
  • 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 lswith grepor egrepand put it in your profile as an alias:

您还可以使用lswithgrepegrep并将其作为别名放在您的个人资料中:

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 lsoptions, for sample:

我喜欢使用ls选项,例如:

  • -luse a long listing format
  • -tsort by modification time, newest first
  • -rreverse order while sorting
  • -F, --classifyappend indicator (one of */=>@|) to entries
  • -h, --human-readablewith -l and -s, print sizes like 1K 234M 2G etc...
  • -l使用长列表格式
  • -t按修改时间排序,最新的在前
  • -r排序时倒序
  • -F,--classify将指示符(*/=>@| 之一)附加到条目
  • -h--human-readable使用 -l 和 -s,打印大小如 1K 234M 2G 等...

Sometime --colorand 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/pathby .to list for current directoryor ..for parent directory.

您可以取代/some/path通过.以列表当前目录..父目录

回答by Anabioz

{ find . -maxdepth 1 -type f | xargs ls -1t | less; }

added xargsto make it works, and used -1instead of -lto show only filenames without additional lsinfo

添加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 ; 菲; 完毕