Definition:
When you type the ls command in your terminal, it lists all the files and folders inside the current directory you are working in.
Syntax:
ls [-a] [-l] [-lh] [-R] [file_or_directory]
Example:
Let’s say I have some files and folders that I created in the terminal. After some time, if I want to see these files and directories but forget their names, I can type the ls command to list them.
Files and directories created:
- file1.txt
- file2.txt
- file3.txt
- .hiddenfile # hidden file
- docs/ # subdirectory
Without Option:
Output:
$ ls
file1.txt
file2.txt
file3.txt
docs
With Options:
1. ls -a:
This command displays all files and folders, including hidden files (those starting with a .).
Output:
$ ls -a
. .. file1.txt file2.txt file3.txt .hiddenfile docs
2. ls -l:
It lists all files, folders, and subfolders vertically with their permissions (read, write, execute), owner (user), group, size (in machine-readable form), and date & time.
Output:
$ ls -l
-rw-r--r-- 1 user group 1200 Aug 25 10:20 file1.txt
-rw-r--r-- 1 user group 2450 Aug 25 10:21 file2.txt
-rw-r--r-- 1 user group 980 Aug 25 10:22 file3.txt
drwxr-xr-x 2 user group 4097 Aug 25 10:26 docs
-rw-r--r-- 1 user group 50 Aug 25 10:24 .hiddenfile
3. ls -lh:
This command is similar to ls -l, but it displays the sizes of files and folders in human-readable format, such as KB, MB, etc.
Output:
$ ls -lh
-rw-r--r-- 1 user group 1.2K Aug 25 10:20 file1.txt
-rw-r--r-- 1 user group 2.4K Aug 25 10:21 file2.txt
-rw-r--r-- 1 user group 980B Aug 25 10:22 file3.txt
drwxr-xr-x 2 user group 4.0K Aug 25 10:23 docs
-rw-r--r-- 1 user group 50B Aug 25 10:24 .hiddenfile
4. ls -R:
When you run this command on a directory, it lists the contents of that directory and all its subdirectories recursively.
Output:
$ ls -R
.:
file1.txt file2.txt file3.txt docs .hiddenfile
./docs:
notes.txt report.pdf
Note:
To use any command, type the command name followed by a space and then the folder name, such as:
ls folder_name. Before the command name, you give the dollar sign $.
