In this guide, we are going to learn How to use ls Command on Linux. On Linux and other Unix-based operating systems, the ls command is used to list files or directories.
The ls tool, like the File Explorer or Finder, allows you to list all files or folders in the current directory by default, and then interact with them via the command line.
How to use ls Command on Linux
ls is an abbreviation for “list” The ls command is utilized to list files or directories on Linux and other operating systems built on the Unix platform. In the Linux terminal, ls is one of the most frequently used commands. There are numerous ls commands with diverse functions.
The ls command is one type of Linux Directory command. Also shows a directory’s entire contents.
ls
Debian.dpkg dir1 dir2 dir3 doc.txt kifa.txt mac tec.txt tutor.txt
The command ls -a
In general, a user cannot see hidden files on Linux. The hidden files begin with a .(dot) and are not visible in the normal directory. The ls -a command comes in handy here. This command displays the whole contents of the current directory, including hidden files.
ls -a
. .. Debian.dpkg dir1 dir2 dir3 doc.txt kifa.txt mac tec.txt tutor.txt
The Command ls -l
As previously stated, the ls command just displays the files. But what if the user wishes to access the files’ contents? Users must use the ls -l command to obtain that particular information. The file will be shown in the form of a long list. The contents are displayed column by column in the long list format.
If the contents have seven distinct properties, the attributes will be shown in seven distinct columns.
As an example,
- The first column contains information about file permissions.
- The total number of associations with the list is specified in the second column.
- The third and fourth columns contain information about the owner and group.
- The entire file size is specified in the fifth column (in Bytes).
- The sixth column displays the most recently updated date and time.
- The file/directory name is displayed in the seventh column.
ls -l
total 16
-rw-rw-r-- 1 ezra ezra 0 Ful 30 22:20 Debian.dpkg
drwxrwxr-x 3 ezra ezra 4096 Ful 30 22:05 dir1
drwxrwxr-x 3 ezra ezra 4096 Ful 30 22:05 dir2
drwxrwxr-x 3 ezra ezra 4096 Ful 30 22:25 dir3
-rw-rw-r-- 1 ezra ezra 0 Ful 30 22:19 doc.txt
-rw-rw-r-- 1 ezra ezra 0 Ful 30 22:11 kifa.txt
drwxrwxr-x 2 ezra ezra 4096 Ful 30 22:20 mac
-rw-rw-r-- 1 ezra ezra 0 Ful 30 22:11 tec.txt
-rw-rw-r-- 1 ezra admin 0 Ful 30 22:06 tutor.txt
List the sizes of the files
To list files or directories and their sizes, use the ls -s command (the s is lowercase).
ls -s
total 16
0 Debian.dpkg 4 dir1 4 dir2 4 dir3 0 doc.txt 0 kifa.txt 4 mac 0 tec.txt 0 tutor.txt
To sort files, use ls -S (the S uppercase).
ls -S
dir1 dir2 dir3 mac Debian.dpkg doc.txt kifa.txt tec.txt tutor.txt
List files having readable file sizes in long format
To list the files or directories in the same table format as previously, use the ls -lh command, but with an additional column denoting the size of each file/directory:
ls -lh
total 16K
-rw-rw-r-- 1 ezra ezra 0 Ful 30 22:20 Debian.dpkg
drwxrwxr-x 3 ezra ezra 4.0K Ful 30 22:05 dir1
drwxrwxr-x 3 ezra ezra 4.0K Ful 30 22:05 dir2
drwxrwxr-x 3 ezra ezra 4.0K Ful 30 22:25 dir3
-rw-rw-r-- 1 ezra ezra 0 Ful 30 22:19 doc.txt
-rw-rw-r-- 1 ezra ezra 0 Ful 30 22:11 kifa.txt
drwxrwxr-x 2 ezra ezra 4.0K Ful 30 22:20 mac
-rw-rw-r-- 1 ezra ezra 0 Ful 30 22:11 tec.txt
-rw-rw-r-- 1 ezra admin 0 Ful 30 22:06 tutor.txt
The Command ls -d */
This command is used when you just want to display directories and not any other files.
ls -d */
Box/ dir/ emptydir/ job/ newcopy/ permit/ projects/ sam/ shared-directory/ shell.rr/ testdir/
List of files and folders
To list the contents of the directory and its subdirectories, use the ls * command:
ls *
opt:
foxitsoftware
Pictures:
Deepin 'MISP CR' 'Netwok Security'
Public:
snap:
skype snap-store teams teams-for-linux
Templates:
Vagrant:
Videos:
AnyDesk
'VirtualBox VMs':
Alma-9 Oracle-9 Rocky-9 Ubuntu-20 windows-10 Windows-7
ybacklight:
LICENSE Makefile meson.build README.md src
List files in the root directory
To list the contents of the root directory, use the ls / command.
ls /
bin cdrom etc lib lib64 lost+found mnt proc run snap swapfile tmp var
boot dev home lib32 libx32 media opt root sbin srv sys usr
List the files in a different directory
To view the contents of another directory, enter the ls [directory path here] command.
ls [directory path]
i.e., in /etc directory, we use ls /etc
acpi chrony ethertypes hosts libccid_Info.plist mongod.conf.dpkg-old popularity-contest.conf security tlp.d
adduser.conf console-setup firefox hosts.allow libibverbs.d mtab ppp selinux tmpfiles.d
alsa cracklib fonts hosts.deny libnl-3 mtools.conf printcap sensors3.conf ubuntu-advantage
alternatives cron.d fprintd.conf hp libpaper.d mysql profile sensors.d ucf.conf
anacrontab cron.daily fstab i3 libreoffice nanorc profile.d services udev
anydesk cron.hourly fuse.conf i3blocks1.conf.bak libvirt netplan protocols sgml udisks2
apache2 cron.monthly fwupd i3blocks.conf locale.alias network pulse shadow ufw
apg.conf crontab gai.conf i3blocks.conf.bak locale.gen networkd-dispatcher python3 shadow- updatedb.conf
apm cron.weekly gamemode.ini i3status.conf localtime NetworkManager python3.8 shells update-manager
apparmor cups gconf ifplugd logcheck networks qemu-ifdown skel update-motd.d
apparmor.d cupshelpers gdb init login.defs newt qemu-ifup snmp update-notifier
apport dbus-1 gdm3 init.d logrotate.conf nsswitch.conf rc0.d speech-dispatcher UPower
ls -g Command
We use this command whenever you wish to display all other file information without the owner information column.
ls -g
total 16
-rw-rw-r-- 1 ezra 0 Ful 30 22:20 Debian.dpkg
drwxrwxr-x 3 ezra 4096 Ful 30 22:05 dir1
drwxrwxr-x 3 ezra 4096 Ful 30 22:05 dir2
drwxrwxr-x 3 ezra 4096 Ful 30 22:25 dir3
-rw-rw-r-- 1 ezra 0 Ful 30 22:19 doc.txt
-rw-rw-r-- 1 ezra 0 Ful 30 22:11 kifa.txt
drwxrwxr-x 2 ezra 4096 Ful 30 22:20 mac
-rw-rw-r-- 1 ezra 0 Ful 30 22:11 tec.txt
-rw-rw-r-- 1 admin 0 Ful 30 22:06 tutor.txt
ls -lG Command
We use this command if you wish to display all other file information without the group column. The group column will be excluded from the output.
ls -lG
total 16
-rw-rw-r-- 1 ezra 0 Ful 30 22:20 Debian.dpkg
drwxrwxr-x 3 ezra 4096 Ful 30 22:05 dir1
drwxrwxr-x 3 ezra 4096 Ful 30 22:05 dir2
drwxrwxr-x 3 ezra 4096 Ful 30 22:25 dir3
-rw-rw-r-- 1 ezra 0 Ful 30 22:19 doc.txt
-rw-rw-r-- 1 ezra 0 Ful 30 22:11 kifa.txt
drwxrwxr-x 2 ezra 4096 Ful 30 22:20 mac
-rw-rw-r-- 1 ezra 0 Ful 30 22:11 tec.txt
-rw-rw-r-- 1 ezra 0 Ful 30 22:06 tutor.txt
ls ~ Command
The ls ~ command shows the entire contents of the home directory.
ls ~
2022-09-20-10-00-58.053-VBoxSVC-7943.log id_rsa.pub nomachine_7.1.3_1_amd64.deb Public Templates
2022-09-20-20-40-20.012-VBoxSVC-9927.log Learn nomachine_8.1.2_1_amd64.deb saf tutor
Desktop Music nomachine_8.1.2_1_amd64.deb.1 snap Videos
Documents my-directory opt sudo 'VirtualBox VMs'
Downloads my-project Pictures teamviewer_amd64.deb
ls../ Command
With this command, you can get information about the parent directory. Assume there are two directories. The current directory is the “Downloads” directory, and the preceding directory, i.e., its parent directory, is the “Home” directory.
ls ../
bin Desktop Documents Downloads ffffffff Music opt Pictures Public snap Templates Vagrant Videos 'VirtualBox VMs' ybacklight
ls -F Command
If you use the -F option with the ls command, it will append a new character ‘/’ to the end of each directory.
ls -F
Debian.dpkg dir1/ dir2/ dir3/ doc.txt kifa.txt mac/ tec.txt tutor.txt
ls –help Command
This command will display documentation with all possible parameters for the ls command:
ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
-b, --escape print C-style escapes for nongraphic characters
--block-size=SIZE with -l, scale sizes by SIZE when printing them;
e.g., '--block-size=M'; see SIZE format below
-B, --ignore-backups do not list implied entries ending with ~
-c with -lt: sort by, and show, ctime (time of last
modification of file status information);
with -l: show ctime and sort by name;
otherwise: sort by ctime, newest first
-C list entries by columns
--color[=WHEN] colorize the output; WHEN can be 'always' (default
if omitted), 'auto', or 'never'; more info below
-d, --directory list directories themselves, not their contents
-D, --dired generate output designed for Emacs' dired mode
-f do not sort, enable -aU, disable -ls --color
-F, --classify append indicator (one of */=>@|) to entries
--file-type likewise, except do not append '*'
--format=WORD across -x, commas -m, horizontal -x, long -l,
single-column -1, verbose -l, vertical -C
--full-time like -l --time-style=full-iso
-g like -l, but do not list owner
--group-directories-first
group directories before files;
can be augmented with a --sort option, but any
use of --sort=none (-U) disables grouping
-G, --no-group in a long listing, don't print group names
-h, --human-readable with -l and -s, print sizes like 1K 234M 2G etc.
--si likewise, but use powers of 1000 not 1024
-H, --dereference-command-line
follow symbolic links listed on the command line
--dereference-command-line-symlink-to-dir
follow each command line symbolic link
that points to a directory
--hide=PATTERN do not list implied entries matching shell PATTERN
(overridden by -a or -A)
--hyperlink[=WHEN] hyperlink file names; WHEN can be 'always'
(default if omitted), 'auto', or 'never'
--indicator-style=WORD append indicator with style WORD to entry names:
none (default), slash (-p),
file-type (--file-type), classify (-F)
-i, --inode print the index number of each file
-I, --ignore=PATTERN do not list implied entries matching shell PATTERN
-k, --kibibytes default to 1024-byte blocks for disk usage;
used only with -s and per directory totals
-l use a long listing format
-L, --dereference when showing file information for a symbolic
ls -n Command
To list UID and GID in files. Use the ls -n command in the terminal to show the UID(unique identifier) and GID (global identifier).
ls -n
total 16
-rw-rw-r-- 1 1000 1000 0 Ful 30 22:20 Debian.dpkg
drwxrwxr-x 3 1000 1000 4096 Ful 30 22:05 dir1
drwxrwxr-x 3 1000 1000 4096 Ful 30 22:05 dir2
drwxrwxr-x 3 1000 1000 4096 Ful 30 22:25 dir3
-rw-rw-r-- 1 1000 1000 0 Ful 30 22:19 doc.txt
-rw-rw-r-- 1 1000 1000 0 Ful 30 22:11 kifa.txt
drwxrwxr-x 2 1000 1000 4096 Ful 30 22:20 mac
-rw-rw-r-- 1 1000 1000 0 Ful 30 22:11 tec.txt
-rw-rw-r-- 1 1000 1000 0 Ful 30 22:06 tutor.txt
ls -r Command
The ls -r command returns a list of all existing files and directories in reverse order.
ls -r
tutor.txt tec.txt mac kifa.txt doc.txt dir3 dir2 dir1 Debian.dpkg
ls -R Command
The ls -R command not only displays the long listing directory trees, but it also lists all files and directories with their related subdirectories all the way down to the final file.
ls -R
.:
Debian.dpkg dir1 dir2 dir3 doc.txt kifa.txt mac tec.txt tutor.txt
./dir1:
dir10
./dir1/dir10:
./dir2:
dir3
./dir2/dir3:
./dir3:
dir5
./dir3/dir5:
./mac
ls -i Command
The ls -i tool displays all existing files or directories with an inode number before them.
ls -i
7602985 Debian.dpkg 8388638 dir2 7602984 doc.txt 8388641 mac 7602982 tutor.txt
7602980 dir1 7602979 dir3 7602983 kifa.txt 7602981 tec.tx
Sort files by date and time in a list
Type the ls -t
command to list files or directories and sort by last modified date in descending order (biggest to smallest). You can also add a -r
flag to reverse the sorting order, like so: ls -tr.
Use the ls -t command to list files or directories and arrange them in descending order (largest to smallest) by the latest modified date.
ls -t
dir3 Debian.dpkg mac doc.txt kifa.txt tec.txt tutor.txt dir1 dir2
ls -tr
dir2 dir1 tutor.txt tec.txt kifa.txt doc.txt mac Debian.dpkg dir3
To separate files and directories with comma
We use ls -m.
ls -m
Debian.dpkg, dir1, dir2, dir3, doc.txt, kifa.txt, mac, tec.txt, tutor.txt
To add quotation marks to every directory
We use ls -Q.
ls -Q
"Debian.dpkg" "dir1" "dir2" "dir3" "doc.txt" "kifa.txt" "mac" "tec.txt" "tutor.txt"
To sort directories and files alphabetically
We use ls -X.
ls -X
dir1 dir2 dir3 mac Debian.dpkg doc.txt kifa.txt tec.txt tutor.txt
Conclusion
There are dozens of different commands and combinations you may investigate to list out files and folders based on your needs. One thing to keep in mind is the potential to mix many commands at once.
If you forget any command or are unclear about what to do, you can run ls –help, man ls, or ls, which will display a manual with all possible options for the ls command.
Thank you!
You may also see this: