linux终端 隐藏文件,如何只在终端中显示隐藏的文件?

问题描述

我有一个包含数千个文件的目录,其中一些是隐藏的。

命令ls -a列出所有文件,包括隐藏文件,但我只需要列出隐藏的文件。

我应该使用什么命令?

最佳解决思路

命令 :

ls -ld .?*

只会列出隐藏的文件。

解释:

-l use a long listing format

-d, --directory

list directory entries instead of contents, and do not derefer‐

ence symbolic links

.?* will only state hidden files

次佳解决思路

ls -d .!(|.)

确切地说,OP正在寻找什么。

第三种解决思路

如果你只想要当前目录中的文件(不递归),你可以这样做

echo .[^.]*

这将打印名称以.开头并且后跟一个或多个non-dot字符的所有文件的名称。请注意,对于名称以连续点开头的文件,这将失败,因此例如....foo将不会显示。

你也可以使用find:

find -mindepth 1 -prune -name '.*'

-mindepth确保我们不匹配.,并且-prune意味着find不会下降到子目录中。

第四种思路

ls -ad .*

在Bash为我工作。

第五种思路

使用find和awk,

find . -type f | awk -F"/" '$NF ~ /^\..*$/ {print $NF}'

说明:

find . -type f – >列出当前目录中的所有文件以及它的路径,

./foo.html

./bar.html

./.foo1

awk -F"/" '$NF ~ /^\..*$/ {print $NF}'

/作为字段分隔符awk检查最后一个字段是否带点。如果它以点开始,那么它会打印该相应行的最后一个字段。

第六种思路

对于复杂的搜索,find通常是比使用名称匹配更好的选项。

find . -mindepth 1 -maxdepth 1 -name '.*'

要么

find . -mindepth 1 -maxdepth 1 -name '.*' -o -name '*~'

find .搜索当前目录

-mindepth 1排除。和..从列表中

-maxdepth 1将搜索限制到当前目录

-name '.*'查找以点开头的文件名

-o或

-name '*~'查找以波形符结尾的文件名(通常,这些是来自文本编辑程序的备份文件)

但是,这个和所有其他答案都会丢失当前目录的.hidden文件中的文件。如果您正在编写脚本,则这些行将读取.hidden文件并显示存在的文件名。

if [[ -f .hidden]] # if '.hidden' exists and is a file

then

while read filename # read file name from line

do

if [[ -e "$filename" ]] # if the read file name exists

then

echo "$filename" # print it

fi

done < .hidden # read from .hidden file

fi

第七种思路

你还可以做些什么,is ls .?*或ls .!(|),它会向你展示当前目录下隐藏的文件/目录和下面的其他文件/目录中的所有内容

例如:从我的终端

$ ls .?*

.bash_history .dmrc .macromedia .weather

.bash_logout .gksu.lock .profile .wgetrc

.bash_profile .bashrc.save .ICEauthority .toprc .Xauthority

.bashrc .lastdir .viminfo .xsession-errors

.bashrc~ .dircolors .lynxrc .vimrc .xsession-errors.old

..:

Baron

.adobe:

Flash_Player

.aptitude:

cache config

.cache:

compizconfig-1 rhythmbox

dconf shotwell

现在注意上面的结果,它显示了每个文件/目录及其子目录和任何隐藏文件。

[1:0:248][ebaron@37signals:pts/4][~/Desktop]

$ ls .!(|)

.bash_aliases .bashrc1 .bashrc1~

..:

askapache-bash-profile.txt examples.desktop Public top-1m.csv

backups Firefox_wallpaper.png PycharmProjects top-1m.csv.zip

Desktop java_error_in_PYCHARM_17581.log Shotwell Import Log.txt topsites.txt

Documents Music Templates Videos

Downloads Pictures texput.log vmware

对不起,我无法评论。以解释ls .?*和@cioby23答案ls -d .[!.]* .??*之间的差异为什么它实际上是两次打印隐藏文件是因为字面上你问了两次.??*,.?*,.[!.]*它们是同一件事,因此将它们中的任何一个添加到不同的命令字符将打印两次。

参考资料

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值