Common Linux Commands

1. ls Command

This is an abbreviation for “list”. The ls command can be used to view the files contained in a Linux directory, as well as to check file permissions (including directory, folder, and file permissions), and to view directory information, etc.

Common Parameter Combinations:

ls -a List all files in the directory, including hidden files that start with .
ls -A List all files except . and ..
ls -r Reverse order
ls -t Sort by file modification time
ls -S Sort by file size
ls -h Display sizes in a human-readable format
ls -l List detailed information about files, including permissions, owner, size, etc.

Examples:

(1) Sort by time in reverse order and display detailed file information in a human-readable format

ls -lhrt

(2) Display detailed information of files 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 syntax for the cd (change directory) command is:

cd [directory_name]

Description: Switch the current directory to dirName.

Examples:

(1) Enter the root directory

cd /

(2) Enter the “home” directory

cd ~

(3) Enter the last working path

cd -

(4) Use the parameter from the previous command as the cd parameter.

cd !$

3. pwd Command

The pwd command is used to view the current working directory path.

Examples:

(1) View the current path

pwd

(2) View the actual path of a symbolic link

pwd -P

4. mkdir Command

The mkdir command is used to create directories.

Available options:

  • -m: Set access permissions for the newly created directory, which can also be set using the chmod command;
  • -p: Can be a path name. If some directories in the path do not exist, this option will automatically create those directories, allowing multiple directories to be created at once.

Examples:

(1) Create a directory named t in the current working directory

mkdir t

(2) Create a path of test/t1/t in the tmp directory, creating it if it does not exist:

mkdir -p /tmp/test/t1/t

5. rm Command

Delete one or more files or directories in a directory. If the -r option is not used, rm will not delete directories. If rm is used to delete files, they can usually be restored.

rm [options] file...

Examples:

(1) Delete any .log files, asking for confirmation before each deletion:

rm -i *.log

(2) Delete the test subdirectory and all files within it without confirmation:

rm -rf test

(3) Delete files starting with -f

rm ---f*

6. rmdir Command

Remove one or more subdirectory entries from a directory. To delete a directory, you must also have write permission on its parent directory.

Note: Cannot delete non-empty directories

Examples:

(1) If the parent subdirectory is deleted, making it an empty directory, delete it as well:

rmdir -p parent/child/child11

7. mv Command

Move files or rename files. Depending on the type of the second parameter (if it is a directory, the file is moved; if it is a file, the file is renamed).

When the second parameter is a directory, the first parameter can be multiple files or directories separated by spaces, moving the specified multiple 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 files log1.txt, log2.txt, log3.txt to the root directory test3

mv log1.txt log2.txt log3.txt /test3

(3) Rename file1 to file2, asking whether to overwrite if file2 already exists

mv -i log1.txt log2.txt

(4) Move all files in the current folder to the parent directory

mv * ../

8. cp Command

Copy source files to target files, or copy multiple source files to target directories.

Note: When copying via command line, if the target file already exists, it will prompt whether to overwrite. However, in shell scripts, if the -i parameter is not added, it will not prompt and will directly overwrite!

-i Prompt
-r Copy directories and all items within them
-a Copy files with the same timestamp as the original

Examples:

(1) Copy a.txt to the test directory, keeping the original file’s timestamp, prompting if the original file exists

cp -ai a.txt test

(2) Create a link (shortcut) for a.txt

cp -s a.txt link_a.txt

9. cat Command

The cat command has three main functions:

1. Display the entire file at once:

cat filename

2. Create a file from the keyboard:

cat > filename

Can only create new files, cannot edit existing files.

3. Merge several files into one file:

cat file1 file2 > file
  • -b Number non-empty output lines
  • -n Output all line numbers

Examples:

(1) Add line numbers to the contents of log2012.log and input it into log2013.log

cat -n log2012.log log2013.log

(2) Append the contents of log2012.log and log2013.log with line numbers (excluding blank lines) to log.log

cat -b log2012.log log2013.log >> log.log

(3) Use here doc to generate a new file

cat > log.txt <<EOF
Hello
World
PWD=$(pwd)
EOF
ls -l log.txt
cat log.txt
Hello
World
PWD=/opt/soft/test

