▼Click the image below to search for the code【001】, and receive154-page Linux study notes.

cd
Change directory:
> cd ../ # Change to the parent directory
> cd /tmp # Change to the /tmp directory
> cd ~ # Change to the current user's home directory
ls command
Command to view files and directories, abbreviation for list:
> ls -l # List long data strings, including file attributes and permission data
> ls -a # List hidden files
> ls -d # List the directory itself, rather than listing the directory's file data
> ls -lh # List file sizes in a more readable way (GB, kB, etc.)
> ls -lR # List contents along with subdirectories (recursive listing), equivalent to showing all files in that directory
grep command
Text search:
>grep -a # Search binary files as text files
>grep -c # Count the occurrences of the search string
>grep -i # Ignore case differences, treating upper and lower case as the same
>grep -v # Inverse selection, showing lines that do not contain the 'search string'
>grep -rl# Recursively query matching files in the directory
find command
Has powerful search capabilities:
> find / -name *.txt # Find all .txt files in the system
> find / -size +300M. # Find files larger than 300M in the system
> find . -perm 0755 # Find files with permission 0755 in the current directory
> find / -user rumenz # List files owned by rumenz
> find / -group rumenz # List files belonging to the group rumenz
> find / -size +300M -exec rm -rf {} \; # Find files larger than 300M in the system and delete them
cp command
File copy:
>cp -a # Copy files along with their attributes
>cp -p # Copy along with file attributes rather than using the default method, similar to -a, commonly used for backups
>cp -i # If the target file already exists, it will ask before overwriting
>cp -r # Recursively copy, used for directory copying
>cp -u # Only copy if the target file differs from the source file
>cp -rf # Forcefully overwrite copy the file
mv command
Used to move files:
>mv -f # Force means if the target file already exists, it will overwrite without asking
>mv -i # If the target file already exists, it will ask whether to overwrite
>mv -u # If the target file already exists and is newer, it will update
>mv -rf # Forcefully recursively copy files
rm command
Delete files or directories:
>rm -f # Means force, ignore non-existent files, no warning messages
>rm -i # Interactive mode, will ask the user whether to delete before proceeding
>rm -r # Recursive delete, most commonly used for directory deletion, it is a very dangerous parameter
>rm -rf / # This is a dangerous command that will cause the loss of all system files
>rm -rf rumenz/* # Force delete non-hidden files in the rumenz directory
>rm -rf rumenz/* # Force delete all files in the rumenz directory, including hidden files
ps command
View processes:
>ps aux # View all process data in the system
>ps ax # View all processes not related to the terminal
>ps -lA # View all process data in the system
>ps axjf # View part of the process tree status
kill command
Used to send a signal to a job (%jobnumber) or a PID (number):
>kill -signal PID
Common parameters for signals are as follows:
1: SIGHUP, restart terminated processes
2: SIGINT, equivalent to pressing ctrl+c, interrupt a program
3: SIGKILL, forcefully interrupt a process
4: SIGTERM, terminate a process normally
5: SIGSTOP, equivalent to pressing ctrl+z, pause a process
killall command
Used to send a signal to a process started by a command:
>killall -9 nginx # Directly end the nginx process
Tip: You can directly follow killall with the process name, no need to find the process PID like with kill.
file command
This command is used to determine the basic data of the file following the file command, as file types in Linux are not distinguished by suffixes, so this command is very useful for us.
> file rumenz.txt # Check the file type of rumenz.txt
rumenz.txt: ASCII text
> file /usr/bin/ls # Check the file type of the command ls
/usr/bin/ls: ELF 64-bit LSB executable
> file -i rumenz.txt # Check the MIME type of the file rumenz.txt
rumenz.txt: text/plain; charset=us-ascii
> file -z redis-5.0.8.tar.gz # Attempt to interpret the contents of the compressed file
redis-5.0.8.tar.gz: POSIX tar archive
tar command
This command is used to package files, by default it does not compress; if corresponding parameters are specified, it will also call corresponding compression programs (such as gzip and bzip2) for compression and decompression.
-c Create a tar package, abbreviation for create
-t View the files in the packaged (tar) file
-x Unpack a tar package or decompress (tar.gz), can be paired with -C (uppercase) to specify the directory to extract to, abbreviation for extract
-j Compress or decompress using bzip2
-J Compress or decompress using xz
-z Compress or decompress using gzip
-v Show the execution process. Abbreviation for verbose
-f filename: filename is the file to be processed
-C Specify the directory for compression and extraction
--- tar
> tar -cvf rumenz.tar rumenz.txt. # Package rumenz.txt into rumenz.tar, default not compressed
> tar -xvf rumenz.tar # Unpack to the current directory
> tar -xvf rumenz.tar -C /tmp # Unpack to the tmp directory
--- tar.gz
> tar -czvf rumenz.tar.gz rumenz.txt # Compress rumenz.txt into rumenz.tar.gz
> tar -xzvf rumenz.tar.gz # Unpack rumenz.tar.gz to the current directory
> tar -xzvf rumenz.tar.gz -C /tmp # Unpack rumenz.tar.gz to the tmp directory
> tar -xzvf rumenz.tar.gz rumenz.txt # Only unpack the rumenz.txt file
--- tar.bz2
> tar -cjvf rumenz.tar.bz2 rumenz.txt # Compress rumenz.txt into rumenz.tar.bz2
> tar -xjvf rumenz.tar.bz2 # Unpack rumenz.tar.bz2 to the current directory
> tar -xjvf rumenz.tar.bz2 -C /tmp # Unpack rumenz.tar.bz2 to the tmp directory
> tar -xjvf rumenz.tar.bz2 rumenz.txt # Only unpack the rumenz.txt file
---tar.xz
> tar -cJvf rumenz.tar.xz rumenz.txt # Compress rumenz.txt into rumenz.tar.xz
> tar -xJvf rumenz.tar.xz # Unpack rumenz.tar.xz to the current directory
> tar -xJvf rumenz.tar.xz -C /tmp # Unpack rumenz.tar.xz to the tmp directory
> tar -xJvf rumenz.tar.xz rumenz.txt # Only unpack the rumenz.txt file
cat command
This command is used to view the contents of text files, followed by the name of the file to be viewed, usually used with more and less.
> cat rumenz.txt # View the contents of rumenz.txt
> cat rumenz.txt | more # When the file content is large, it can be viewed in pages using more
Use cat to add content to a file cat >:
> cat > rumenz.sh << EOF
> 123
> 345
> rumenz.com
> EOF
EOF means end of file, use cat to append content to a file cat >>:
> cat >> rumenz.sh << EOF
> 111
> 222
> EOF
chgrp command
This command is used to change the user group of a file:
> chgrp rumenz rumenz.txt # Change the user group of rumenz.txt to rumenz
> chgrp rumenz -R ./tmp # Recursively change the user group of all files (including directories) in the tmp directory to rumenz
chown command
Modify the owner and group of a file:
> chown rumenz:rumenz rumenz.txt # Change the owner and group of rumenz.txt to rumenz, rumenz
> chown -R rumenz:rumenz rumenz/ # Change the owner and group of all files (including directories) under the rumenz directory to rumenz, rumenz
> chown rumenz rumenz.txt # Change the owner of rumenz.txt to rumenz
> chown :rumenz rumenz.txt # Change the group of rumenz.txt to rumenz
chmod command
This command is used to change file permissions, chmod can use u (user), g (group), o (other), a (all) and + (add), – (remove), = (set) to change file permissions in combination with rwx.
Numeric permissions are divided into three types: read (r=4), write (w=2), execute (x=1):
> chmod a+r rumenz.txt # Give read permission to all users for rumenz.txt
> chmod a-x rumenz.txt # Remove execute permission for all users of rumenz.txt
> chmod a+rw rumenz.txt # Give read and write permissions to all users for rumenz.txt
> chmod +rwx rumenz.txt # Give read, write, and execute permissions to all users for rumenz.txt
> chmod +x rumenz.sh # Give executable permission to rumenz.sh
> chmod -R a+r rumenz/ # Give read permission to all files (including directories) in the rumenz directory
> chmod -R a-x rumenz/ # Remove execute permission for all files (including directories) in the rumenz directory
> chmod 755 rumenz.txt # Equivalent to u=rwx (4+2+1),go=rx (4+1 & 4+1)
> chmod u=rwx,go=rx rumenz.txt # Equivalent to the previous line
vim command
i Insert before the cursor
I Insert at the beginning of the line
a Insert after the cursor
A Insert at the end of the line
o, O Open a new line
Esc Exit insert mode
:w Save
:wq, :x Save and exit
:q Exit (saved)
:q! Force exit
/string Search for the string
n Search for the next occurrence of the specified string
:%s/old/new/g Globally replace the specified string old with new
:n1,n2s/old/new/g Replace within a certain range, n1, n2 indicates line numbers
dd Delete a line
dw Delete a word
x Delete the next character
X Delete the previous character
D Delete the last character of a line
[N]yy Copy a line or N lines
yw Copy a word
p Paste
top command
The top command is a commonly used performance analysis tool in Linux that can display the resource usage status of various processes in real-time, similar to the task manager in Windows. After entering the top command, the commonly used shortcuts are as follows: d Specify the time interval between two screen information refreshes. Of course, users can use the s interactive command to change it.
p Monitor the status of a specific process ID.
q This option will make top refresh without any delay. If the calling program has superuser privileges, top will run with the highest possible priority.
S Specify cumulative mode.
s Run the top command in safe mode. This will remove the potential dangers brought by interactive commands.
i Prevent top from displaying any idle or dead processes.
c Display the entire command line instead of just the command name.
m Toggle display memory information.
t Toggle display process and CPU state information.
c Toggle display command name and full command line.
M Sort by resident memory size.
P Sort by CPU usage percentage.
T Sort by time/cumulative time.
q Exit the program.
> top -p pid # Display information for a specified process
> top -Hp pid # Display resource usage for all threads under pid process
time command, measure the execution time of a command (i.e., program):
> time ls # Measure the execution time of ls
real 0m0.004s
user 0m0.001s
sys 0m0.001s
Recently added courses:
Linux System Management | Linux Service Management | MySql Database Management | Shell Scripting | Python Introduction | Ansible Automation Operations | Enterprise KVM Virtualization | Kubernetes Container Orchestration | Docker | Large Website Clusters | ELK Log Center | Zabbix
▼ Click [Read Original] to grab a 14-day trial experience course quota