23

The best I could find via Google/man ls was:

ls -l | grep -v '^total'

Is there something I'm missing in the flags to ls?

Also, why is the total size in kb, regardless of the -h flag? scratch that, seems like it does, not sure what I was seeing then.

4
  • 5
    Perhaps a little bit more efficient could be ls -l | tail -n+2, for the rest I do not have an answer.
    – enzotib
    Sep 13, 2011 at 9:25
  • I like enzotib's method more. Suggestion: set it up as an alias and name it lsl. Just 3 letters ;) oh and ls -lh shows me 1.2K styled sizes.
    – Rinzwind
    Sep 13, 2011 at 9:30
  • I think you've gotten the easiest way.
    – Robert
    Sep 23, 2018 at 14:09
  • Lots of good suggestions here for the general case of removing any first line of an output
    – prosoitos
    Jun 27, 2020 at 18:11

6 Answers 6

22

Looking in the source code of coreutils, I found out that total will always be displayed when using the -l option on directories.

Using the -d option to list entries instead of directory contents hides total. But if you run that without arguments (or on a directory), it'll just show the directory and not its contents. Therefore, you need wildcards. * matches all files and .* matches hidden files as well (which corresponds with the -a option):

ls -ld * .*

As for the -h option, it works for me. 1118360 bytes show up as 1.1M. Files smaller than 1024 show up in bytes.

3
  • 1
    actually -d lists directories instead of entries.
    – H2ONaCl
    Sep 21, 2013 at 5:39
  • @broiyan You can take the ls manual page to the letter or consider this: ls / can be interpreted as "list the entry / if it is a file or the directory contents of / if it is a directory". Using -d removes the last condition and will always print the entry instead of directory contents. This applies no matter if a path is a file or directory.
    – Lekensteyn
    Sep 21, 2013 at 9:41
  • I'd upvote this again if i could
    – a2f0
    Mar 17, 2018 at 22:04
10

Using wildcards to avoid having ls running the directory listing is suboptimal, because it prevents you from using ls options like --almost-all.

Like Enzotib's suggestion, the simplest way to remove it is to pipe it through tail to chop off the first line. However, ls will detect that it its output is a pipe rather than interactive, and change its defaults in an unwanted way. Hence, to make it robust, you should also add some options:

  • --color=always: keep showing colors
  • --hide-control-chars: print ? in filenames in place of control characters that could mess up the console output

I have a script ~/bin/l (you could also use a Bash alias in ~/.bash_aliases):

#!/bin/bash
ls -l --color=always --hide-control-chars "$@" | tail --lines=+2

You can also add any other ls options you want by default, e.g. --group-directories-first --time-style='+%FT%T.%N%:::z' --indicator-style=slash.

1
  • However, this won't work if you if you pass multiple filenames to ls.
    – Sparhawk
    Apr 26, 2017 at 3:57
4

ls -l *

Will give you "normally named" folder contents without the total. That might be suitable for your needs. Unfortunately, it will not give you the dot prefixed files. If you use ls -l * .* you will get a mess since linux will navigate up the tree.

Note the dot prefix could mean the folder or with a wildcard it means the above folder so it does not help you to pick regular files that are dot prefixed. This seems to have been an unfortunate design decision.

1
  • Even ls -l * will list contents of the directories, not the directories themselves.
    – x-yuri
    Sep 12, 2021 at 14:23
3

$ ls -l [DIRECTORY] | sed 1d
(output contains file names)