(4) Reverse display

tac log.txt
PWD=/opt/soft/test
World
Hello

10. more Command

Similar to cat, more displays content one page at a time, making it easier for users to read page by page. The basic command is to press the space bar (space) to display the next page, and press the b key to go back one page.

Command Parameters:

+n      Start displaying from line n
-n       Define screen size as n lines
+/pattern Search for the string (pattern) before displaying each file, then start displaying from two lines before that string
-c       Clear the top of the screen before displaying
-d       Prompt "Press space to continue, 'q' to quit", disabling the bell function
-l       Ignore Ctrl+l (page break) characters
-p       Page through the file by clearing the window instead of scrolling, similar to the -c option
-s       Display multiple consecutive blank lines as one line
-u       Remove underlines from the file content

Common Operation Commands:

Enter to move down n lines, needs to be defined. Default is 1 line
Ctrl+F   Scroll down one screen
Space bar to scroll down one screen
Ctrl+B  Return to the previous screen
= Output the current line number
:f     Output the file name and current line number
V      Call vi editor
! Command to call Shell and execute command
q       Exit more

Examples:

(1) Display content from the 3rd line of the file

more +3 text.txt

(2) Display detailed information of listed files, using a pipe to show 5 lines at a time

ls -l | more -5

Press space to display the next 5 lines.

11. less Command

Less is similar to more, but with less you can browse files freely, while more can only move forward and not backward. Additionally, less does not load the entire file before viewing.

Common Command Parameters:

-i  Ignore case when searching
-N  Display line numbers for each line
-o <filename> Save the output of less to the specified file
-s  Display consecutive blank lines as one line
/string: Search down for "string"
?string: Search up for "string"
n: Repeat the previous search (related to / or ?)
N: Reverse repeat the previous search (related to / or ?)
-x <number> Display the "tab" key as the specified number of spaces
b  Scroll back one page
d  Scroll back half a page
h  Display help interface
Q  Exit less command
u  Scroll forward half a page
y  Scroll forward one line
Space bar scrolls one line
Enter key scrolls one page
[pagedown]: Scroll down one page
[pageup]: Scroll up one page

Examples:

(1) View process information using ps and display it via less

ps -aux | less -N

(2) View multiple files

less 1.log 2.log

You can use n to view the next one, and p to view the previous one.

12. head Command

The head command is used to display the beginning of a file to standard output. By default, the head command prints the first 10 lines of the corresponding file.

Common Parameters:

-n<number> Display the specified number of lines (if the number is plural, it counts 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 content of a specified file. If no file is specified, it processes input information. Commonly used to view log files.

Common Parameters:

-f Continuously read (commonly used to view incrementing log files)
-n<number> Display the specified number of lines (counting from the end)

(1) Continuously read the gradually increasing file content

ping 127.0.0.1 > ping.log &

Run in the background: You can use jobs -l to view, or use fg to bring it to the foreground.

tail -f ping.log

(View log)

14. which Command

To find a file in Linux but do not know where it is located, you can use the following commands to search:

which     View the location of executable files.
whereis View the location of files.
locate  Use with a database to view file locations.
find        Actually search the hard drive for file names.

which searches for the location of a system command in the specified PATH and returns the first search result. Using the which command, you can see whether a system command exists and which command is being executed.

Common Parameters:

-n  Specify the length of the file name, which must be greater than or equal to the longest file name in 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 not found because cd is a built-in command, while which searches for commands in the PATH)

View the current PATH configuration:

echo $PATH

Or use env to view all environment variables and their corresponding 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 description files (parameter -m), and source code files (parameter -s). If parameters are omitted, it returns all information. Both whereis and locate are based on the system’s built-in database for searching, so they are very efficient, while find traverses the hard drive to find files.

Common Parameters:

-b   Locate executable files.
-m   Locate help files.
-s   Locate source code files.
-u   Search for files other than executable files, source code files, and help files in the default path.

Examples:

(1) Find files related to the locate program

whereis locate

(2) Find the source code files of locate

whereis -s locate

(3) Find the help files of locate

whereis -m locate

16. locate Command

