ls command is the most commonly used command under Linux. The ls command is the abbreviation of list. By default, ls is used to print out the list of the current directory. If ls specifies another directory, the list of files and folders in the specified directory will be displayed. Through the ls command, you can not only view the files contained in the Linux folder, but also view file permissions (including directory, folder, file permissions), view directory information, and so on. The ls command is used a lot in daily Linux operations!
1. Command format:
ls [option] [directory name]
2 . Command function:
List all subdirectories and files in the target directory.
3. Commonly used parameters :
-a, –all lists all files in the directory, including implicit files starting with .
-a Same as -a, but do not list "." (indicating the current directory) and ".." (indicating the parent directory of the current directory).
-c With -lt: Sort by ctime and display ctime (the time when the file status was last changed) With -l: Display ctime but sort by name Otherwise: Sort by ctime
-c Each column is listed from top to bottom Item
–color[=when] Controls whether color resolution files are used. when can be one of 'never', 'always' or 'auto'
-d, –directory will display the directory as a file, rather than displaying the files below it.
-d, –dired produces results suitable for the dired mode of emacs
-f does not sort the output files, the -au option takes effect, and the -lst option fails
-g is similar to -l, but does not List owners
-g, –no-group Do not list any information about groups
-h, –human-readable List file sizes in an easy-to-understand format (e.g. 1k 234m 2g)
–si Like -h, but the file size is taken to the power of 1000 instead of 1024
-h, –dereference-command-line uses the real destination
–indicator-style= method indicated by the symbolic link in the command line Specify the indicator symbol
-i, –inode prints the inode of each file No.
-i, –ignore=style does not print out any items that match the shell wildcard character
4. Common examples:
Example 1: List the details of all files and directories under the /home/peidachang folder
Command:ls -l -r /home/peidachang
Be careful when using the ls command The format of the command: after the command prompt, first is the keyword of the command, followed by the command parameters. There should be a dash "-" before the command parameters. All command parameters have specific functions. You can customize them according to your needs. Select one or more parameters. Following the command parameters is the operation object of the command. In the above command "ls -l -r /home/peidachang", "ls" is the command keyword, "-l -r" is the parameter, and "/home/peidachang" is the operation object of the command. In this command, two parameters are used, namely "l" and "r". Of course, you can also use them together, as shown below:
Command: ls -lr /home/peidachang
The execution result of this form is exactly the same as the above command form. In addition, if the operation object of the command is located in the current directory, you can directly operate the operation object; if it is not in the current directory, you need to give the full path of the operation object. For example, in the above example, my current folder is the peidachang folder. I want to operate the peidachang file in the home folder. I can directly enter ls -lr peidachang, or I can use ls -lr /home/peidachang.
Example 2: To list the detailed contents of all directories starting with "t" in the current directory, you can use the following command:
Command: ls -l t*
You can view information about all files in the current directory whose file names begin with "t". In fact, in the command format, the content within the square brackets can be omitted. For the command ls, if the command parameters and operation objects are omitted and "ls" is entered directly, the contents of the current working directory will be listed.
Example 3: List only the subdirectories under the file
Command: ls -f /opt/soft |grep /$
List Subdirectory under the /opt/soft file
Output:
[root@localhost opt]# ls -f /opt/soft |grep /$
jdk1.6.0_16/
subversion-1.6. 1/
tomcat6.0.32/
Command: ls -l /opt/soft | grep "^d"
List the subdirectories under the /opt/soft file Details
Output:
[root@localhost opt]# ls -l /opt/soft | grep "^d"
drwxr-xr-x 10 root root 4096 09-17 18:17 jdk1. 6.0_16
drwxr-xr-x 16 1016 1016 4096 10-11 03:25 subversion-1.6.1
drwxr-xr-x 9 root root 4096 2011-11-01 tomcat6.0.32
Example 4: List all files in the current working directory whose names begin with s. After the newer ones, you can use the following command:
Command: ls -ltr s*
Output:
[root@localhost opt]# ls -ltr s*
src:
Total 0
script:
Total 0
soft:
Total 350644
drwxr-xr-x 9 root root 4096 2011-11-01 tomcat6.0.32
-rwxr-xr-x 1 root root 81871260 09-17 18:15 jdk-6u16-linux-x64.bin
drwxr-xr-x 10 root root 4096 09-17 18:17 jdk1.6.0_16
-rw-r--r-- 1 root root 205831281 09-17 18:33 apache-tomcat-6.0.32.tar .gz
-rw-r--r-- 1 root root 5457684 09-21 00:23 tomcat6.0.32.tar.gz
-rw-r--r-- 1 root root 4726179 10-10 11:08 subversion-deps-1.6.1.tar.gz
-rw-r--r-- 1 root root 7501026 10-10 11:08 subversion-1.6.1.tar.gz
drwxr- xr-x 16 1016 1016 4096 10-11 03:25 subversion-1.6.1
Example 5: List all files and directories in the current working directory; add "/" after the directory name, executable file Add "*"
to the name. Command: ls -af
Output:
[root@localhost opt]# ls -af
log/ script/ soft / src/ svndata/ web/
Example 6: Calculate the number of files and directories in the current directory
Command:
ls -l * |grep "^-" |wc -l ---Number of files
ls -l * |grep "^d"|wc -l ---Number of directories
Example 7: List in ls Absolute path to the file
Command: ls | sed "s:^:`pwd`/:"
Output:
[root@localhost opt]# ls | sed "s:^:`pwd`/:"
/opt/log
/opt/script
/opt/soft
/opt/src
/opt/svndata
/ opt/web
Example 8: List the absolute paths of all files (including hidden files) in the current directory, without recursion on the directory
Command: find $pwd -maxdepth 1 | xargs ls -ld
Output:
[root@localhost opt]# find $pwd -maxdepth 1 | xargs ls -ld
drwxr-xr-x 8 root root 4096 10-11 03:43 /opt
drwxr-xr-x 2 root root 4096 2012-03-08 /opt/log
drwxr-xr-x 2 root root 4096 2012-03-08 /opt/script
drwxr-xr -x 5 root root 4096 10-11 03:21 /opt/soft
drwxr-xr-x 2 root root 4096 2012-03-08 /opt/src
drwxr-xr-x 4 root root 4096 10 -11 05:22 /opt/svndata
drwxr-xr-x 4 root root 4096 10-09 00:45 /opt/web
Example 9: Recursively list all files in the current directory ( Absolute path including hidden files)
Command: find $pwd | xargs ls -ld
Example 10: Specify file time output format
Command :
ls -tl --time-style=full-iso
Output:
[root@localhost soft]# ls -tl --time-style=full-iso
Total 350644
drwxr-xr-x 16 1016 1016 4096 2012-10-11 03:25:58.000000000 0800 subversion-1.6.1
ls -ctl --time-style=long-iso
Output:
[root@localhost soft]# ls -ctl --time-style=long-iso
Total 350644
drwxr-xr-x 16 1016 1016 4096 2012-10-11 03 :25 subversion-1.6.1
Extension:
1. Display color directory list
Open /etc/bashrc and add the following line:
alias ls="ls --color"
The next time you start bash, you can display a colored directory list like in slackware. The meaning of the color is as follows:
1. Blue--> Directory
2. Green-->Executable file
3. Red-->Compressed file
4. Light blue-->Linked file
5. Gray--> Other files
The above is the detailed content of How to use the linux ls command. For more information, please follow other related articles on the PHP Chinese website!

