Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. UNIX(Linux)Beginner - Advanced ls Command (Part 1) and Dotfiles

Advanced ls Command (Part 1) and Dotfiles - Images: Japanese

Hey there, everyone!

Let's continue with more on the ls command and an intro to dot files.

Quick recap first: 'ls' lists the contents of the current directory (the current working directory). It comes with a few options, so let's go through those.

Starting from the root directory /, a plain ls looks like this.

[root@localhost /]# ls
bin   dev  home  lib64       media  opt   root  selinux  sys  usr
boot  etc  lib   lost+found  mnt    proc  sbin  srv      tmp  var

A quick scrolling list of directories and files.

Adding '-1' (the number one) makes it output one item per line. Note: '-2' and other numbers don't work — only '-1' does.

[root@localhost /]# ls -1
bin
boot
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
sbin
selinux
srv
sys
tmp
usr
var

Adding -R outputs the contents of subdirectories recursively — and then the contents of those subdirectories, and so on. Let's try it.

Say we're in '/root', and inside it we've created a 'test' directory, inside that another 'test' directory, and inside that yet another 'test' directory. A plain 'ls' shows this:

[root@localhost ~]# ls
test

But with ls -R:

[root@localhost ~]# ls -R
.:
test

./test:
test

./test/test:
test

./test/test/test:

Every nested directory is listed. Useful when you want to understand the full structure of a directory tree.

As mentioned briefly in the previous article, the 'tree' command is a handy alternative to 'ls -R' for visualizing directory structure. Its output looks like this.

[root@localhost var/www]# tree
.
├── index.html
├── test
    └── index.html

It shows the structure as an actual tree. The author tends to find tree more readable than ls -R for this purpose.

That said, 'tree' isn't installed by default on most systems. If you can't install it due to permission constraints, use 'ls -R' instead — it's a core command that's always available.

Next: '-F'. This appends a 'type indicator' after each filename or directory name in the output, indicating what kind of data each entry is.

(nothing)Regular file
*Executable file
@Symbolic link
/Directory

Symbolic link is roughly equivalent to a shortcut in Windows.

Let's try 'ls -F' somewhere interesting. It's most informative in a directory full of executable files, so let's look at '/bin' — the home of core UNIX commands.

Here's a technique we haven't covered yet: 'ls' can take a path as its first argument. Instead of navigating to a directory with 'cd' and then running 'ls', you can pass the path directly to 'ls' and it'll list that directory's contents from wherever you are.

Let's use it. We'll move to the home directory first (for no particular reason — just to demonstrate).

[root@localhost bin]# cd
[root@localhost ~]# pwd
/root

From here, 'ls -F /bin' lists the contents of '/bin' with type indicators.

[root@localhost bin]# cd
[root@localhost ~]# pwd
/root
[root@localhost ~]# ls -F /bin
arch*           fgrep@               mktemp*         sort*
awk@            find*                more*           stty*
basename*       findmnt*             mount*          su*
bash*           fusermount*          mountpoint*     sync*
cat*            gawk*                mv*             tar*
chgrp*          grep*                netstat*        taskset*
chmod*          gtar@                nice*           touch*
chown*          gunzip*              nisdomainname@  tracepath*
cp*             gzip*                ping*           tracepath6*
cpio*           hostname*            ping6*          true*
cut*            ipcalc*              plymouth*       ulockmgr_server*
dash*           iptables-xml@        ps*             umount*
date*           iptables-xml-1.4.7@  pwd*            uname*
dd*             kbd_mode*            raw*            unicode_start*
df*             kill*                readlink*       unicode_stop*
dmesg*          link*                rm*             unlink*
dnsdomainname@  ln*                  rmdir*          usleep*
domainname@     loadkeys*            rpm*            vi*
dumpkeys*       logger*              rvi@            view@
echo*           login*               rview@          ypdomainname@
egrep@          ls*                  sed*            zcat*
env*            lsblk*               setfont*
ex@             mkdir*               sh@
false*          mknod*               sleep*

The contents of '/bin' were listed from the home directory, and each entry has a type indicator at the end. Everything with a '*' is an executable, and entries with '@' are symbolic links.

That said — modern UNIX-based OSes already color-code 'ls' output by type, so type indicators via '-F' are rarely needed anymore. After spending so much time on it, the honest assessment is: you can probably ignore '-F'. The path argument for 'ls', on the other hand, is worth remembering.

'ls' can take multiple path arguments at once. When you do, it lists each one in turn. Here's an example with '/bin', '/etc', and '/var'.