Locate quickly finds files by searching the system’s built-in document database, which is updated by the updatedb program, called periodically by the cron daemon. By default, the locate command is faster than searching the entire hard drive for data, but it may not find files that were created or renamed recently. In the default setting, updatedb runs once a day, and the settings can be modified by changing crontab (etc/crontab).

Locate is similar to the find command and can use regex matching searches such as *, ? etc.

Common Parameters:

-l num (number of lines to display)
-f   Exclude specific file systems, such as excluding proc
-r   Use regular expressions as search criteria

Examples:

(1) Find all files related to pwd (file names containing pwd)

locate pwd

(2) Search for all files starting with sh in the etc directory

locate /etc/sh

(3) Find files in the /var directory that end with reason

locate -r '^/var.*reason$' (where . represents one character, * represents multiple tasks; .* represents any number of characters)

17. find Command

Used to search for files in the file tree and perform corresponding actions.

Command format:

find pathname -options [-print -exec -ok ...]

Command parameters:

pathname: The directory path that the find command searches for. For example, use . to represent the current directory, and / to represent the system root directory.
-print: The find command outputs matching files to standard output.
-exec: The find command executes the shell command given by the parameter on matching files. The corresponding command format is 'command'{} \;, note the space between {} and \;.
-ok: Works the same as -exec, but executes the shell command in a safer mode, prompting the user for confirmation before executing each command.

Command Options:

-name Search for files by file name
-perm Search for files by file permissions
-user Search for files by file owner
-group Search for files by file group.
-type Search for a specific type of file, 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 length 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 n*24 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 n*24 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 a minus sign - to limit the change time to files within n days, and a plus sign + to limit the change time to files older than n days).
-maxdepth n Maximum search directory depth
-prune Use this option to indicate directories to be ignored. Be careful when using the -prune option, as it will be ignored by the find command if you also use the -depth option.

Examples:

(1) Find files modified within the last 48 hours

find -atime -2

(2) Find files ending with .log in the current directory.. represents the current directory

find ./ -name '*.log'

(3) Find files in the /opt directory with permissions of 777

find /opt -perm 777

(4) Find files larger than 1K

find -size +1000c

Find files equal to 1000 characters

find -size 1000c

-exec parameter followed by command, which is terminated by a ; as an end marker, so this semicolon is essential. Considering that semicolons have different meanings in various systems, a backslash is added before it. The {} brackets represent the file names found by the previous find command.

Examples:

(5) Find files in the current directory that were changed more than 10 days ago and delete them (without prompt)

find . -type f -mtime +10 -exec rm -f {} \;

(6) Find all files ending with .log in the current directory, changed more than 5 days ago, and delete them, but prompt before deletion. Press y to delete the file, press n to not delete

find . -name '*.log' -mtime +5 -ok -exec rm {} \;

(7) Find files starting with passwd in the current directory that contain the string “pkg”

find . -type f -name 'passwd*' -exec grep "pkg" {} \;

(8) Use the exec option to execute the cp command

find . -name '*.log' -exec cp {} test3 \;

-xargs find command passes the matched files to the xargs command, while the xargs command only gets a portion of the files each time instead of all, unlike the -exec option. This way, it can first process the first batch of files obtained, then the next batch, and so on.

Examples:

(9) Find each regular file in the current directory, then use xargs to determine the file type

find . -type f -print | xargs file

(10) Find all files ending with js in the current directory that contain the string ‘editor’

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) Use the grep command to search for the word hostnames in all regular files in the current directory and highlight the line it is on:

find . -name '*' -type f -print | xargs grep -n 'hostnames'

(13) Find files in the current directory that start with a lowercase letter, end with 4 to 9, and end with .log:

find . -name '[a-z]*[4-9].log' -print

(14) In the test directory, find files not in the test4 subdirectory

find test -path 'test/test4' -prune -o -print

(15) Example 1: Find files newer than log2012.log but older than log2017.log

find -newer log2012.log ! -newer log2017.log

Using the depth option:

The depth option allows the find command to back up files on tape, hoping to back up all files first, and then back up files in subdirectories.

Example: The find command starts from the root directory of the file system to find a file named CON.FILE. It will first match all files and then enter subdirectories to search

find / -name "CON.FILE" -depth -print

