如何找到隐藏文件

发布于 2024-10-08 18:43:20 字数 336 浏览 8 评论 0原文

我有一些文件,它们的名字是这样的

.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三生殊途 2024-10-15 18:43:20

不要使用 ls 使用 shell 模式匹配:

for i in .* ; do echo $i; done;

如果您想要所有文件、隐藏文件和普通文件,请执行以下操作:(

for i in * .* ; do echo $i; done;

请注意,这也会为您提供 ...< /code>,如果您不想要这些文件,则必须将其过滤掉,另请注意,如果没有(隐藏)文件,则此方法会失败,在这种情况下,您还必须过滤掉 *.*

如果您想要所有文件并且不介意使用 bash 特定选项,您可以通过设置 dotglobnullglobdotglob 将使 * 也找到隐藏文件(但不是 ...),nullglob<如果没有匹配的文件,/code> 将不会返回 *。因此,在这种情况下,您不必进行任何过滤:

shopt -s dotglob nullglob
for i in * ; do echo $i; done;

Instead of using ls use shell pattern matching:

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 .*)

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:

shopt -s dotglob nullglob
for i in * ; do echo $i; done;
不气馁 2024-10-15 18:43:20

为了避免 ... 你可以这样做:

find . -name ".*" -type f -maxdepth 1 -exec basename {} ";"

这将打印你想要的内容。如果您需要执行 echo 以外的操作,只需将其作为 exec 的参数即可。

for fname in .*;执行 echo $fname; done; 也会打印 ...

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.

for fname in .*; do echo $fname; done; will print . and .. as well.

无力看清 2024-10-15 18:43:20

要查找隐藏文件,请使用 find:

find . -wholename "./\.*"

将它们从结果中排除:

find . -wholename "./\.*" -prune -o -print

处理带有空格的整个文件的另一种方法是将它们视为行:

ls -1a | while read aFileName
do
  echo $aFileName
done

To find hidden files use 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文