YouTip LogoYouTip

Linux Comm Ls

[![Image 1: Linux Command Manual](#) Linux Command Manual](#) The Linux `ls` (English full form: list directory contents) command is used to display the contents of a specified working directory (listing the files and subdirectories contained in the current working directory). ### Syntax ls [name...] **Parameters**: | Parameter | Description | | --- | --- | | `-a` or `--all` | Display all files (including hidden files starting with `.`). | | `-A` or `--almost-all` | Display all files except `.` and `..` (including hidden files). | | `-l` | List files in long format (detailed information: permissions, owner, size, modification time, etc.). | | `-h` or `--human-readable` | When used with `-l`, display file sizes in a human-readable format (e.g., KB, MB). | | `-t` | Sort by modification time (newest first). | | `-r` or `--reverse` | Reverse the sort order (used with `-t`, `-S`, etc.). | | `-S` | Sort by file size (largest first). | | `-R` or `--recursive` | List subdirectory contents recursively. | | `-F` or `--classify` | Append an indicator to filenames (e.g., `/` for directories, `*` for executable files). | | `--color` | Colored output (usually enabled by default, `--color=auto`). | | `-i` or `--inode` | Display the inode number of files. | | `-n` or `--numeric-uid-gid` | Display UID and GID numerically (instead of username and group name). | | `-d` or `--directory` | List directories themselves, not their contents (often used with `-l`). | | `-1` | List one file per line (default when terminal width is insufficient). | | `-m` | Fill width with a comma-separated list of files. | | `-Q` or `--quote-name` | Enclose filenames in double quotes (useful for filenames with spaces). | | `--group-directories-first` | List directories first, then files. | | `--time-style=` | Customize the time display format (e.g., `+%Y-%m-%d`). | ## Examples ls -l # List files and directories in the current directory in long format ls -a # List all files and directories in the current directory, including hidden files ls -lh # List files and directories in the current directory with human-readable sizes ls -t # List files and directories in the current directory sorted by modification time ls -R # Recursively list all files and subdirectories in the current directory ls -l /etc/passwd # Display detailed information about the /etc/passwd file ### Examples List all files in the current directory in detail (including hidden files): ls -la Sort files by size in reverse order (largest files first): ls -lShr Recursively list the contents of the /var/log directory and display file sizes in human-readable format: ls -lhR /var/log Display detailed information about directories only (without recursion): ls -ld /etc Sort by modification time (newest files last): ls -ltr List all directories under the root directory: # ls / bin dev lib media net root srv upload www boot etc lib64 misc opt sbin sys usr home lost+found mnt proc selinux tmp var List detailed information for all directories and files under the /bin directory: ls -lR /bin When filenames contain spaces, special characters, or start with a dash, you can use a backslash () for escaping or enclose the filename in quotes. For example: ls "my file.txt" # List the file named "my file.txt" ls my file.txt # List the file named "my file.txt" ls -- -filename # List the file named "-filename" The `ls` command can also use wildcards for pattern matching, where `*` matches any characters, `?` matches a single character, and `[...]` matches characters within a specified range. For example: ls *.txt # List all files with a .txt extension ls file?.txt # List files named file?.txt, where ? represents any single character ls *.txt # List files starting with a, b, or c and having a .txt extension List all files in the current working directory starting with 's', with the newest files appearing last: ls -ltr s* When using the `ls -l` command, the characters in the first column indicate the type and permissions of the file or directory. The first character represents the file type, for example: * `-` represents a regular file * `d` represents a directory * `l` represents a symbolic link * `c` represents a character device file * `b` represents a block device file * `s` represents a socket file * `p` represents a named pipe (FIFO) When using the `ls -l` command, the remaining 9 characters in the first column represent the access permissions for the file or directory, corresponding to three sets of `rwx` permissions. For example: * `r` represents read permission * `w` represents write permission * `x` represents execute permission * `-` represents no corresponding permission The first three characters represent the owner's permissions, the middle three represent the group's permissions, and the last three represent other users' permissions. For example: -rw-r--r-- 1 user group 4096 Feb 21 12:00 file.txt This indicates a file named file.txt, where the owner has read and write permissions, and the group and other users only have read permission. Find the most recently modified files: ls -lt | head -5 Displays the 5 most recently modified files. Count the number of files: ls | wc -l Counts the number of files in the current directory (excluding hidden files). * * * ## Notes The output color of the `ls` command can be controlled with the `--color` option: * Blue: Directories * Green: Executable files * Red: Compressed files * Cyan: Link files * Yellow: Device files When using `ls` in scripts, be cautious as directly parsing its output may be unreliable. It is recommended to use other methods. Different Linux distributions may have slight variations in the `ls` command. You can check the specific help documentation with `man ls`. [![Image 2: Linux Command Manual](#) Linux Command Manual](#)
← Linux Comm QuotacheckLinux Comm Umount β†’