分享

Linux 中使用 ls 命令列出子文件夹

 rqn2008 2025-03-11

(以下命令均支持使用重定向符把文件夹清单输出到文本文件中,比如:tree > 1.txt)


银河麒麟Linux中,如何用ls命令列出所有子文件夹?

在银河麒麟 Linux 系统中,ls 命令本身功能有限,它主要用于列出目录内容,但可以结合其他命令或者使用特定选项来列出所有子文件夹。以下为你介绍几种常见的方法:

方法一:使用 ls -d 选项结合通配符


-d 选项可以让 ls 命令只列出目录本身,而不列出目录中的内容。结合通配符 */ 可以匹配所有子文件夹。

命令示例


bash
ls -d */
bash
9
1
2
ls -d */


解释


  • -d:只显示目录本身,而非目录中的内容。

  • */:通配符,匹配当前目录下的所有子文件夹。


使用场景


该方法适用于仅查看当前目录下的一级子文件夹。

方法二:递归列出所有子文件夹


如果你想递归地列出当前目录及其所有子目录下的所有文件夹,可以结合 find 命令。

命令示例


bash
find . -type d

解释


  • .:表示从当前目录开始查找。

  • -type d:指定查找的类型为目录(d 代表 directory)。


使用场景


当你需要查看当前目录及其所有子目录下的所有文件夹时,此方法非常有用。

方法三:使用 tree 命令


tree 命令可以以树形结构显示目录及其子目录。

安装 tree 命令


如果系统中没有安装 tree 命令,可以使用以下命令进行安装:

bash
sudo apt-get update
sudo apt-get install tree

命令示例


bash
tree -d

解释


  • -d:只显示目录,不显示文件。


使用场景


该方法适用于需要以直观的树形结构查看目录层次的情况。

示例


假设当前目录结构如下:

plaintext
.
├── dir1
│   ├── subdir1
│   └── subdir2
├── dir2
└── file.txt

  • 使用 ls -d */ 命令,输出将是:


plaintext
dir1/ dir2/

  • 使用 find . -type d 命令,输出将是:


plaintext
.
./dir1
./dir1/subdir1
./dir1/subdir2
./dir2

  • 使用 tree -d 命令,输出将是:


plaintext
.
├── dir1
│   ├── subdir1
│   └── subdir2
└── dir2

2 directories

 LS的帮助文件:

ls --help

用法:ls [选项]... [文件]...

List information about the FILEs (the current directory by default).

Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

必选参数对长短选项同时适用。

  -a, --all不隐藏任何以. 开始的项目

  -A, --almost-all列出除. 及.. 以外的任何项目

      --author与-l 同时使用时列出每个文件的作者

  -b, --escape以八进制溢出序列表示不可打印的字符

      --block-size=SIZE      with -l, scale sizes by SIZE when printing them;

                               e.g., '--block-size=M'; see SIZE format below

  -B, --ignore-backups       do not list implied entries ending with ~

  -c                         with -lt: sort by, and show, ctime (time of last

                               modification of file status information);

                               with -l: show ctime and sort by name;

                               otherwise: sort by ctime, newest first

  -C                         list entries by columns

      --color[=WHEN]         colorize the output; WHEN can be 'always' (default

                               if omitted), 'auto', or 'never'; more info below

  -d, --directory            list directories themselves, not their contents

  -D, --dired                generate output designed for Emacs' dired mode

  -f                         do not sort, enable -aU, disable -ls --color

  -F, --classify             append indicator (one of */=>@|) to entries

      --file-type            likewise, except do not append '*'

      --format=WORD          across -x, commas -m, horizontal -x, long -l,

                               single-column -1, verbose -l, vertical -C

      --full-time            like -l --time-style=full-iso

  -g类似-l,但不列出所有者

      --group-directories-first

                             group directories before files;

                               can be augmented with a --sort option, but any

                               use of --sort=none (-U) disables grouping

  -G, --no-group             in a long listing, don't print group names

  -h, --human-readable       with -l and -s, print sizes like 1K 234M 2G etc.

      --si                   likewise, but use powers of 1000 not 1024

  -H, --dereference-command-line

                             follow symbolic links listed on the command line

      --dereference-command-line-symlink-to-dir

                             follow each command line symbolic link

                               that points to a directory

      --hide=PATTERN         do not list implied entries matching shell PATTERN

                               (overridden by -a or -A)

      --hyperlink[=WHEN]     hyperlink file names; WHEN can be 'always'

                               (default if omitted), 'auto', or 'never'

      --indicator-style=WORD  append indicator with style WORD to entry names:

                               none (default), slash (-p),

                               file-type (--file-type), classify (-F)

  -i, --inode                print the index number of each file

  -I, --ignore=PATTERN       do not list implied entries matching shell PATTERN

  -k, --kibibytes            default to 1024-byte blocks for disk usage;

                               used only with -s and per directory totals

  -l使用较长格式列出信息

  -L, --dereference当显示符号链接的文件信息时,显示符号链接所指示

