Operation and Maintenance
Linux Operation and Maintenance
How to use ls command to list files in Linux? (code example)The ls command is one of the basic commands that any Linux user should know; it is used to list information about files and directories in a file system. The following article will show you how to use the ls command in Linux. I hope it will be helpful to you.

How to use the ls command?
The syntax of the ls command is as follows:
ls [OPTIONS] [FILES]
The ls command has many [OPTIONS] options. Here are some commonly used options:
● -l : You can make ls list files in long list format.
● -a: Display all files and directories, including the following files.
● -t: List the files in order of creation (modification) time.
● -r: Display files in reverse order (originally in English alphabetical order).
● -R: If there are files in the directory, the files in the directory will be listed in sequence.
When used without parameters, the ls command will list the names of all files in the current working directory in alphabetical order.
ls
Output:
cache empty games lib local mail opt run
To list the files in a specific directory, you need to pass the path of the directory to the ls command. For example, to list the contents of the directory /etc, you would type:
ls /etc
You can also pass multiple directories and files to the ls command separated by spaces:
ls /etc /var /etc/passwd
If the logged-in user does not read permission to the directory, the user will receive a message stating that the ls command cannot open the directory:
ls /root
Output:
ls: cannot open directory '/root': Permission denied
How to list long List format?
The default output of the ls command only displays the name of the file, which is not very informative. However, when using the long list format, the ls command will display the following file information:
● File type
● File permissions
● Number of hard links to the file
● File owner
● File group
● File size
● Date and time
● File name
We can Use the -l (lowercase L) option to cause ls to output file information in a long listing format. For example:
ls -l /etc/hosts
Output:
-rw-r--r-- 1 root root 337 Oct 4 11:31 /etc/hosts
Explanation: Let us explain the output information
1. The first character '-' displays the file type
In our example, the first character is -, indicating a regular file. The values for other file types are as follows:
● -: Regular files
● b: Block special files
● c: Character special files
● d : Directory
● l : Symbolic link
● n : Network file
● p : FIFO
● s : Socket
2. Nine characters (rw-r--r--) display file permissions
The first three characters are for users, the last three characters are for groups, and the last three characters are for others. We can use the chmod command to change file permissions; the permission characters can take the following values:
● r: permission to read the file
● w: permission to write the file
● x: Permission to execute the file
● s: Setgid bit
● t: Stickiness
In our example, rw-r--r-- represents the user The file can be read and written, while the group and others can only read the file. However, the number of nine characters (rw-r--r--) followed by 1 allows the number of characters following the hard link to the file.
3. root root 337
Two fields root root display the file owner and group, followed by the file size (337) in bytes. If you want to output the dimensions in a readable format, you need to use the -h option. Users can change file owners using the chmod command.
4, Oct 4 11:31
Oct 4 11:31 is the last modification date and time of the file.
5./etc/hosts
/etc/hosts is the name of the file.
How to display hidden files?
In Linux, hidden files are any files starting with a dot (.); and by default, the ls command will not display hidden files.
How to display all files, including hidden files, you need to use the -a option:
ls -la ~/
Output:
drwxr-x--- 10 linuxize linuxize 4096 Feb 12 16:28 . drwxr-xr-x 18 linuxize linuxize 4096 Dec 26 09:21 .. -rw------- 1 linuxize linuxize 1630 Nov 18 2017 .bash_history drwxr-xr-x 2 linuxize linuxize 4096 Jul 20 2018 bin drwxr-xr-x 2 linuxize linuxize 4096 Jul 20 2018 Desktop drwxr-xr-x 4 linuxize linuxize 4096 Dec 12 2017 .npm drwx------ 2 linuxize linuxize 4096 Mar 4 2018 .ssh
We also use the -l option to display all files details.
How to modify the ls output order?
By default, the ls command lists files in alphabetical order; but we can also modify the output order:
● --sort=extension (or -X) : Sort alphabetically by entry extension
● --sort=size (or -S): Sort by file size
● --sort=time (or -t): Sort by file size Modification time sorting
● --sort=version (or -v): Natural type (version) number in the text
If you want to get the results in reverse sort order, you need to use -r options.
For example, to sort the files in the /var directory by modification time in reverse sort order, you would use:
ls -ltr /var
It is worth mentioning that the ls command does not display the directory content occupation of total space.
How to list subdirectories recursively?
Use the -R option to tell the ls command to display the contents of the subdirectory:
ls -R
相关视频教程推荐:《Linux教程》
以上就是本篇文章的全部内容,希望能对大家的学习有所帮助。更多精彩内容大家可以关注php中文网相关教程栏目!!!
The above is the detailed content of How to use ls command to list files in Linux? (code example). For more information, please follow other related articles on the PHP Chinese website!
Linux: A Look at Its Fundamental StructureApr 16, 2025 am 12:01 AMThe basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.
Linux Operations: System Administration and MaintenanceApr 15, 2025 am 12:10 AMThe key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.
Understanding Linux's Maintenance Mode: The EssentialsApr 14, 2025 am 12:04 AMLinux maintenance mode is entered by adding init=/bin/bash or single parameters at startup. 1. Enter maintenance mode: Edit the GRUB menu and add startup parameters. 2. Remount the file system to read and write mode: mount-oremount,rw/. 3. Repair the file system: Use the fsck command, such as fsck/dev/sda1. 4. Back up the data and operate with caution to avoid data loss.
How Debian improves Hadoop data processing speedApr 13, 2025 am 11:54 AMThis article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file
How to learn Debian syslogApr 13, 2025 am 11:51 AMThis guide will guide you to learn how to use Syslog in Debian systems. Syslog is a key service in Linux systems for logging system and application log messages. It helps administrators monitor and analyze system activity to quickly identify and resolve problems. 1. Basic knowledge of Syslog The core functions of Syslog include: centrally collecting and managing log messages; supporting multiple log output formats and target locations (such as files or networks); providing real-time log viewing and filtering functions. 2. Install and configure Syslog (using Rsyslog) The Debian system uses Rsyslog by default. You can install it with the following command: sudoaptupdatesud
How to choose Hadoop version in DebianApr 13, 2025 am 11:48 AMWhen choosing a Hadoop version suitable for Debian system, the following key factors need to be considered: 1. Stability and long-term support: For users who pursue stability and security, it is recommended to choose a Debian stable version, such as Debian11 (Bullseye). This version has been fully tested and has a support cycle of up to five years, which can ensure the stable operation of the system. 2. Package update speed: If you need to use the latest Hadoop features and features, you can consider Debian's unstable version (Sid). However, it should be noted that unstable versions may have compatibility issues and stability risks. 3. Community support and resources: Debian has huge community support, which can provide rich documentation and
TigerVNC share file method on DebianApr 13, 2025 am 11:45 AMThis article describes how to use TigerVNC to share files on Debian systems. You need to install the TigerVNC server first and then configure it. 1. Install the TigerVNC server and open the terminal. Update the software package list: sudoaptupdate to install TigerVNC server: sudoaptinstalltigervnc-standalone-servertigervnc-common 2. Configure TigerVNC server to set VNC server password: vncpasswd Start VNC server: vncserver:1-localhostno
Debian mail server firewall configuration tipsApr 13, 2025 am 11:42 AMConfiguring a Debian mail server's firewall is an important step in ensuring server security. The following are several commonly used firewall configuration methods, including the use of iptables and firewalld. Use iptables to configure firewall to install iptables (if not already installed): sudoapt-getupdatesudoapt-getinstalliptablesView current iptables rules: sudoiptables-L configuration


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Notepad++7.3.1
Easy-to-use and free code editor