The five core components of the Linux operating system are: 1. Kernel, 2. System libraries, 3. System tools, 4. System services, 5. File system. These components work together to ensure the stable and efficient operation of the system, and together form a powerful and flexible operating system.

The five core elements of Linux are: 1. Kernel, 2. Command line interface, 3. File system, 4. Package management, 5. Community and open source. Together, these elements define the nature and functionality of Linux.

Linux user management and security can be achieved through the following steps: 1. Create users and groups, using commands such as sudouseradd-m-gdevelopers-s/bin/bashjohn. 2. Bulkly create users and set password policies, using the for loop and chpasswd commands. 3. Check and fix common errors, home directory and shell settings. 4. Implement best practices such as strong cryptographic policies, regular audits and the principle of minimum authority. 5. Optimize performance, use sudo and adjust PAM module configuration. Through these methods, users can be effectively managed and system security can be improved.

The core operations of Linux file system and process management include file system management and process control. 1) File system operations include creating, deleting, copying and moving files or directories, using commands such as mkdir, rmdir, cp and mv. 2) Process management involves starting, monitoring and killing processes, using commands such as ./my_script.sh&, top and kill.

Shell scripts are powerful tools for automated execution of commands in Linux systems. 1) The shell script executes commands line by line through the interpreter to process variable substitution and conditional judgment. 2) The basic usage includes backup operations, such as using the tar command to back up the directory. 3) Advanced usage involves the use of functions and case statements to manage services. 4) Debugging skills include using set-x to enable debugging mode and set-e to exit when the command fails. 5) Performance optimization is recommended to avoid subshells, use arrays and optimization loops.

Linux is a Unix-based multi-user, multi-tasking operating system that emphasizes simplicity, modularity and openness. Its core functions include: file system: organized in a tree structure, supports multiple file systems such as ext4, XFS, Btrfs, and use df-T to view file system types. Process management: View the process through the ps command, manage the process using PID, involving priority settings and signal processing. Network configuration: Flexible setting of IP addresses and managing network services, and use sudoipaddradd to configure IP. These features are applied in real-life operations through basic commands and advanced script automation, improving efficiency and reducing errors.

The methods to enter Linux maintenance mode include: 1. Edit the GRUB configuration file, add "single" or "1" parameters and update the GRUB configuration; 2. Edit the startup parameters in the GRUB menu, add "single" or "1". Exit maintenance mode only requires restarting the system. With these steps, you can quickly enter maintenance mode when needed and exit safely, ensuring system stability and security.

The core components of Linux include kernel, shell, file system, process management and memory management. 1) Kernel management system resources, 2) shell provides user interaction interface, 3) file system supports multiple formats, 4) Process management is implemented through system calls such as fork, and 5) memory management uses virtual memory technology.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