18. chmod Command

Used to change the access permissions of files or directories in the Linux system. It controls the access permissions of files or directories. This command has two usages: one is the symbolic method with letters and operators; the other is the numeric method with numbers.

Each file or directory’s access permissions have three groups, each represented by three bits, which are the read, write, and execute permissions for the file owner; the read, write, and execute permissions for users in the same group as the owner; and the read, write, and execute permissions for other users in the system. You can use ls -l test.txt to find out.

For example, for the 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. In the usual sense, a directory is also a file. If the first character is a dash, it indicates a non-directory file. If it is d, it indicates a directory. From the second character to the tenth, the next 9 characters are grouped in 3s, representing the permissions for the 3 groups of users for the file or directory. Permission characters use a dash to represent no permission, r for read, w for write, and x for execute.

Common Parameters:

-c Report processing information when changes occur
-R Process all files in the specified directory and its subdirectories

Permission Range:

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 of the directory or file
a : All users and groups

Permission Codes:

r : Read permission, represented by the number 4
w : Write permission, represented by the number 2
x : Execute permission, represented by the number 1
- : Delete permission, represented by the number 0
s : Special permission

Examples:

(1) Add executable permission for all users to the file t.log

chmod a+x t.log

(2) Revoke all original permissions, then give the owner read permission, and output processing information

chmod u=r t.log -c

(3) Assign read, write, and execute (7) permissions to the owner of file, assign read and execute (5) permissions to the group of file, and assign execute (1) permissions to other users

chmod 751 t.log -c (or: chmod u=rwx,g=rx,o=x t.log -c)

(4) Add read permissions to all files in the test directory and its subdirectories

chmod u+r,g+r,o+r -R test/ -c

19. tar Command

Used to compress and decompress files. Tar itself does not have compression capabilities, only packaging capabilities; compression and decompression are completed by calling other functions.

Understand the two concepts: packaging and compression. Packaging refers to turning a large number of files or directories into a single file; compression refers to reducing a large file into a smaller file using some compression algorithm.

Common Parameters:

-c Create a new compressed file
-f Specify the compressed file
-r Add files to an already compressed file
-u Add modified and existing files to the compressed package
-x Extract files from the compressed package
-t Display the contents of the compressed file
-z Support gzip compression
-j Support bzip2 compression
-Z Support decompressing files
-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 the specified directory and use gz compression

tar -zcvf /tmp/etc.tar.gz /etc

(3) View the contents of the just packaged file (must add z because it is compressed using gzip)

tar -ztvf /tmp/etc.tar.gz

(4) To compress and package /home, /etc, but not /home/dmtsai

tar --exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc

20. chown Command

chown changes the owner of the specified file to the specified user or group, where the user can be a username or user ID; the group can be a group name or group ID; the file is a list of files to change permissions, separated by spaces, supporting wildcards.

-c Display information about the changed parts
-R Process the specified directory and subdirectories

Examples:

(1) Change the owner and group and display change information

chown -c mail:mail log2012.log

(2) Change the file group

chown -c :mail t.log

(3) Change the owner and group of the folder and its subdirectories to mail

chown -cR mail: test/

21. df Command

Displays disk space usage. Obtains information on how much space has been occupied on the hard drive and how much space is left. If no file name is specified, the available space of all currently mounted file systems will be displayed. By default, disk space will be 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 Display inode information
-k Block size is 1024 bytes
-l Only show local disks
-T List file system types

Examples:

(1) Display disk usage

df -l

(2) List all file systems and their types in a human-readable format

df -haT

22. du Command

The du command is also used to view space usage, but unlike the df command, the Linux du command is used to view the disk space used by files and directories:

Command format:

du [options] [file]

Common Parameters:

-a Display the size of all files in the directory
-k Display file size in KB
-m Display file size in MB
-g Display file size in GB
-h Display file size in a human-readable format
-s Only display the total
-c or --total  In addition to displaying the size of individual directories or files, also display the total of all directories or files

Examples:

(1) Display the size of the folder and its subfolders in a human-readable format

du -h scf/

(2) Display the size of all files in the folder in a human-readable format

du -ah scf/

(3) Display the size of several files or directories and also count their total