[root@localhost ~]# ls /bin /etc /var
/bin:
arch           echo                kill           ping6     tar
awk            egrep               link           plymouth  taskset
basename       env                 ln             ps        touch
bash           ex                  loadkeys       pwd       tracepath
cat            false               logger         raw       tracepath6
chgrp          fgrep               login          readlink  true
chmod          find                ls             rm        ulockmgr_server
chown          findmnt             lsblk          rmdir     umount
cp             fusermount          mkdir          rpm       uname
cpio           gawk                mknod          rvi       unicode_start
cut            grep                mktemp         rview     unicode_stop
dash           gtar                more           sed       unlink
date           gunzip              mount          setfont   usleep
dd             gzip                mountpoint     sh        vi
df             hostname            mv             sleep     view
dmesg          ipcalc              netstat        sort      ypdomainname
dnsdomainname  iptables-xml        nice           stty      zcat
domainname     iptables-xml-1.4.7  nisdomainname  su
dumpkeys       kbd_mode            ping           sync

/etc:
DIR_COLORS               filesystems     modprobe.d      rpc
DIR_COLORS.256color      fstab           motd            rpm
DIR_COLORS.lightbgcolor  gai.conf        mtab            rsyslog.conf
NetworkManager           gcrypt          multipath       rsyslog.d
X11                      gnupg           my.cnf          rwtab
adjtime                  group           networks        rwtab.d
aliases                  group-          nsswitch.conf   sasl2
aliases.db               grub.conf       openldap        securetty
alternatives             gshadow         opt             security
anacrontab               gshadow-        pam.d           selinux
audisp                   host.conf       passwd          services
audit                    hosts           passwd-         sestatus.conf
bash_completion.d        hosts.allow     pkcs11          shadow
bashrc                   hosts.deny      pki             shadow-
blkid                    init            plymouth        shells
centos-release           init.conf       pm              skel
chkconfig.d              init.d          popt.d          ssh
cron.d                   inittab         postfix         ssl
cron.daily               inputrc         ppp             statetab
cron.deny                iproute2        prelink.conf.d  statetab.d
cron.hourly              iscsi           printcap        sudo-ldap.conf
cron.monthly             issue           profile         sudo.conf
cron.weekly              issue.net       profile.d       sudoers
crontab                  krb5.conf       protocols       sudoers.d
crypttab                 ld.so.cache     rc              sysconfig
csh.cshrc                ld.so.conf      rc.d            sysctl.conf
csh.login                ld.so.conf.d    rc.local        sysctl.d
dbus-1                   libaudit.conf   rc.sysinit      system-release
default                  libuser.conf    rc0.d           system-release-cpe
depmod.d                 localtime       rc1.d           terminfo
dhcp                     login.defs      rc2.d           udev
dracut.conf              logrotate.conf  rc3.d           virc
dracut.conf.d            logrotate.d     rc4.d           xdg
environment              lvm             rc5.d           xinetd.d
ethers                   magic           rc6.d           yum
exports                  makedev.d       redhat-release  yum.conf
favicon.png              mke2fs.conf     resolv.conf     yum.repos.d

/var:
cache  empty  lib    lock  mail  opt       run    tmp
db     games  local  log   nis   preserve  spool  yp

Pretty handy — give it a try.

Now let's look at '-a'. This is a high-frequency option. It makes 'ls' also show 'dot files'.

"What's a dot file?" — exactly what it sounds like: a file whose name starts with '.' (a dot). In UNIX-based OSes, files that shouldn't be casually touched — configuration files, for example — are conventionally named with a leading dot, which makes them hidden. So "dot file = hidden file" in UNIX terms.

Let's try it. Here's the output of ls -a at the root directory.

[root@localhost /]# ls -a
.          bin   etc   lib64       mnt   root     srv  usr
..         boot  home  lost+found  opt   sbin     sys  var
.autofsck  dev   lib   media       proc  selinux  tmp

And here's 'ls' without '-a'.

[root@localhost /]# ls
bin   dev  home  lib64       media  opt   root  selinux  sys  usr
boot  etc  lib   lost+found  mnt    proc  sbin  srv      tmp  var

Comparing the two: '.', '..', and '.autofsck' appear in the '-a' output but not without it.

'.autofsck' is the dot file here. The '.' and '..' entries are the same ones from the 'cd' article — '.' is the current directory, '..' is the parent directory.

"But the root directory is the very top — why does 'ls -a' at root show '..' for a parent that doesn't exist?" — that's a fair observation. The author has wondered about it too and looked into it before, but never found a clear answer. Just treat it as: "at the root directory, '..' also points to the root directory itself." Not something to worry about.

On the topic of '.autofsck' specifically: it's a file that UNIX-based OSes generate when the OS starts up and delete when the OS shuts down normally. On the next boot, if '.autofsck' already exists, the OS knows the previous shutdown wasn't clean. Side note — not something you need to memorize.

There's also a '-A' option (uppercase A). It behaves like '-a' but excludes the '.' and '..' entries from the output. If those two get in the way, '-A' is an option. In practice, '.' and '..' are rarely in the way, so '-a' is usually all you need.

By the way — a casual internal note: '-A' exists, but you'll almost never need it. Just remember '-a'.

Alright, that got a little long. Let's stop here and pick up with more ls content in the next article. See you there!

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .