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
Simple ls command
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..
someDir some-file.txt some-other-file.txt zebra.png
Sample output
Format Output
If you want to change the output format, the same ls command can be used with different options and combinations to fulfill your needs.
List one file per line: with -l option
To print exactly one file per line in the output, you can use ls -l
ls -l
ls -l command
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
Sample output of ls -l
You may have noticed that this ls -l prints a verbose output in each line along with its file permissions, ownership, size (in bytes), etc.
Print file-names-only in each line : with -1 option
If you want to print only the names of the file and directories, one in each line, you can use the option -1
ls -1
Command to print only filenames in each line, no other details
Output:
linux@revist.tech:~$ ls -1
someDir
some-file.txt
some-other-file.txt
zebra.png
Sample output of `ls -1`
Printing hidden files and folders: with -a option
If there are any hidden files or directories 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
Command to include hidden files and directories also in the output
Output:
linux@revist.tech:~$ ls -a
someDir some-file.txt some-other-file.txt zebra.png .hidden.txt
Sample output of ls -a
Combining options
List one file per line + include hidden files too
To show list of each file and directories in separate lines including the hidden files and folders, you can use combination of -l and -a together by writing as ls -la.
ls -la
Command to print files and folders with details including hidden files and directories
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
Sample output of ls -la
List only file and directory names per line + include hidden files too
To show all files one per line including hidden ones but with no additional information, you can use -1 and -a together as ls -1a.
ls -1a
Command to list file-names only per line including hidden files and folders
Output:
linux@revist.tech:~$ ls -1a
someDir
some-file.txt
some-other-file.txt
zebra.png
.hidden.txt
Sample output of command `ls -1a`
Releted Posts
Go From Zero, Part 12: Building a REST API with the Standard Library
This is Part 12 of 12 in Go From Zero, a beginner-to-production series on the Go programming language. If you missed it, Part 11: Best Practices for Production-Ready Go is the place to catch up.
Read moreGo From Zero, Part 11: Best Practices for Production-Ready Go
This is Part 11 of 12 in Go From Zero, a beginner-to-production series on the Go programming language. If you missed it, Part 10: Testing, Benchmarks, and Fuzzing is the place to catch up.
Read moreGo From Zero, Part 10: Testing, Benchmarks, and Fuzzing
This is Part 10 of 12 in Go From Zero, a beginner-to-production series on the Go programming language. If you missed it, Part 9: Generics Without the Headache is the place to catch up.
Read more