du -hc test/ scf/

(4) Output the space used by each subdirectory in the current directory

du -hc --max-depth=1 scf/

23. ln Command

The function is to create a synchronized link for a file at another location. When needed in different directories, there is no need to create the same file for each directory. Links created by ln (link) reduce disk usage.

Link Types: Soft links and hard links

Soft links:

  • 1. Soft links exist in the form of paths. Similar to shortcuts in Windows operating systems
  • 2. Soft links can cross file systems, while hard links cannot
  • 3. Soft links can link to a non-existent file name
  • 4. Soft links can link to directories

Hard links:

  • 1. Hard links exist in the form of file copies. But do not occupy actual space.
  • 2. Hard links do not allow creating links to directories
  • 3. Hard links can only be created within the same file system

Important Notes:

  • First: The ln command will keep every link file synchronized, meaning that no matter which one you modify, the other files will also change accordingly;
  • Second: ln links are divided into soft links and hard links. A soft link is created using ln -s source_file target_file, which will generate a file mirror at the selected location without occupying disk space. A hard link is created using ln source_file target_file without the -s parameter, which will generate a file of the same size as the source file at the selected location. Regardless of whether it is a soft link or a hard link, the files remain synchronized.
  • Third: The ln command is used to link files or directories. If two or more files or directories are specified at the same time, and the last destination is an existing directory, it will copy all previously specified files or directories to that directory. If multiple files or directories are specified and the last destination is not an existing directory, an error message will occur.

Common Parameters:

-b Remove and overwrite previously established links
-s Soft link (symbolic link)
-v Display detailed processing information

Examples:

(1) Create a soft link for a file and display operation information

ln -sv source.log link.log

(2) Create a hard link for a file and display operation information

ln -v source.log link1.log

(3) Create a soft link for a directory

ln -sv /opt/soft/test/test3 /opt/soft/test/test5

24. date Command

Displays or sets the system’s date and time.

Command Parameters:

-d<string> Display the date and time indicated by the string. The string must be enclosed in double quotes.
-s<string> Set the date and time according to the string. The string must be enclosed in double quotes.
-u  Display GMT.
%H Hour (00-23)
%I Hour (00-12)
%M Minutes (represented as 00-59)
%s Total seconds. The starting time is 1970-01-01 00:00:00 UTC.
%S Seconds (represented in local customary way)
%a Abbreviation of the week.
%A Full name of the week.
%d Date (represented as 01-31).
%D Date (including year, month, and day).
%m Month (represented as 01-12).
%y Year (represented as 00-99).
%Y Year (represented as four digits).

Examples:

(1) Display the next day

date +%Y%m%d --date="+1 day" // Display the date of the next day

(2) Using the -d parameter

date -d "nov 22" This year's November 22 is Wednesday
date -d '2 weeks' The date two weeks later
date -d 'next monday' (The date of next Monday)
date -d next-day +%Y%m%d (The date of tomorrow) or: date -d tomorrow +%Y%m%d
date -d last-day +%Y%m%d (The date of yesterday) or: date -d yesterday +%Y%m%d
date -d last-month +%Y%m (What month was last month)
date -d next-month +%Y%m (What month will next month be)

25. cal Command

Can display the Gregorian (solar) calendar. If there is only one parameter, it indicates the year (1-9999). If there are two parameters, it indicates the month and year:

Common Parameters:

-3 Display the calendar for the previous month, current month, and next month
-m Display Monday as the first column
-j Display the current year's day number
-y [year] Display the calendar for the current year [year]

Examples:

(1) Display the specified year and month date

cal 92012

(2) Display the calendar for each month of 2013

cal -y 2013

(3) Display the previous, current, and next month with Monday as the first column

cal -3 -m

26. grep Command

A powerful text search command, grep (Global Regular Expression Print) searches using global regular expressions.

The way grep works is that it searches for string patterns in one or more files. If the pattern includes spaces, it must be quoted, and all strings after the pattern are considered file names. The search results are sent to standard output without affecting the original file content.

Command format:

grep [option] pattern file|dir

Common Parameters:

