[英]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.