$ ls -ld [path-to-dir]/*
(output contains relative paths but will preserve colors)

$ ls -l [DIRECTORY] | tail -n +2

$ ls -l [DIRECTORY] | awk 'NR>=1'


Note:
The following doesn't work if you have a file named "total...."

$ ls -l | grep -v '^total'

1
  • ...or if you have a non-English locale: LANG=ja_JP.utf8 ls -al | head -n 1 (given that the locale is installed).
    – x-yuri
    Sep 12, 2021 at 14:17
1

To answer your exact question would be ls -1A . which displays files, directories, and hidden files/directories, does not use any pipes or external tools (grep/tr/cut..), and does not rely on turning on or off any shell globbing options such as extglob, globstar, or nullglob. I have tested it to verify that statement.

-1    list one file per line.  Avoid '\n' with -q or -b
-A,   --almost-all           do not list implied . and ..

Example output:

user@host:/dir$ ls -1A .
temp
terraform
.test
tor
torrent-stuff
.Trash-1000
virtual_appliance
visualblind
vpn-ipv4.txt
vpn-ipv6.txt
zztestfile
.zztestfile2

As a side note, here is what I keep in my .bash_aliases relating to listing dirs/files:

# ls directories
alias lsd2='ls -d */'
alias lsd3='ls -p . | grep -E "/$" | tr -d "/"'
alias lsd4='ls -lA | grep -E "^d"'
alias lsd5='ls -pv . | grep -E "/$" | tr -d "/"'
alias lsd='ls -lAd */'
# ls files
alias lsf='ls -pA | grep -vE "/$"'
alias lsf2='ls -p | grep -v "/"'
alias lsf3='ls -p . | grep "/" | tr -d "/"'
alias lsf4='ls -lA | grep -vE "^d"'
# ls normal
alias lsnormal='ls -lAh1d *'
alias lsnormal2='ls -1 .'
0

ls does not seem to accept R parameter when -1 is specified. It also does not take location argument (if you are in the /home directory, the command will not return / root content)

sudo ls -l1diR *
     13 lrwxrwxrwx   1 root root     7 Հլս  8 23:58 bin -> usr/bin
 131073 drwxr-xr-x   4 root root  4096 Հլս  9 00:30 boot
2228225 drwxrwxr-x   2 root root  4096 Հլս  9 00:00 cdrom
      1 drwxr-xr-x  21 root root  4300 Հլս 12 08:46 dev
1572865 drwxr-xr-x 140 root root 12288 Հլս 12 09:06 etc
1179649 drwxr-xr-x   3 root root  4096 Հլս  9 00:01 home
     14 lrwxrwxrwx   1 root root     7 Հլս  8 23:58 lib -> usr/lib
     15 lrwxrwxrwx   1 root root     9 Հլս  8 23:58 lib32 -> usr/lib32
     16 lrwxrwxrwx   1 root root     9 Հլս  8 23:58 lib64 -> usr/lib64
     17 lrwxrwxrwx   1 root root    10 Հլս  8 23:58 libx32 -> usr/libx32
     11 drwx------   2 root root 16384 Հլս  8 23:58 lost+found
 524289 drwxr-xr-x   3 root root  4096 Հլս  9 00:11 media
 262145 drwxr-xr-x   2 root root  4096 Ապր 15 03:27 mnt
2097153 drwxr-xr-x   4 root root  4096 Հլս 10 01:39 opt
      1 dr-xr-xr-x 325 root root     0 Հլս 12 08:46 proc
1310721 drwx------   4 root root  4096 Հլս  9 10:36 root
      1 drwxr-xr-x  35 root root   940 Հլս 12 09:07 run
     18 lrwxrwxrwx   1 root root     8 Հլս  8 23:58 sbin -> usr/sbin
 393217 drwxr-xr-x  11 root root  4096 Հլս 10 01:25 snap
1835009 drwxr-xr-x   2 root root  4096 Ապր 15 03:27 srv
      1 dr-xr-xr-x  13 root root     0 Հլս 12 08:46 sys
1966081 drwxrwxrwt  19 root root  4096 Հլս 12 09:09 tmp
1310722 drwxr-xr-x  14 root root  4096 Ապր 15 03:27 usr
 655361 drwxr-xr-x  14 root root  4096 Ապր 15 03:32 var

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .