53

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 \.\..
jww
  • 97,681
  • 90
  • 411
  • 885
nondoo
  • 601
  • 1
  • 5
  • 13

3 Answers3

69

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
mgutt
  • 5,867
  • 2
  • 50
  • 77
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
5
$ ls -lA

works best for my needs.

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

alias ll='ls -lA'
Alan
  • 411
  • 6
  • 18
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
Community
  • 1
  • 1
runlevel0
  • 2,715
  • 2
  • 24
  • 31
  • BTW, as root nothing happens except an error message "cannot remove" but still, better to be over-cautious with production servers. – runlevel0 Jun 01 '16 at 10:51