Learning Common Linux Commands
Category Programming Technology
1. ls Command
The ls command, short for list, allows you to view the contents of a Linux directory, including file permissions (for directories, folders, and files), and directory information.
Common Parameter Combinations:
ls -a List all files in the directory, including hidden files starting with '.'
ls -A List all files except '.' and '..'
ls -r List in reverse order
ls -t List by file modification time
ls -S List by file size
ls -h List in human-readable size
ls -l List detailed information including permissions, owner, file size, and more, besides the file name
Examples:
(1) List files in human-readable format, sorted by time in reverse order, with detailed information:
ls -lhrt
(2) List files with detailed information, sorted by size in reverse order:
ls -lrS
(3) List detailed contents of all directories starting with "t" in the current directory:
ls -l t*
(4) List absolute paths of files (excluding hidden files):
ls | sed "s:^:`pwd`/:"
(5) List absolute paths of files (including hidden files):
find $pwd -maxdepth 1 | xargs ls -ld
2. cd Command
The cd (changeDirectory) command syntax:
cd [directory name]
Description: Changes the current directory to dirName.
Examples:
(1) Navigate to the root directory:
cd /
(2) Navigate to the "home" directory:
cd ~
(3) Return to the previous working directory:
cd -
(4) Use the previous command's parameters as the cd parameters:
cd !$
3. pwd Command
The pwd command is used to view the path of the current working directory.
Examples:
(1) View the current path:
pwd
(2) View the actual path of a symbolic link:
pwd -P
4. mkdir Command
Available options:
--m: Set access permissions for the new directory. --p: Can be a path name. If any directories in the path do not exist, they will be created automatically.
Examples:
(1) Create a directory named "t" in the current working directory:
mkdir t
(2) Create a directory path "test/t1/t" in the /tmp directory, creating any necessary parent directories:
mkdir -p /tmp/test/t1/t
5. rm Command
Used to delete one or more files or directories from a directory. Without the -r option, rm will not delete directories. If you use rm to delete a file, it is usually still possible to recover the file.
rm [options] file…
Examples:
(1) Delete any .log files, prompting for confirmation before each deletion:
rm -i *.log
(2) Delete the "test" subdirectory and all its contents without confirmation:
rm -rf test
(3) Delete files starting with "-f":
rm -- -f*
6. rmdir Command
Used to remove one or more subdirectories from a directory. You must have write permission for the parent directory to delete a directory.
Note: Cannot delete non-empty directories.
Examples:
(1) Delete "parent/child/child11" and if "parent/child" becomes empty, delete it as well:
rmdir -p parent/child/child11
7. mv Command
Used to move files or rename files. Depending on the second parameter type (if it's a directory, move the files; if it's a file, rename the file).
When the second parameter is a directory, the first parameter can be multiple files or directories separated by spaces, and then move the specified files to the directory specified by the second parameter.
Examples:
(1) Rename the file test.log to test1.txt:
mv test.log test1.txt
(2) Move the files log1.txt, log2.txt, log3.txt to the /test3 directory:
mv log1.txt log2.txt log3.txt /test3
(3) Rename the file file1 to file2, prompting for confirmation if file2 already exists:
mv -i log1.txt log2.txt
(4) Move all files in the current directory to the parent directory:
mv * ../
8. cp Command
Copies source files to destination files, or multiple source files to a destination directory.
Note: When copying from the command line, if the destination file already exists, it will prompt whether to overwrite it. In shell scripts, if the -i parameter is not added, it will not prompt and will overwrite directly!
-i Prompt
-r Copy directories and their contents
-a Preserve the original file timestamps
Examples:
(1) Copy a.txt to the test directory, preserving the original file timestamps, and prompt if the original file exists:
cp -ai a.txt test
(2) Create a symbolic link (shortcut) for a.txt:
cp -s a.txt link_a.txt
9. cat Command
cat has three main functions:
Display the entire file:
cat filename
Create a file from keyboard input:
cat > filename
Can only create new files, not edit existing ones.
- Combine multiple files into one:
cat file1 file2 > file
- -b Number non-empty lines
- -n Number all lines
Examples:
(1) Add line numbers to the contents of log2012.log and output to log2013.log:
cat -n log2012.log log2013.log
(2) Add line numbers (excluding blank lines) to the contents of log2012.log and log2013.log and append to log.log:
cat -b log2012.log log2013.log log.log
(3) Generate a new file using here doc:
cat >log.txt <<EOF
>Hello
>World
>PWD=$(pwd)
>EOF
ls -l log.txt
cat log.txt
Hello
World
PWD=/opt/soft/test
(4) Display in reverse order:
tac log.txt
PWD=/opt/soft/test
World
Hello
10. more Command
Similar to cat, more displays files one page at a time, allowing users to read page by page. The basic commands are pressing the spacebar to move to the next page and pressing 'b' to move back one page.
Command Parameters:
+n Start displaying from line n
-n Define the screen size to n lines
+/pattern Search for the string (pattern) before displaying each file, and start displaying from two lines after the string
-c Clear the screen before displaying
-d Prompt "Press space to continue, 'q' to quit" and disable the bell
-l Ignore Ctrl+l (form feed) characters
-p Clear the screen instead of scrolling to page the file, similar to -c
-s Display consecutive blank lines as one line
-u Remove underscores from the file content
Common Operation Commands:
Enter Move down n lines, default is 1 line
Ctrl+F Scroll down one screen
Space Scroll down one screen
Ctrl+B Move back one screen
= Output the current line number
:f Output the file name and current line number
V Invoke the vi editor
!command Invoke the shell and execute the command
q Exit more
Examples:
(1) Display the contents of the file starting from line 3:
more +3 text.txt
(2) Display detailed information of file directories, showing 5 lines at a time:
ls -l | more -5
Press the spacebar to display the next 5 lines.
11. less Command
less is similar to more but allows you to browse files freely, while more can only move forward and not backward. less does not load the entire file before viewing.
Common Command Parameters:
-i Ignore case when searching
-N Display line numbers
-o <filename> Save the output of less to a specified file
-s Display consecutive blank lines as one line
/string: Search forward for "string"
?string: Search backward for "string"
n: Repeat the previous search (related to / or ?)
N: Repeat the previous search in reverse (related to / or ?)
-x <number> Display "tab" as a specified number of spaces
b Move back one page
d Move back half a page
h Display help interface
Q Exit less
u Scroll up half a page
y Scroll up one line
Space Scroll down one line
Enter Scroll down one page
[PageDown]: Scroll down one page
[PageUp]: Scroll up one page
Examples:
(1) Display process information with ps and paginate with less:
ps -aux | less -N
(2) View multiple files:
less 1.log 2.log
Use n to view the next file and p to view the previous file.
12. head Command
head displays the beginning of a file to the standard output. By default, the head command prints the first 10 lines of the corresponding file.
Common Parameters:
-n<lines> Display the specified number of lines (negative numbers count from the end)
Examples:
(1) Display the first 20 lines of 1.log:
head 1.log -n 20
(2) Display the first 20 bytes of 1.log:
head -c 20 log2014.log
(3) Display the last 10 lines of t.log:
head -n -10 t.log
13. tail Command
Used to display the end of a specified file. If no file is specified, it processes input information. Commonly used to view log files.
Common Parameters:
-f Loop read (commonly used to view increasing log files)
-n<lines> Display the specified number of lines (from the end)
(1) Loop read and display the increasing file content:
ping 127.0.0.1 > ping.log &
Run in the background: Use jobs -l to view, or use fg to bring it to the foreground.
tail -f ping.log
(View logs)
14. which Command
To find a file in Linux when you don't know where it is, you can use the following commands:
which View the location of an executable file.
whereis View the location of a file.
locate View the file location using the database.
find Search the hard drive for the file name.
which searches for the location of a system command in the PATH, which is the specified path, and returns the first search result. Using the which command, you can see if a system command exists and which location's command is being executed.
Common Parameters:
-n Specify the file name length, which must be greater than or equal to the longest file name among all files.
Examples:
(1) Check if the ls command exists and which one is executed:
which ls
(2) Check which:
which which
(3) Check cd:
which cd (shows it does not exist, because cd is a built-in command, and which searches for commands in PATH)
View the current PATH configuration:
echo $PATH
Or use env to view all environment variables and their values.
15. whereis Command
The whereis command can only be used to search for program names and only searches for binary files (parameter -b), man pages (parameter -m), and source code files (parameter -s). If no parameters are omitted, it returns all information. Both whereis and locate search based on the system's built-in database, so they are very efficient, while find searches the hard drive for files.
Common Parameters:
-b Locate executable files.
-m Locate help files.
-s Locate source code files.
-u Search for other files excluding executable files, source code files, and help files in the default paths.
Examples:
(1) Find related files for the locate program:
whereis locate
(2) Find the source code file for locate:
whereis -s locate
(3) Find the help documentation for locate
whereis -m locate
### 16. locate Command
The locate command searches the system's built-in document database to quickly find files. The database is updated by the updatedb program, which is periodically called by the cron daemon. By default, the locate command is faster at searching the database than searching the entire hard drive, but it may not find files that were recently created or renamed. The updatedb program runs once a day by default and can be adjusted via crontab settings (etc/crontab).
The locate command is similar to the find command and can use wildcards like * and ? for regex matching.
**Common Parameters:**
-l num (number of lines to display) -f Exclude specific file systems, such as proc -r Use regex as the search condition
**Examples:**
(1) Find all files related to pwd (files containing pwd in their names)
locate pwd
(2) Search for files in the etc directory that start with sh
locate /etc/sh
(3) Find files in the /var directory that end with reason
locate -r '^/var.reason$' (where . represents a single character, * represents any number of characters; . represents any number of characters)
### 17. find Command
Used to search for files in a file tree and perform corresponding actions.
Command Format:
find pathname -options [-print -exec -ok ...]
Command Parameters:
pathname: The directory path where the find command searches. Use . for the current directory and / for the system root directory. -print: The find command outputs matching files to standard output. -exec: The find command executes the given shell command on matching files. The command format is 'command' { } \;, with a space between { } and \;. -ok: Same as -exec, but executes the shell command in a safer mode, prompting the user to confirm before each command execution.
**Command Options:**
-name: Search for files by name -perm: Search for files by permission -user: Search for files by owner -group: Search for files by group -type: Search for files of a certain type, such as: b - block device file d - directory c - character device file l - symbolic link file p - pipe file f - regular file
-size n :[c]: Search for files of size n blocks, with c indicating file byte size -amin n: Search for files accessed in the last N minutes -atime n: Search for files accessed in the last n24 hours -cmin n: Search for files whose status was changed in the last N minutes -ctime n: Search for files whose status was changed in the last n24 hours -mmin n: Search for files whose data was changed in the last N minutes -mtime n: Search for files whose data was changed in the last n*24 hours (Use - to specify changes within n days, and + to specify changes before n days.) -maxdepth n: Maximum directory depth to search -prune: Specify directories to ignore. Note that -prune is ignored if -depth is used. -newer: Search for files newer than a specified file but older than another.
**Examples:**
(1) Find files modified within the last 48 hours
find -atime -2
(2) Find files ending with .log in the current directory
find ./ -name '*.log'
(3) Find files with permission 777 in the /opt directory
find /opt -perm 777
(4) Find files larger than 1K
find -size +1000c
Find files exactly 1000 characters in size
find -size 1000c
-exec parameter is followed by the command command, which ends with ;. The semicolon is essential, and a backslash is added before it to avoid conflicts. The {} brackets represent the file names found by the find command.
**Examples:**
(5) Find and delete files older than 10 days in the current directory without prompting
find . -type f -mtime +10 -exec rm -f {} \;
(6) Find and delete files ending with .log and older than 5 days in the current directory, prompting before deletion
find . -name '*.log' mtime +5 -ok -exec rm {} \;
(7) Find files starting with passwd and containing the string "pkg" in the current directory
find . -f -name 'passwd*' -exec grep "pkg" {} \;
(8) Execute the cp command using the exec option
find . -name '*.log' -exec cp {} test3 \;
-xargs: The find command passes matching files to the xargs command, which processes files in batches rather than all at once, unlike -exec. This allows it to process the first batch, then the next, and so on.
Examples:
(9) Find and determine the file types of all regular files in the current directory using xargs
find . -type f -print | xargs file
(10) Find all .js files in the current directory containing the string 'editor' and print their names
find . -type f -name ".js" -exec grep -lF 'ueditor' {} \; find -type f -name '.js' | xargs grep -lF 'editor'
(11) Use xargs to execute the mv command
find . -name "*.log" | xargs -i mv {} test4
(12) Search for the word 'hostnames' in all regular files in the current directory and mark the lines where it appears
find . -name * -type f -print | xargs grep -n 'hostnames'
(13) Find files in the current directory starting with a lowercase letter and ending with 4 to 9 followed by .log
find . -name '[a-z]*[4-9].log' -print
(14) Search in the test directory but exclude the test/test4 subdirectory
find test -path 'test/test4' -prune -o -print
(15) Find files newer than log2012.log but older than log2017.log
find -newer log2012.log ! -newer log2017.log
**Using depth Option:**
The depth option allows the find command to first back up all files before backing up files in subdirectories when backing up the file system to a tape.
Example: Find a file named CON.FILE starting from the root directory, matching all files first and then entering subdirectories
find / -name "CON.FILE" -depth -print
### 18. chmod Command
Used to change the access permissions of files or directories in Linux. It controls the access permissions for files or directories. The command has two types of usage: one with letters and operator expressions, and the other with numbers.
Each file or directory has three sets of permissions, each represented by three characters: read, write, and execute permissions for the file owner, the group owner, and other users. Use `ls -l test.txt` to view these permissions.
Example for file log2012.log:
-rw-r--r-- 1 root root 296K 11-13 06:03 log2012.log
The first column has 10 positions. The first character specifies the file type. If it is a hyphen (-), it is a non-directory file. If it is a d, it is a directory. The next 9 characters, in groups of 3, represent the permissions for the owner, group, and other users, respectively. The permission characters are: hyphen for no permission, r for read, w for write, and x for execute.
Common Parameters:
-c: Report changes when they occur -R: Process specified directories and their subdirectories
Permission Scope:
u: Current user of the directory or file g: Current group of the directory or file o: Users or groups other than the current user or group a: All users and groups
Permission Codes:
r: Read permission, represented by 4 w: Write permission, represented by 2 x: Execute permission, represented by 1 -: Remove permission, represented by 0 s: Special permission
Examples:
(1) Add execute permission for all users to the file t.log
chmod a+x t.log
(2) Revoke all existing permissions and grant read permission to the owner, with change reporting
chmod u=r t.log -c
(3) Assign read, write, and execute (7) permissions to the owner, read and execute (5) permissions to the group, and execute (1) permission to others for the file t.log
chmod 751 t.log -c (or: chmod u=rwx,g=rx,o=x t.log -c)
(4) Add read permissions to the text directory and its subdirectories for all users, with change reporting
chmod u+r,g+r,o+r -R text/ -c
Used for compressing and decompressing files. The tar command itself does not have compression capabilities; it only has packaging capabilities. Compression and decompression are handled by other tools.
Clarify two concepts: packaging and compression. Packaging refers to combining a bunch of files or directories into one file; compression refers to reducing the size of a large file through compression algorithms.
**Common Parameters:**
-c: Create a new compressed file -f: Specify the compressed file -r: Add files to an existing compressed package -u: Add changed and existing files to a compressed package -x: Extract files from a compressed package -t: Display the contents of a compressed file -z: Support gzip compression -j: Support bzip2 compression -Z: Support compress decompression -v: Display the operation process
Regarding gzip and bzip2 compression:
gzip example: Compress gzip fileName .tar.gz and .tgz. Decompress: gunzip filename.gz or gzip -d filename.gz Corresponding: tar zcvf filename.tar.gz. tar zxvf filename.tar.gz
bz2 example: Compress bzip2 -z filename .tar.bz2. Decompress: bunzip filename.bz2 or bzip -d filename.bz2 Corresponding: tar jcvf filename.tar.gz. Decompress: tar jxvf filename.tar.bz2
**Examples:**
(1) Package all files into a tar package
tar -cvf log.tar 1.log,2.log or tar -cvf log.*
(2) Package all files and directories under /etc into a specified directory and compress using gzip
tar -zcvf /tmp/etc.tar.gz /etc
(3) View the contents of the newly packaged file (must include z, as it uses gzip compression)
tar -ztvf /tmp/etc.tar.gz
(4) Package /home and /etc, excluding /home/dmtsai
tar -zcvf /tmp/system.tar.gz --exclude=/home/dmtsai /home/* /etc
tar --exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc
### 20. chown Command
The `chown` command changes the owner and group of specified files. The user can be a username or user ID, and the group can be a group name or group ID. The files are specified as a space-separated list and support wildcard characters.
-c Display information about the changes made -R Process directories and their subdirectories recursively
**Examples:**
(1) Change owner and group, and display change information
chown -c mail:mail log2012.log
(2) Change file group
chown -c :mail t.log
(3) Change folder and subfolder owner and group to mail
chown -cR mail: test/
### 21. df Command
Displays disk space usage. It provides information about how much disk space is used and how much is available. If no file name is specified, it shows the available space for all currently mounted file systems. By default, disk space is displayed in 1KB units unless the environment variable `POSIXLY_CORRECT` is specified, in which case it will be displayed in 512-byte units.
-a List all file systems -h Display information in a human-readable format -i Show inode information -k Display in 1024-byte blocks -l Show only local disks -T List file system types
**Examples:**
(1) Display disk usage
df -l
(2) List all file systems and their types in a readable format
df -haT
### 22. du Command
The `du` command also views space usage but differs from `df` as it shows the disk usage of files and directories.
Command format:
du [options] [files]
**Common Parameters:**
-a Display sizes of all files in directories -k Display sizes in KB -m Display sizes in MB -g Display sizes in GB -h Display sizes in a human-readable format -s Show only the total size -c or --total Show total size of all directories and files
**Examples:**
(1) Display directory and subdirectory sizes in a readable format
du -h scf/
(2) Display all file sizes in a directory in a readable format
du -ah scf/
(3) Show individual and total sizes of specified directories
du -hc test/ scf/
(4) Output the space used by subdirectories in the current directory
du -hc --max-depth=1 scf/
### 23. ln Command
Creates a link to a file in another location, allowing the same file to be accessed from multiple directories without duplicating the file.
Link types: Soft link and Hard link
Soft link:
- Exists as a path. Similar to a Windows shortcut.
- Can cross file systems; hard links cannot.
- Can link to a non-existent file.
- Can link to directories.
Hard link:
- Exists as a file copy but does not consume additional space.
- Cannot link to directories.
- Must be within the same file system.
**Notes:**
- `ln` ensures synchronization between linked files.
- Soft links are created with `ln -s source target`, while hard links are created with `ln source target`.
- `ln` can link multiple files or directories to a destination, which must be an existing directory.
**Common Parameters:**
-b Delete or overwrite previous links -s Create a symbolic (soft) link -v Display detailed processing information
**Examples:**
(1) Create a soft link and show operation details
ln -sv source.log link.log
(2) Create a hard link and show operation details
ln -v source.log link1.log
(3) Create a soft link to a directory
ln -sv /opt/soft/test/test3 /opt/soft/test/test5
### 24. date Command
Displays or sets the system date and time.
Command parameters:
-d<string> Display the date and time specified by the string. -s<string> Set the date and time according to the string. -u Display GMT. %H Hour (00-23) %I Hour (00-12) %M Minute (00-59) %s Total seconds since 1970-01-01 00:00:00 UTC. %S Second (local time format) %a Abbreviated weekday name. %A Full weekday name. %d Day of the month (01-31). %D Date (mm/dd/yy). %m Month (01-12). %y Year (00-99). %Y Full year (four digits).
**Examples:**
(1) Display the next day
date +%Y%m%d --date="+1 day"
(2) Use of -d parameter
date -d "nov 22" date -d '2 weeks' date -d 'next monday' date -d next-day +%Y%m%d date -d last-day +%Y%m%d date -d last-month +%Y%m date -d next-month +%Y%m
### 25. cal Command
Displays the Gregorian calendar. If one parameter is given, it represents the year (1-9999). If two parameters are given, they represent the month and year.
Common parameters:
-3 Show the calendars for the previous, current, and next months -m Show Monday as the first day of the week -j Display the day of the year -y [year] Display the calendar for the specified year
**Examples:**
(1) Display the calendar for a specific month and year
cal 9 2012
(2) Display the calendar for the entire year 2013
cal -y 2013
(3) Display three months with Monday as the first day
cal -3m
### 26. grep Command
A powerful text search tool that searches for patterns in files. It outputs the lines that match a specified pattern.
Command format:
grep [options] pattern [files]
Common parameters:
-A n --after-context Show n lines after the match -B n --before-context Show n lines before the match -C n --context Show n lines around the match -c --count Count the number of matching lines -i Ignore case -l List only the names of files with matching lines -f Read patterns from a file -n Show the line number of matches -R Recursively search directories
grep Regular Expressions:
^ Anchors the start of a line $ Anchors the end of a line . Matches any single character
- Matches zero or more of the preceding character .* Matches any character sequence [] Matches any character in the range [^] Matches any character not in the range (..) Marks a matched substring < Anchors the start of a word > Anchors the end of a word x{m} Matches 'm' occurrences of 'x' x{m,} Matches at least 'm' occurrences of 'x' x{m,n} Matches between 'm' and 'n' occurrences of 'x' \w Matches word characters (alphanumeric) \W Matches non-word characters \b Word boundary
**Examples:**
(1) Find a specific process
ps -ef | grep svn
(2) Count the number of specific processes
ps -ef | grep svn -c
(3) Search for patterns from a file
cat test1.txt | grep -f key.log
(4) Recursively find files starting with 'grep' in a directory
grep -lR '^grep' /tmp
(5) Find lines not starting with 'x'
grep '^[^x]' test.txt
(6) Show lines containing 'ed' or 'at'
grep -E 'ed|at' test.txt
### 27. wc Command
The `wc` (word count) command counts the number of bytes, words, and lines in specified files and outputs the results.
Command format:
wc [options] [files]
**Command parameters:**
-c Count bytes -l Count lines -m Count characters -w Count words, defined as sequences separated by whitespace, tabs, or newlines
**Examples:**
(1) Display the number of lines, words, bytes, and file name
wc text.txt
Result:
7 8 70 test.txt
(2) Count the number of lines in the output
cat test.txt | wc -l
### 28. ps Command
The `ps` (process status) command displays the status of running processes. It provides a snapshot of the current processes. For continuous monitoring, use `top`.
Process states in Linux:
- Running (either running or waiting in the run queue)
- Interrupted (sleeping, blocked, waiting for a condition or signal)
- Uninterruptible (cannot be woken or run; must wait for an interrupt)
- Zombie (process terminated but descriptor remains until parent calls wait)
- Stopped (stopped by signals SIGSTOP, SIGSTP, SIGTIN, SIGTOU)
ps State Codes:
D Uninterruptible sleep (usually IO) R Running (on run queue) S Interrupted (sleeping) T Stopped (traced or stopped) Z Zombie (defunct process)
**Command parameters:**
-A Display all processes a Display all processes -a Display all processes on the terminal c Show real process names e Display environment variables f Show process relationships r Show running processes on the terminal -aux Show all processes including those of other users
**Examples:**
(1) Display all processes with environment variables and relationships
ps -ef
(2) Display all current processes
ps -A
(3) Use grep to find a specific process
ps -aux | grep apache
(4) Find PIDs related to the cron and syslog services
ps aux | grep '(cron|syslog)'
- top command
Displays information about processes currently running on the system, including process ID, memory usage, and CPU usage.
Common parameters:
-c Display complete process commands
-s Secure mode
-p <process number> Display specific process
-n <number of times> Number of cyclic displays
Example:
(1)
top - 14:06:23 up 70 days, 16:44, 2 users, load average: 1.25, 1.32, 1.35
Tasks: 206 total, 1 running, 205 sleeping, 0 stopped, 0 zombie
Cpu(s): 5.9%us, 3.4%sy, 0.0%ni, 90.4%id, 0.0%wa, 0.0%hi, 0.2%si, 0.0%st
Mem: 32949016k total, 14411180k used, 18537836k free, 169884k buffers
Swap: 32764556k total, 0k used, 32764556k free, 3612636k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
28894 root 22 0 1501m 405m 10m S 52.2 1.3 2534:16 java
The first five lines are the overall statistical information area of the current system status.
First line, task queue information, same as the execution result of the uptime command, specific parameter description is as follows:
14:06:23 — Current system time
up 70 days, 16:44 — The system has been running for 70 days, 16 hours, and 44 minutes (during which the system has not been rebooted!)
2 users — There are currently 2 users logged into the system
load average: 1.15, 1.42, 1.44 — The three numbers following load average are the load status for 1 minute, 5 minutes, and 15 minutes, respectively.
The load average data is calculated every 5 seconds by checking the number of active processes and then calculating a specific value. If this number divided by the number of logical CPUs is higher than 5, it indicates that the system is operating under overload.
Second line, Tasks — processes, specific information description is as follows:
There are currently 206 processes in the system, with 1 running, 205 in sleep mode, 0 in stopped state, and 0 in zombie state.
Third line, CPU status information, specific attribute description is as follows:
5.9%us — Percentage of CPU used by user space.
3.4% sy — Percentage of CPU used by kernel space.
0.0% ni — Percentage of CPU used by processes that have changed priority.
90.4% id — Percentage of idle CPU.
0.0% wa — Percentage of CPU waiting for IO.
0.0% hi — Percentage of CPU used by hardware interrupts (Hardware IRQ).
0.2% si — Percentage of CPU used by software interrupts (Software Interrupts).
Note: The concept of CPU usage here is different from that in Windows and requires an understanding of the user space and kernel space in Linux systems!
Fourth line, memory status, specific information is as follows:
32949016k total — Total physical memory (32GB)
14411180k used — Total memory in use (14GB)
18537836k free — Total free memory (18GB)
169884k buffers — Memory used for caching (169M)
Fifth line, swap partition information, specific information description is as follows:
32764556k total — Total swap space (32GB)
0k used — Total swap space in use (0K)
32764556k free — Total free swap space (32GB)
3612636k cached — Total cached swap space (3.6GB)
Sixth line, blank.
Seventh line and below: Status monitoring of each process (task), column information description is as follows:
PID — Process ID
USER — Process owner
PR — Process priority
NI — Nice value. Negative values indicate high priority, positive values indicate low priority.
VIRT — Total amount of virtual memory used by the process, in kb. VIRT=SWAP+RES
RES — Size of physical memory used by the process that has not been swapped out, in kb. RES=CODE+DATA
SHR — Size of shared memory, in kb
S — Process status. D=Uninterruptible sleep state R=Running S=Sleep T=Tracked/Stopped Z=Zombie process
%CPU — CPU usage percentage since the last update
%MEM — Physical memory usage percentage by the process
TIME+ — Total CPU time used by the process, in 1/100 seconds
COMMAND — Process name (command name/command line)
top interactive commands
h Display help information for top interactive commands
c Toggle between displaying command names and complete command lines
m Sort by memory usage
P Sort by CPU usage percentage
T Sort by time/cumulative time
W Save current settings to ~/.toprc file
o or O Change the order of displayed items
30. kill command
Sends a specified signal to the corresponding process. If no signal is specified, it sends SIGTERM (15) to terminate the specified process. If the process cannot be terminated, the "-KILL" parameter can be used, which sends the SIGKILL (9) signal to forcefully end the process. The process ID can be viewed using the ps command or the jobs command. The root user can affect processes of any user, while non-root users can only affect their own processes.
Common parameters:
-l Signal, if no signal number parameter is added, the "-l" parameter will list all signal names
-a When processing the current process, do not limit the correspondence between the command name and the process ID
-p Specify that the kill command only prints the process ID of the related process without sending any signals
-s Specify the signal to be sent
-u Specify the user
Example:
(1) First use ps to find the process pro1, then use kill to terminate it
kill -9 $(ps -ef | grep pro1)
31. free command
Displays the system memory usage, including physical memory, swap area memory, and kernel buffer memory.
Command parameters:
-b Display memory usage in bytes
-k Display memory usage in kilobytes
-m Display memory usage in megabytes
-g Display memory usage in gigabytes
-s<interval seconds> Continuously display memory usage
-t Display total memory usage
Example:
(1) Display memory usage
free
free -k
free -m
(2) Display memory usage in total
free -t
(3) Periodically query memory usage
free -s 10
>
Original article: https://www.cnblogs.com/gaojun/p/3359355.html
** Click to share notes
-
-
- ```