的对象而并非符号链接本身的信息

  -m所有项目以逗号分隔,并填满整行行宽

  -n, --numeric-uid-gid      like -l, but list numeric user and group IDs

  -N, --literal              print entry names without quoting

  -o                         like -l, but do not list group information

  -p, --indicator-style=slash

                             append / indicator to directories

  -q, --hide-control-chars   print ? instead of nongraphic characters

      --show-control-chars   show nongraphic characters as-is (the default,

                               unless program is 'ls' and output is a terminal)

  -Q, --quote-name           enclose entry names in double quotes

      --quoting-style=WORD   use quoting style WORD for entry names:

                               literal, locale, shell, shell-always,

                               shell-escape, shell-escape-always, c, escape

                               (overrides QUOTING_STYLE environment variable)

  -r, --reverse逆序排列

  -R, --recursive递归显示子目录

  -s, --size以块数形式显示每个文件分配的尺寸

  -S                         sort by file size, largest first

      --sort=WORD            sort by WORD instead of name: none (-U), size (-S),

                               time (-t), version (-v), extension (-X)

      --time=WORD            with -l, show time as WORD instead of default

                               modification time: atime or access or use (-u);

                               ctime or status (-c); also use specified time

                               as sort key if --sort=time (newest first)

      --time-style=TIME_STYLE  time/date format with -l; see TIME_STYLE below

  -t                         sort by modification time, newest first

  -T, --tabsize=COLS         assume tab stops at each COLS instead of 8

  -u                         with -lt: sort by, and show, access time;

                               with -l: show access time and sort by name;

                               otherwise: sort by access time, newest first

  -U                         do not sort; list entries in directory order

  -v                         natural sort of (version) numbers within text

  -w, --width=COLS           set output width to COLS.  0 means no limit

  -x                         list entries by lines instead of by columns

  -X                         sort alphabetically by entry extension

  -Z, --context              print any security context of each file

  -1                         list one file per line.  Avoid '\n' with -q or -b

      --help显示此帮助信息并退出

      --version显示版本信息并退出

The SIZE argument is an integer and optional unit (example: 10K is 10*1024).

Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).

The TIME_STYLE argument can be full-iso, long-iso, iso, locale, or +FORMAT.

FORMAT is interpreted like in date(1).  If FORMAT is FORMAT1<newline>FORMAT2,

then FORMAT1 applies to non-recent files and FORMAT2 to recent files.

TIME_STYLE prefixed with 'posix-' takes effect only outside the POSIX locale.

Also the TIME_STYLE environment variable sets the default style to use.

使用色彩来区分文件类型的功能已被禁用,默认设置和 --color=never 同时禁用了它。

使用 --color=auto 选项,ls 只在标准输出被连至终端时才生成颜色代码。

LS_COLORS 环境变量可改变此设置,可使用 dircolors 命令来设置。

退出状态:

 0  正常

 1  一般问题 (例如:无法访问子文件夹)

 2  严重问题 (例如:无法使用命令行参数)

GNU coreutils 在线帮助:<https://www./software/coreutils/>

请向 <http:///team/zh_CN.html> 报告 ls 的翻译错误

完整文档请见:<https://www./software/coreutils/ls>

或者在本地使用:info '(coreutils) ls invocation'



TREE的帮助文件

$ tree --help

usage: tree [-acdfghilnpqrstuvxACDFJQNSUX] [-H baseHREF] [-T title ]

[-L level [-R]] [-P pattern] [-I pattern] [-o filename] [--version]

[--help] [--inodes] [--device] [--noreport] [--nolinks] [--dirsfirst]

[--charset charset] [--filelimit[=]#] [--si] [--timefmt[=]<f>]

[--sort[=]<name>] [--matchdirs] [--ignore-case] [--fromfile] [--]

[<directory list>]

  ------- Listing options -------

  -a            All files are listed.

  -d            List directories only.

  -l            Follow symbolic links like directories.

  -f            Print the full path prefix for each file.

  -x            Stay on current filesystem only.

  -L level      Descend only level directories deep.

  -R            Rerun tree when max dir level reached.

  -P pattern    List only those files that match the pattern given.

  -I pattern    Do not list files that match the given pattern.

  --ignore-case Ignore case when pattern matching.

  --matchdirs   Include directory names in -P pattern matching.

  --noreport    Turn off file/directory count at end of tree listing.

  --charset X   Use charset X for terminal/HTML and indentation line output.

  --filelimit # Do not descend dirs with more than # files in them.

  --timefmt <f> Print and format time according to the format <f>.

  -o filename   Output to file instead of stdout.

  ------- File options -------

  -q            Print non-printable characters as '?'.

  -N            Print non-printable characters as is.

  -Q            Quote filenames with double quotes.

  -p            Print the protections for each file.

  -u            Displays file owner or UID number.

  -g            Displays file group owner or GID number.

  -s            Print the size in bytes of each file.

  -h            Print the size in a more human readable way.

  --si          Like -h, but use in SI units (powers of 1000).

  -D            Print the date of last modification or (-c) status change.

  -F            Appends '/', '=', '*', '@', '|' or '>' as per ls -F.

  --inodes      Print inode number of each file.

  --device      Print device ID number to which each file belongs.

  ------- Sorting options -------

  -v            Sort files alphanumerically by version.

  -t            Sort files by last modification time.

  -c            Sort files by last status change time.

  -U            Leave files unsorted.

  -r            Reverse the order of the sort.

  --dirsfirst   List directories before files (-U disables).

  --sort X      Select sort: name,version,size,mtime,ctime.

  ------- Graphics options -------

  -i            Don't print indentation lines.

  -A            Print ANSI lines graphic indentation lines.

  -S            Print with CP437 (console) graphics indentation lines.

  -n            Turn colorization off always (-C overrides).

  -C            Turn colorization on always.

  ------- XML/HTML/JSON options -------

  -X            Prints out an XML representation of the tree.

  -J            Prints out an JSON representation of the tree.

  -H baseHREF   Prints out HTML format with baseHREF as top directory.

  -T string     Replace the default HTML title and H1 header with string.

  --nolinks     Turn off hyperlinks in HTML output.

  ------- Input options -------

  --fromfile    Reads paths from files (.=stdin)

  ------- Miscellaneous options -------

  --version     Print version and exit.

  --help        Print usage and this help message and exit.

  --            Options processing terminator.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多