问题
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 \.\..
回答1:
Read ls(1) documentation (perhaps with man ls
). At least, take the habit of trying
ls --help
or better yet (since ls
might be aliased, e.g. in your ~/.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 .*
)
回答2:
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:
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 (!)
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:
ls -d1A .* | tail -n +3
来源:https://stackoverflow.com/questions/22407480/command-to-list-all-files-except-dot-and-dot-dot