57

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 \.\..

3 Answers 3

81

Regarding the ls(1) documentation (man ls):

-A, --almost-all do not list implied . and ..

you need (without any additional argument such as .*):

ls -A

or better yet:

/bin/ls -A
Sign up to request clarification or add additional context in comments.

3 Comments

How come /bin/ls -A excludes the dot directories, but ls -A does not? At least, that is the case on my system, which runs a distro based on Ubuntu 14.04.5.
@AndyForceno: probably ls is some alias (or shell function) e.g. in your .bashrc
\ls -A to disable ls alias, but /bin/ls is bullet proof :)
7
$ ls -lA

works best for my needs.

For convenience I recommend to define an alias within .bashrc-file as follows:

alias ll='ls -lA'

Comments

4

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

1 Comment

BTW, as root nothing happens except an error message "cannot remove" but still, better to be over-cautious with production servers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.