-A n --after-context Display n lines after the matching character
-B n --before-context Display n lines before the matching character
-C n --context Display n lines before and after the matching character
-c --count Count the number of columns that match the pattern
-i Ignore case
-l Only list the names of files whose content matches the specified pattern
-f Read keywords from a file
-n Display the line number of the matching content in the file
-R Recursively search folders

grep’s regular expression:

^# Anchor the start of the line, e.g., '^grep' matches all lines starting with grep.
$  # Anchor the end of the line, e.g., 'grep$' matches all lines ending with grep.
.# Matches one non-newline character, e.g., 'gr.p' matches gr followed by any character, then p.
*# Matches zero or more preceding characters, e.g., '*grep' matches all lines with one or more spaces followed by grep.
.*# Together represents any character.
[]# Matches a character within a specified range, e.g., '[Gg]rep' matches both Grep and grep.
[^]# Matches a character not in the specified range, e.g., '[^A-FH-Z]rep' matches a line starting with a letter not in A-R and T-Z, followed by rep.
\(..\)  # Marks matching characters, e.g., '\(love\)' marks love as 1.
\<      # Anchors the start of a word, e.g., '\<grep' matches lines containing words starting with grep.
\>      # Anchors the end of a word, e.g., 'grep\>' matches lines containing words ending with grep.
x\{m\}  # Repeats character x, m times, e.g., '0\{5\}' matches lines containing 5 o's.
x\{m,\}  # Repeats character x, at least m times, e.g., 'o\{5,\}' matches lines with at least 5 o's.
x\{m,n\}  # Repeats character x, at least m times, no more than n times, e.g., 'o\{5,10\}' matches lines with 5 to 10 o's.
\w    # Matches word and number characters, i.e., [A-Za-z0-9], e.g., 'G\w*p' matches G followed by zero or more word or number characters, then p.
\W    # The inverse of \w, matches one or more non-word characters, such as punctuation.
\b    # Word boundary, e.g., '\bgrep\b' matches only grep.

Examples:

(1) Find a specified process

ps -ef | grep svn

(2) Find the number of specified processes

ps -ef | grep svn -c

(3) Read keywords from a file

cat test1.txt | grep -f key.log

(4) Recursively search for lines starting with grep in a folder and only list files

grep -lR '^grep' /tmp

(5) Find lines that do not start with x

grep '^[^x]' test.txt

(6) Display lines containing either ed or at

grep -E 'ed|at' test.txt

27. wc Command

The wc (word count) function counts the number of bytes, words, and lines in the specified file and outputs the statistics.

Command format:

wc [option] file..

Command Parameters:

-c Count bytes
-l Count lines
-m Count characters
-w Count words, defined as strings separated by whitespace, tabs, or newline characters

Examples:

(1) Find the number of lines, words, bytes, and file name

wc text.txt

Result:

7870     test.txt

(2) Count the number of lines in the output result

cat test.txt | wc -l

28. ps Command

The ps (process status) command is used to view the current running process status at once. If you need dynamic continuous results, use top.

There are 5 states for processes in Linux:

  • 1. Running (currently running or waiting in the run queue)
  • 2. Interrupted (sleeping, blocked, waiting for a condition to be met or receiving a signal)
  • 3. Uninterruptible (not waking up upon receiving a signal and cannot run, the process must wait until an interrupt occurs)
  • 4. Zombie (the process has terminated, but the process descriptor exists until the parent process calls wait4() to release it)
  • 5. Stopped (the process stops running after receiving SIGSTOP, SIGSTP, SIGTIN, SIGTOU signals)

The ps tool identifies the 5 states of a process with the following codes:

D Uninterruptible sleep (usually IO)
R Runnable (on run queue)
S Sleeping
T Traced or stopped
Z A defunct ("zombie") process

Command Parameters:

-A Show all processes
a Show all processes
-a Show all processes on the same terminal
c Show the real name of the process
e Show environment variables
f Show the relationship between processes
r Show processes running on the current terminal
-aux Show all processes including those used by others

Examples:

(1) Display the current environment variables of all processes and the relationships between processes

ps -ef

(2) Display all current processes

ps -A

(3) Use grep to find a specific process

ps -aux | grep apache

(4) Find the PID numbers related to the cron and syslog services

ps aux | grep '(cron|syslog)'

