What is ls command ?
ls command is a list command. It is used to list files and directories within a specified directory. It provides a way to view the contents of a directory, helping users identify what files and subdirectories are present. The syntax of the ls command is:
ls [option] [directory]
How to use ls command with examples
Example 1: Display basic listing
In your Linux environment, open your terminal and navigate to any directory you want to list the contents. In this case, we are at the /home/tim directory. Now we execute the ls command:
the output displays a list of files and directories in the current working directory, which is the home directory for the user named “tim.”
Example 2: Listing directories at the root of the filesystem
We use ls / command to shows directories at the root of the filesystem.
Example 3: Listing contents of the user’s home directory
We use ls ~ (tilde symbol ) to ask the system to list the files and directories in the home directory. In this case the user’s home directory is /home/tim, running ls ~ would be equivalent to running ls /home/tim , listing the contents of the “tim” user’s home directory.
Example 4: Listing one level up from the current working directory
In this example, the current working directory is /home/tim/Documents. Running ls ../ will list the content of the directory /home/tim, which is the parent directory of ‘Documents‘.
The pwd command was used to confirm the current working directory and then use ls ../ to list the contents of the parent directory.
Example 5: Listing in a long format
Using ls -l will list the contents of the current directory in a long format. -l is an option that stands for “long format”. The output will include additional information such as:
- File permissions (indicating who can read, write, and execute the file).
- Number of hard links.
- Owner of the file.
- Group associated with the file.
- File size in bytes.
- Modification date and time.
- File/directory name.
In this output, the first column represents the file permissions, followed by other details about each file or directory.
Example 6: Listing hiding files and directories
ls -a is the command used to list files and directories, including hidden ones, in the specified directory. The -a option stands for “all”. In this example, you can see the listing of files and directories in /home/tim, including hidden ones. The entries starting with .
are hidden.
Example 7: Listing and Sorting by size
The ls -S command is used to list files and directories in the specified directory sorted by size in descending order. The -S option or flag that instructs ls to sort the entries by size. The ls -S command will display files and directories with the largest size first, followed by entries in descending order of size. For example, if you have files in the current directory with varying sizes, running ls -S would show something like
You can also combine many options with the ls command. For example, using ls -Sl to list files and directories in the specified directory, sorted by size in descending order, and displayed in a long format:
Example 8: Filtering
The ls command also enable us to filtering capabilities, it helps you to narrow down and find what you looking for. Below are some examples:
In this example, ls -S my_scr?pt.txt lists files in the current directory matching the pattern my_scr?pt .txt (where ? is a wildcard representing any single character), and it sorts them by size in descending order. This case, the it display my_scrapt.txt and my_script.txt in descending order of size.
This example shows the command ls -S my_scr[ai]pt.txt lists files in the current directory (~/Test1) that match the parttern my_scr[ai]pt.txt. The [ai] part is a character set, meaning it matches either ‘a’ or ‘i’. The command then sorts the matching files by size in descending order. The result displays my_scrapt.txt and my_script.txt.
In this next example, ls -S my* lists files in the current directory (~/Test1) that start witht the prefix “my” and sorts them by size in descending order. In this case, it displays files ‘my_file.txt‘ ; ‘my_scrapt.txt‘ ; ‘my_script.txt‘ in descending order of size.
Furthermore, you can specify what should not be include in the pattern match by using the exclamation point (!). The command ls -S f[!a]ll lists files in the current directory (~/Test1) that match the pattern f[!a]ll where [!a] is a negation, meaning that it matches any character except ‘a’. The output shows files ‘fill‘ and ‘full‘ in descending order of size.
Conclusion
In a nutshell, by experimenting with the diverse options, combining them creatively, and exploring wildcard patterns, you can uncover even more ways to tailor the ls command to your specific needs. So feel free to explore more capabilities of ls command and discover the countless ways it can enhance your command-line experience.