The ls command is one of the most basic and frequently used commands in Linux. It is used to list the contents of directories, including files and subdirectories. Whether you’re a Linux beginner or an experienced user, mastering the ls command is essential for navigating and managing your file system efficiently. If you’re using a cloud server like Kamatera, understanding ls helps you manage files directly from the terminal. This guide provides a step-by-step walkthrough of how to use the ls command, from basic usage to advanced options.
Step by step guide
- Start by opening your terminal on your Linux machine, launching Git Bash on Windows, or accessing your cloud instance via SSH. You must have at least one server deployed (How to deploy a Kamatera server)
2. The following command lists the files and directories in the current working directory.
Command:
ls
3. List files in a specific directory: followed by a directory path:
Command:
ls /home/username/Documents
Note: Replace /home/username/Documents with the path you want to view.
4. Use -l for long listing format: Add the -l option to view details such as permissions, ownership, file size, and modification date:
Command:
ls -l
5. View hidden files: Use the -a option to include hidden files (those beginning with a dot .):
Command:
ls -a
6. Combine options: Combine multiple options for more detailed listings:
Command:
ls –la
This shows all files, including hidden ones, in long format.
7. Sort files by time, size, etc.
- To sort files by modification time (most recent first):
Command:
ls –lt
- To sort by file size:
Command:
ls -lS
8. Human-readable file sizes: To show file sizes in a human-readable format (KB, MB, etc.):
Command:
ls –lh
9. Recursively list subdirectories: To list contents of all subdirectories:
Command:
ls –R
10. Customize output with color and formatting: Most Linux systems support colorized output by default. If not, use:
Command:
ls --color=auto
11. The below command displays the inode number of each file or directory in the listing. Inodes are unique identifiers used by the filesystem to track files, regardless of their name or path.
Command:
ls –i
12. The ls -g command is a variation of the ls -l (long listing) command, but without showing the file owner. It displays detailed file information excluding the user name, and only shows the group ownership, along with permissions, size, and modification time.
Command:
ls -g
13. The command below is used to list only directories (not files) in the current location.
Command:
ls –d */
14. The below command is used to list all files in the current directory that have a .txt extension.
Command:
ls *.txt
15. This command is a powerful combination of options used to list all files (including hidden ones) in long format, sorted by modification time (most recently modified at the top).
Command:
ls -alt
16. The below command combines several useful options to display detailed, human-readable information about files, including their inode numbers.
Command:
ls -lih
17. This command is used to count the number of files and directories in the current directory (non-recursively and excluding hidden files by default).
Command:
ls | wc -l
18. This command is used to list only the files (not directories) in the current directory.
Command:
</ls -p | grep -v /
19. This command is used to find all files modified in the last 7 days (in the current directory and subdirectories) and then list their details using ls -l.
Command:
find . -type f -mtime -7 | xargs ls –l
20. This command is used to search for a specific file (or pattern) in the current directory and display its detailed information, such as permissions, size, timestamp, and ownership.
Command:
ls –l | grep ‘filename’
Note: Replace ‘filename’ with the actual name of the file you want to search for.
And that’s it! You have successfully learned the Linux Ls command to list files and directories.