命令列出除以外的所有文件。 (点)和..(点点) - Linux

时间:2022-11-18 16:58:17

I'm trying to find a command that would list all files (including hidden files), but must exclude the current directory and parent directory. Please help.

我正在尝试查找列出所有文件(包括隐藏文件)的命令,但必须排除当前目录和父目录。请帮忙。

   $ ls -a \.\..

2 个解决方案

#1


15  

Read ls(1) documentation (perhaps with man ls). At least, take the habit of trying

阅读ls(1)文档(也许与man ls)。至少,要养成尝试的习惯

ls --help

or better yet (since ls might be aliased, e.g. in your ~/.bashrc)

或更好(因为ls可能是别名,例如在你的〜/ .bashrc中)

/bin/ls --help

You'll get something starting with:

你会得到一些东西:

Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all                  do not ignore entries starting with .
  -A, --almost-all           do not list implied . and ..
      --author               with -l, print the author of each file
  -b, --escape               print C-style escapes for nongraphic characters

etc....

等等....

You want ls -A or better yet /bin/ls -A (without any additional argument such as .*)

你想要ls -A或更好的/ bin / ls -A(没有任何额外的参数,如。*)

#2


3  

I have a situation where I want to remove a series of dot-directories. In my servers we mark directories for removal adding a dot and certain other text patterns (timestamp) for automated removal. Sometimes I need to do that manually.

我有一种情况,我想删除一系列的点目录。在我的服务器中,我们标记要删除的目录,添加一个点和某些其他文本模式(时间戳)以便自动删除。有时候我需要手动完成。

As I commented to Basile Starynkevitch's reply, when you use a globbing pattern like the one below the -A switch loses its function and works just as -a:

正如我对Basile Starynkevitch的回复所评论的那样,当你使用类似下面的一个模式时,-A开关失去了它的功能并且像-a一样工作:

 runlevel0@ubuntu:~/scripts$ ls -1dA .*
.
..
.comparepp.sh.swp

It would most certainly give an error if I try to remove files as a user, but I just don't want to think what could happen as root (!)

如果我尝试以用户身份删除文件,它肯定会出错,但我只是不想想以root身份发生什么(!)

My approach in this case is:

我在这种情况下的方法是:

for dir in $(ls -1ad .* | tail -n +3) ; do rm -rfv $dir  ; done

I tail out the 2 first line containing the dots as you can see. To tailor the answer to the question asked this would do the job:

我可以看到包含点的第一行2。为了定制问题的答案,这可以做到这一点:

ls -d1A .* | tail -n +3

#1


15  

Read ls(1) documentation (perhaps with man ls). At least, take the habit of trying

阅读ls(1)文档(也许与man ls)。至少,要养成尝试的习惯

ls --help

or better yet (since ls might be aliased, e.g. in your ~/.bashrc)

或更好(因为ls可能是别名,例如在你的〜/ .bashrc中)

/bin/ls --help

You'll get something starting with:

你会得到一些东西:

Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all                  do not ignore entries starting with .
  -A, --almost-all           do not list implied . and ..
      --author               with -l, print the author of each file
  -b, --escape               print C-style escapes for nongraphic characters

etc....

等等....

You want ls -A or better yet /bin/ls -A (without any additional argument such as .*)

你想要ls -A或更好的/ bin / ls -A(没有任何额外的参数,如。*)

#2


3  

I have a situation where I want to remove a series of dot-directories. In my servers we mark directories for removal adding a dot and certain other text patterns (timestamp) for automated removal. Sometimes I need to do that manually.

我有一种情况,我想删除一系列的点目录。在我的服务器中,我们标记要删除的目录,添加一个点和某些其他文本模式(时间戳)以便自动删除。有时候我需要手动完成。

As I commented to Basile Starynkevitch's reply, when you use a globbing pattern like the one below the -A switch loses its function and works just as -a:

正如我对Basile Starynkevitch的回复所评论的那样,当你使用类似下面的一个模式时,-A开关失去了它的功能并且像-a一样工作:

 runlevel0@ubuntu:~/scripts$ ls -1dA .*
.
..
.comparepp.sh.swp

It would most certainly give an error if I try to remove files as a user, but I just don't want to think what could happen as root (!)

如果我尝试以用户身份删除文件,它肯定会出错,但我只是不想想以root身份发生什么(!)

My approach in this case is:

我在这种情况下的方法是:

for dir in $(ls -1ad .* | tail -n +3) ; do rm -rfv $dir  ; done

I tail out the 2 first line containing the dots as you can see. To tailor the answer to the question asked this would do the job:

我可以看到包含点的第一行2。为了定制问题的答案,这可以做到这一点:

ls -d1A .* | tail -n +3