29. top Command

Displays relevant information about currently executing processes in the system, including process ID, memory usage, CPU usage, etc.

Common Parameters:

-c Display the full process command
-s Security mode
-p <process_number> Specify the process to display
-n <count> Number of times to loop display

Examples:

(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      2201501m405m10m S 52.21.32534:16 java

The first five lines are the overall statistics of the current system.

The first line, task queue information, is the same as the result of the uptime command. The specific parameter explanation is as follows:

14:06:23 — Current system time

up 70 days, 16:44 — The system has been running for 70 days and 16 hours and 44 minutes (during which the system has not restarted!)

2 users — Currently, there are 2 users logged into the system

load average: 1.15, 1.42, 1.44 — The three numbers after load average represent the load situation for 1 minute, 5 minutes, and 15 minutes.

The load average data is calculated by checking the number of active processes every 5 seconds and calculating the value according to a specific algorithm. If this number divided by the number of logical CPUs results in a value greater than 5, it indicates that the system is overloaded.

The second line, Tasks — Tasks (processes), specific information explanation is as follows:

The system currently has a total of 206 processes, of which 1 is running, 205 are sleeping, 0 are stopped, and 0 are in a zombie state.

The third line, CPU status information, specific attributes explanation is as follows:

5.9%us — Percentage of CPU occupied by user space.
3.4% sy — Percentage of CPU occupied by kernel space.
0.0% ni — Percentage of CPU occupied by processes with changed priority.
90.4% id — Percentage of idle CPU.
0.0% wa — Percentage of CPU occupied by IO wait.
0.0% hi — Percentage of CPU occupied by hardware interrupts (Hardware IRQ).
0.2% si — Percentage of CPU occupied by software interrupts (Software Interrupts).

Note: The CPU usage ratio here is different from the concept in Windows, and it is necessary to understand the relevant knowledge of user space and kernel space in the Linux system!

The 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 — Amount of cached memory (169M)

The fifth line, swap partition information, specific information explanation is as follows:

32764556k total — Total swap space (32GB)
0k used — Total swap space used (0K)
32764556k free — Total free swap space (32GB)
3612636k cached — Total buffered swap space (3.6GB)

The sixth line, empty line.

The seventh line and below: Status monitoring of each process (task), project column information explanation 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 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 — Shared memory size, in KB
S — Process status. D=Uninterruptible sleep state R=Running S=Sleeping T=Traced/Stopped Z=Zombie process
%CPU — Percentage of CPU time occupied since the last update
%MEM — Percentage of physical memory used by the process
TIME+ — Total CPU time used by the process, in units of 1/100 seconds
COMMAND — Process name (command name/command line)

Top Interactive Commands

h Display help information for top interactive commands
c Toggle display of command name and full command line
m Sort by memory usage
P Sort by CPU usage percentage
T Sort by time/accumulated time
W Write the current settings to ~/.toprc file
o or O Change the order of displayed items

30. kill Command

Send a specified signal to the corresponding process. If no type is specified, it will send SIGTERM (15) to terminate the specified process. If the program cannot be terminated, use the “-KILL” parameter, which sends the signal SIGKILL (9) to forcibly end the process. You can use the ps command or jobs command to view the process number. The root user will affect the processes of other users, while non-root users can only affect their own processes.

Common Parameters:

-l  Signal, if no signal number parameter is added, using the "-l" parameter will list all signal names
-a  When processing the current process, do not limit the correspondence between command names and process numbers
-p  Specify that the kill command only prints the process numbers of related processes without sending any signals
-s  Specify the signal to send
-u  Specify the user

Examples:

(1) First use ps to find the process pro1, then kill it

kill -9 $(ps -ef | grep pro1)

31. free Command

Displays the system memory usage, including physical memory, swap memory, and kernel buffer memory.

Command Parameters:

-b Display memory usage in Bytes
-k Display memory usage in KB
-m Display memory usage in MB
-g Display memory usage in GB
-s<interval seconds> Continuously display memory
-t Display total memory usage

Examples:

(1) Display memory usage

free
free -k
free -m

(2) Display memory usage information in total

free -t

(3) Periodically query memory usage

free -s 10

Leave a Comment