Listing files & directories in Linux

Listing files & directories in Linux/Unix-based systems is very easy. Also, there are some options that you may find useful in the day-to-day usage of Unix-based operating systems. We have curated a list of useful commands with multiple options and examples.

The ls Command

ls

This ls command lists the names of the files and directories in tab-separated format. The command returns only the files and folders in the current directory. Here is a sample output..

linux@revist.tech:~$ ls
someDir  some-file.txt  some-other-file.txt  zebra.png

If you want to change the output format, the same ls command can be used with different options and combinations to fulfill your needs.

The ls Command with -l option (List one file per line)

To print exactly one file per line, you can use ls -l

ls -l

Output:

linux@revist.tech:~$ ls -l
total 4658
drwxr-xr-x 2 linux linux 4096 Jan  7 00:22 someDir
-rw-r--r-- 1 linux linux   15 Jan 10 01:17 some-file.txt
-rw-r--r-- 1 linux linux   21 Jan 10 01:17 some-other-file.txt
-rw-r--r-- 1 linux linux  526 Jan 10 02:34 zebra.png

You may have noticed that this ls -l prints a verbose output in each line along with its file permissions, ownership, size, etc.

If you just want to print ONLY the names of file and directories in one in each line, you can use the option -1

ls -1

Output:

linux@revist.tech:~$ ls -1
someDir
some-file.txt
some-other-file.txt
zebra.png

The ls Command with -a option (Printing hidden files and folders)

If there are any hidden files or direcotries then the plain ls command does not show it in the output. This is because it respects the hidden permission of the files and folders by default. But in case, you want to list the hidden files and directories as well, you can use ls -a option.

ls -a

Output:

linux@revist.tech:~$ ls -a
someDir  some-file.txt  some-other-file.txt  zebra.png .hidden.txt

To show all files one per line including hidden ones, you can use combination of -l and -a together by writing as la -la.

ls -la

Output:

linux@revist.tech:~$ ls -la
total 4680
drwxr-xr-x 2 linux linux 4096 Jan  7 00:22 someDir
-rw-r--r-- 1 linux linux   15 Jan 10 01:17 some-file.txt
-rw-r--r-- 1 linux linux   21 Jan 10 01:17 some-other-file.txt
-rw-r--r-- 1 linux linux  526 Jan 10 02:34 zebra.png
-rw-r--r-- 1 linux linux   22 Jan 10 02:40 .hidden.txt

To show all files one per line including hidden ones but with no additonal information, you can use -1 and -a together as ls -1a.

ls -1a

Output:

linux@revist.tech:~$ ls -1a
someDir
some-file.txt
some-other-file.txt
zebra.png
.hidden.txt
comments powered by Disqus

Releted Posts

Installing multiple JDK in Linux

Before we begin installing Java in our system, we should check if it is already installed or not. To do so we can run the below command in our terminal:

Read more