Most of the time when you're listing the contents of the directory, you're interested in more than just a plain list of the files and directories.
Some distributions have handy (although inconsistent) aliases for ls
command (like ll
) out of the box. But before we get into this, a quick reminder of what is an alias
, and how they work on Linux.
Basically, an alias in Linux is just a quick shortcut to a longer command (or a set of commands).
If you run alias
command (without any arguments) on any modern distribution, there's a very good chance that you'll see something like this:
alias ls='ls --color=auto'
This means that whenever you use ls
in your shell, it will be automatically expanded to ls --color=auto
(which will automatically add extra escape sequences representing colors to the terminal, but only if standard output is connected (--color=always
would force this behavior regardless of the connected output)).
Another way to confirm that ls
is an alias to run type ls
:
ls is aliased to `ls --color=auto'
If you want to run ls
command directly, you can prefix it with command
:
command ls ...
or just \
:
\ls ...
Defining aliases
The output above returned from the alias
command is the exact syntax for defining an alias, i.e. executing alias ls='ls --color=auto'
will re(define) the ls
alias for the current shell.
But since you haven't defined this ls
alias yourself, where does it come from, and where can you add aliases you want to use permanently?
On Debian-based distribution, this ls
alias is most likely defined in your ~/.bashrc
file (which was copied from /etc/skel/.bashrc
file on account creation):
if [ -x /usr/bin/dircolors ]; then
...
alias ls='ls --color=auto'
...
fi
(/etc/skel/.bashrc
is a part of bash
package)
On Fedora (and some other rpm-based distributions), it's globally defined (for all users) in /etc/profile.d/colorls.sh
:
alias ls='ls --color=auto' 2>/dev/null
(/etc/profile.d/colorls.sh
is a part of coreutils-common
package)
Additional aliases
On Debian, /etc/skel/.bashrc
contains a few more ls-related aliases:
#alias ll='ls -l'
#alias la='ls -A'
#alias l='ls -CF'
(since the lines are commented out, you need to uncomment them to make them active)
The Ubuntu version looks like this:
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
(unlike on Debian, they are active by default)
On Fedora, it looks more like this:
alias ll='ls -l --color=auto' 2>/dev/null
alias l.='ls -d .* --color=auto' 2>/dev/null
alias ls='ls --color=auto' 2>/dev/null
Since this is very inconsistent across different distributions (and even versions), it's a good idea to define your own aliases, instead of relying on the provided ones.
Examples
Let's explore this on an example directory with a few files and directories:
for i in {1..3}; do
echo $i > "f$i"
mkdir "d$i"
done
(if you're using zsh, you can use repeat 3 {...}
instead of for
loop).
Plain ls
command doesn't really give us any info besides the names:
d1 d2 d3 f1 f2 f3
Long list
ls -l
(long list format) gives us much more info:
total 24
drwxr-xr-x 2 n n 4096 Aug 18 15:16 d1
drwxr-xr-x 2 n n 4096 Aug 18 15:16 d2
drwxr-xr-x 2 n n 4096 Aug 18 15:16 d3
-rw-r--r-- 1 n n 2 Aug 18 15:16 f1
-rw-r--r-- 1 n n 2 Aug 18 15:16 f2
-rw-r--r-- 1 n n 2 Aug 18 15:16 f3
If you're not using any other aliases, I recommend at least defining ll
. These few characters may not seem a big deal, but they are when you're using this command all the time.
Ubuntu's version has two additional flags - ls -alF
:
total 32
drwxr-xr-x 5 n n 4096 Aug 18 15:16 ./
drwxr-xr-x 26 n n 4096 Aug 17 15:26 ../
drwxr-xr-x 2 n n 4096 Aug 18 15:16 d1/
drwxr-xr-x 2 n n 4096 Aug 18 15:16 d2/
drwxr-xr-x 2 n n 4096 Aug 18 15:16 d3/
-rw-r--r-- 1 n n 2 Aug 18 15:16 f1
-rw-r--r-- 1 n n 2 Aug 18 15:16 f2
-rw-r--r-- 1 n n 2 Aug 18 15:16 f3
(-a
tells ls to show us all files (including the .
and ..
, and files starting with a dot); -F
appends an indicator (i.e. */=>@|) to each entry)
My personal preference is to list nearly all files (without .
and ..
, -A
flag instead of -a
), and to sort them by time:
alias ll='ls -AltF'
Where I should put my alias definition?
The simplest way is to just put them in your ~/.bashrc
file, but very often, you'll already have something like this in your ~/.bashrc
file:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
If not, it's a good idea to add it. This way, you can organize all your aliases in the same place.
You can do the same for zsh by sourcing ~/.zsh_aliases
from your ~/.zshrc
.
Note: this is a wiki article from BetterWays.dev Linux series, you can find the latest (better formatted) version here: https://onethingwell.dev/ll-ls-and-related-aliases
Top comments (0)