Ls command linux guide listing files
Command definition
The ls
command in Linux is used to list the contents of a directory, displaying files and subdirectories. It provides various options to customize the output, such as showing hidden files, displaying detailed information about each file, or sorting the list based on different criteria. By default, ls
shows the names of files and directories in the current working directory, making it an essential command for navigating and managing files in a Linux environment.
Usage examples
-
Basic listing: Displays the names of files and directories in the current directory.
bash ls
-
Listing with details: Shows detailed information about each file, including permissions, owner, size, and modification date.
bash ls -l
-
Including hidden files: Lists all files, including those that start with a dot (hidden files).
bash ls -a
-
Sorting by modification time: Displays files sorted by the time they were last modified, with the most recently modified files listed first.
bash ls -lt
-
Listing in a human-readable format: Shows file sizes in a more understandable format (e.g., KB, MB).
bash ls -lh
-
Recursive listing: Lists all files and directories in the current directory and all its subdirectories.
bash ls -R
-
Sorting by file size: Displays files sorted by size, with the largest files listed first.
bash ls -lS
-
Listing specific file types: Shows only files with a specific extension, such as
.txt
.bash ls *.txt
-
Using color for output: Displays files with different colors based on their type for better visibility.
bash ls --color=auto
-
Listing in a specific directory: Lists the contents of a specified directory instead of the current one.
bash ls myfolder