ls command
- In this linux commands tutorial, you will learn 12 practical tips of ls command in linux / unix with examples
- ls command is mainly used to display / list files and folders in current directory or user specified directory.
Important Command options
S.N | Options | Descriptions |
1. | ls -a | Displays all hidden files |
2. | ls -s | Displays size with files |
3. | ls -l | Displays files and directories in long format |
4. | ls -d | Displays only directories instead of files |
5. | ls . | Displays files in the current directory. |
6. | ls .. | Displays files in the parent directory. |
7. | ls / | Displays files in the root directory. |
8. | ls ~ | Displays files in the user home’s directory. |
Symbols Meaning
- Single dot operator . → Current directory (.)
- Double dot operators .. → Parent directory (..)
- / → Root Directory (/)
- ~ → User Home’s Directory (~)
Tools used
- Terminal – Repl.it (Online Linux Terminal) https://repl.it/
- OS – Windows 10
1. Displaying files and folders in current directory
- The dot operator . indicates the current directory
- Use the following syntax to show the files in current directory
Example
ls (or) ls .
Result
2. Displaying files and folders in parent directory
- The double dot operator .. indicates the parent directory
- Use the following syntax to show the files in parent directory
Example
ls ..
Result
3. Displaying files and folders in home directory
- The operator ~ indicates the home directory
- Use the following syntax to show the files in home directory
Example
ls ~
Result
4. Displaying hidden files
- By default, the ls command shows all files and folders except hidden files
- To show the hidden files, use the option -a
- Use the following syntax to show all files with hidden files
Example
ls -a
Result
5. Displaying only subdirectories
- To show only subdirectories excluding all files, use the option -d
- Use the following syntax to show only directories in the root.
Example
ls -d /* ( /* → show all directories in root or / )
Result
6. Displaying size(s) of files
- By default, the ls command shows all files and folders except file size
- To show the size of files, use the special option -s
- Use the following syntax to show size of all files of current directory
Example
ls -s // show size of all files (all files)
ls -s c5.txt // show size of c5.txt only (single file)
Result – Listing size of all files in the current directory
Result – Displaying the size of specified text file
7. Displaying inode number
- To show the inode number (file serial number) of files, use the option -i
- Use the following syntax to show the inode number of files
Example
ls -i
Result
8. Displaying files in long list format
- It summarizes the file the most important information about the file on a single line
- To show the files in long listing format, use the special option -l
- Use the following syntax to show files in long listing format.
Example
ls -l
- It shows seven columns
- Where
- 1st column shows the file type and file permissions
- 2nd column shows the integer number of links to the file
- 3rd column shows the owner name
- 4th column shows the group name
- 5th column shows the file size in bytes
- 6th column shows the file modification date and time
- 7th column shows the filename / directory name itself.
Result
9. Displaying files and directories in reverse order
- To show files and directories in reverse order, use the special option -r
- Use the following syntax to show files in reverse order.
Example
ls -r
Result
10. Displaying output per line
- The option -1, forces output to be one entry per line
- Use the following syntax to force the output to be one entry per line.
Example
ls -1
Result
11. Displaying files in sorted order
- This option is used to display the files in sorted order based on the file size (it will display big in size first).
- To show the sorted order of files, use the option -S
- Use the following syntax to show the files in order based on the file size.
Example
ls -lS (S is the uppercase)
Result – Listing Files and folders in sorted order only based on the big size
12. List files and output to a file
- If you want to store the result of ls command into a text file, then it is possible to store the output to a file.
- This is done by using output redirection operator > in linux
- Use the following syntax to redirect / store the output of ls command to a text file called “res.txt”. After that, use cat command to show the content of the resultant file cat res.txt.
Example
ls > res.txt
cat res.txt
Result
More Useful Posts
- How to convert Word to Number in Bash Script
- How to Count Word Frequency in Bash Script
- Bash Script – How to convert Number to Words
- Linux Commands – How to use cat, zcat, tac and lolcat commands in Linux
- How to Copy File in Linux (3 Tips) | Linux Tips and Tricks
2 linux tips to decompress file in unix / linux 3 linux tips to reverse the file contents in linux bash linux bash script bash script tutorial c# programming c# programming tutorial c# tutorial c# tutorial for beginners C++ c++ programming language c++ tutorial cat command cat command in linux class and objects in c++ command substitution in bash command substitution in linux difference between java and python for loop in bash for loop in shell script function in python example how to use for loop in shell script java java tutorial linux linux commands linux commands for beginners linux terminal linux tips linux tutorial lolcat command in linux ls command in linux OOPS OOPS Introduction pipe command in linux pygame tutorial python python for beginners python introduction python tutorial Shell Script strings in c++ strings in cplusplus Types of OOPS Languages unix