Linux 如何找到隐藏文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4482288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to find hidden file
提问by jojo
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
谢谢
采纳答案by Peter van der Heijden
Instead of using ls
use shell pattern matching:
而不是使用ls
使用 shell 模式匹配:
for i in .* ; do echo $i; done;
If you want all files, hidden and normal do:
如果你想要所有文件,隐藏和正常的:
for i in * .* ; do echo $i; done;
(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 .*
)
(注意,这将ALS让你.
和..
,如果你不希望这些你必须过滤那些出来,还需要注意的是,如果没有(隐藏)文件,这种方法失败,在这种情况下,你也必须过滤掉*
和.*
)
If you want all files and do not mind using bash
specific options, you could refine this by setting dotglob
and nullglob
. 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:
如果您想要所有文件并且不介意使用bash
特定选项,您可以通过设置dotglob
和nullglob
. dotglob
也会*
找到隐藏文件(但不是.
和..
),如果没有匹配的文件,nullglob
则不会返回*
。因此,在这种情况下,您无需进行任何过滤:
shopt -s dotglob nullglob
for i in * ; do echo $i; done;
回答by khachik
to avoid .
and ..
you can do:
避免.
,..
你可以这样做:
find . -name ".*" -type f -maxdepth 1 -exec basename {} ";"
This will print what you want. If you need to do something more than echo
, just put it as an argument for exec
.
这将打印您想要的内容。如果您需要做的不仅仅是echo
,只需将其作为exec
.
for fname in .*; do echo $fname; done;
will print .
and ..
as well.
for fname in .*; do echo $fname; done;
将打印.
,..
以及。
回答by kon5ad
To find hidden files use find:
要查找隐藏文件,请使用 find:
find . -wholename "./\.*"
To exclude them from the result:
要将它们从结果中排除:
find . -wholename "./\.*" -prune -o -print
And another way to handle whole files with spaces is to treat them as lines:
处理带有空格的整个文件的另一种方法是将它们视为行:
ls -1a | while read aFileName
do
echo $aFileName
done