简体   繁体   English

如何在 Bash 中使用 ls 命令仅显示目录中的隐藏文件

[英]How to display only hidden files in directory using ls command in Bash

I am trying to display the hidden files from a directory using the ls command alone, except for .我试图单独使用 ls 命令显示目录中的隐藏文件,除了. and .. ...

ls -lAd .*

But the command returns .但命令返回. and .. directory names also in the output ...目录名称也在输出中。 What's wrong in this command even though I mentioned "A" option with ls .尽管我在 ls 中提到了“A”选项,但此命令有什么问题。

您可以使用GLOBIGNORE内部变量。

$ GLOBIGNORE=.:.. ls -ld .*

This is here for reference but the answer is given by @anishsane below .这是在这里供参考,但下面@anishsane给出了答案。

Your glob is wrong:你的 glob 是错误的:

.*

will be expanded to .将扩展为. , and .. . ,和.. You can either change the glob to something like:您可以将 glob 更改为:

.??*

but this will not match hidden files like: .a , .b .但这不会匹配隐藏文件,例如: .a.b Or .[^.]* but that will not match files like ..a , ..b , combining both might be an option:.[^.]*但这不会匹配..a..b类的文件,将两者结合起来可能是一种选择:

ls -lAd .[^.]*  ..?*

But that will most likely yeild: ls: error: ..?* no such file or directory .但这很可能 yeild: ls: error: ..?* no such file or directory Enabling nullglob will prevent this:启用 nullglob 将防止这种情况:

shopt -s nullglob
ls -lAd .[^.]*  ..?*

This will however expand to ls -lAd if no hidden files are in the directory, which will show current working directory ( . )但是,如果目录中没有隐藏文件,这将扩展为ls -lAd ,它将显示当前工作目录 ( . )

Alternative you can pipe the output from ls to awk , but that will most likely get rid of the colors:或者,您可以将ls的输出通过管道传输到awk ,但这很可能会消除颜色:

ls -lA | awk '$9 ~ /^\./'

Also consider reading: Why you shouldn't parse the output of ls(1)还可以考虑阅读:为什么你不应该解析 ls(1) 的输出

ls -Al --ignore="[^.]*"

where you ignore all files and directories which NOT start with a .您忽略所有不以.

Extra : You can call this for any directory, which comes in handy if you use it as an alias in your .bashrc额外:您可以为任何目录调用它,如果您将它用作 .bashrc 中的别名,这会派上用场

alias lsh='ls -Al --ignore="[^.]*"'

Just do就做

lsh /path/directory

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

相关问题 如何过滤ls命令的输出只显示二月份创建的文件? - How to filter the output of ls command to display only files created in February? 如何仅显示来自 aws s3 ls 命令的文件? - How to display only files from aws s3 ls command? Linux-使用ls或find命令查找所有者可执行的隐藏文件 - Linux - finding hidden files that are executable by the owner using ls or find command 如何确定ls命令输出是文件还是目录Bash - How to find out if ls command output is file or a directory Bash 如何在bash中一一读取ls命令列出的文件? - how to read files listed by ls command one by one in bash? ls命令忽略特定目录[bash] - ls command ignore specific directory [bash] 在特殊边缘情况下使用 ls 命令时如何仅显示文件所有者 - How to only display owner of file when using ls command with special edge case 如何使用 ls 命令显示所有文件,包括隐藏文件,但不显示名为 '.' 的文件和 '..' - How to use ls command to show all the files including hidden files, but not showing the files named '.' and '..' 如何使用sort命令但不使用ls -lrt命令列出文件 - How to list the files using sort command but not ls -lrt command Bash-正则表达式,确定ls -al的输出是文件还是目录,并且是隐藏的 - Bash - Regex to determine if output of ls -al is file or directory and hidden
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM