如何找到隐藏文件
我有一些文件,它们的名字是这样的
.abc efg.txt
.some other name has a dot in front.txt
......
,我想做这样的事情,
for i in `ls -a` ; do echo $i; done;
我期望结果应该是,
.abc efg.txt
.some other name has a dot in front.txt
但结果却是一团糟.. 我怎样才能得到那些隐藏文件???
谢谢
i have some files, they are named like this
.abc efg.txt
.some other name has a dot in front.txt
......
and i want to do something like this
for i in `ls -a` ; do echo $i; done;
i expected the result should be
.abc efg.txt
.some other name has a dot in front.txt
but it turns out a buch of mess..
how can i get those hidden file???
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要使用
ls
使用 shell 模式匹配:如果您想要所有文件、隐藏文件和普通文件,请执行以下操作:(
请注意,这也会为您提供
.
和..< /code>,如果您不想要这些文件,则必须将其过滤掉,另请注意,如果没有(隐藏)文件,则此方法会失败,在这种情况下,您还必须过滤掉
*
和.*
)如果您想要所有文件并且不介意使用
bash
特定选项,您可以通过设置dotglob
和nullglob
。dotglob
将使*
也找到隐藏文件(但不是.
和..
),nullglob<如果没有匹配的文件,/code> 将不会返回
*
。因此,在这种情况下,您不必进行任何过滤:Instead of using
ls
use shell pattern matching:If you want all files, hidden and normal do:
(Note that this will als get you
.
and..
, if you do not want those you would have to filter those out, also note that this approach fails if there are no (hidden) files, in that case you would also have to filter out*
and.*
)If you want all files and do not mind using
bash
specific options, you could refine this by settingdotglob
andnullglob
.dotglob
will make*
also find hidden files (but not.
and..
),nullglob
will not return*
if there are no matching files. So in this case you will not have to do any filtering:为了避免
.
和..
你可以这样做:这将打印你想要的内容。如果您需要执行
echo
以外的操作,只需将其作为exec
的参数即可。for fname in .*;执行 echo $fname; done;
也会打印.
和..
。to avoid
.
and..
you can do:This will print what you want. If you need to do something more than
echo
, just put it as an argument forexec
.for fname in .*; do echo $fname; done;
will print.
and..
as well.要查找隐藏文件,请使用 find:
将它们从结果中排除:
处理带有空格的整个文件的另一种方法是将它们视为行:
To find hidden files use find:
To exclude them from the result:
And another way to handle whole files with spaces is to treat them as lines: