- Files and directories
1.
The cd command is used to switch the current directory. Its argument is the path of the directory to switch to, and it can be either an absolute path or a relative path.
cd /home enter the '/home' directory
cd .. go back to the previous directory level
cd ../.. go back up two directory levels
cd enter the personal home directory
cd ~user1 enter the personal home directory
cd - go back to the directory you were in last
2.
The pwd command displays the working path
[erik@erik ~]# pwd
/root
3.
The ls command, used to view files and directories; it means list
ls view the files in the directory
ls -l show detailed information about files and directories
ls -a list all files, including hidden files
ls -R list the contents of subdirectories as well (recursive listing), which means every file under that directory will be displayed
ls [0-9] display file names and directory names containing digits
4.
The cp command is used to copy files; it means copy, and it can also copy multiple files into one directory at once
-a :copy the file's characteristics along with it
-p :copy the file's attributes along with it instead of using the default approach; similar to -a, often used for backups
-i :if the target file already exists, ask before overwriting
-r :copy recursively and continuously, used for copying directories //recursive copying is used frequently
-u :copy only when the target file differs from the source file
5.
The mv command is used to move, rename or change the name of files and directories; it means move
-f :force, meaning that if the target file already exists it will be overwritten directly without asking
-i :if the target file already exists, you will be asked whether to overwrite it
-u :update only when the target file already exists and is newer than the target file
6.
The rm command is used to delete files or directories; it means remove
-f :means force, ignore files that don't exist and no warning message appears
-i :interactive mode, asks the user whether to proceed before deleting
-r :recursive deletion, most often used to delete directories; it is a very dangerous argument
- Viewing file contents
1.
The cat command is used to view the contents of a text file; follow it with the file name to view. It is usually combined with more and less through a pipe
cat file1 view the file contents forward starting from the first byte
tac file1 view a file's contents in reverse starting from the last line
cat -n file1 show the file's line numbers
more file1 view the contents of a long file
head -n 2 file1 view the first two lines of a file
tail -n 2 file1 view the last two lines of a file
tail -n +1000 file1 start displaying from line 1000, showing everything after line 1000
cat filename | head -n 3000 | tail -n +1000 display lines 1000 to 3000
cat filename | tail -n +3000 | head -n 1000 start from line 3000 and display 1000 lines (that is, display lines 3000~3999)
- File searching
1.
The find command is used to search the system
find / -name file1 start from '/' and enter the root file system to search for files and directories
find / -user user1 search for files and directories belonging to the user 'user1'
find /usr/bin -type f -atime +100 search for executable files that haven't been used in the past 100 days
find /usr/bin -type f -mtime -10 search for files created or modified within 10 days
whereis halt show the location of a binary, source code or man page
which halt show the full path of a binary or executable file
2.
Delete files larger than 50M:
find /var/mail/ -size +50M -exec rm {} \;
- File permissions - use “+” to set permissions and “-“ to remove them
1.
The chmod command changes file/directory permissions
ls -lh show permissions
chmod ugo+rwx directory1 give the directory's owner (u), group (g) and others (o) read (r, 4), write (w, 2) and execute (x, 1) permissions
chmod go-rwx directory1 remove read, write and execute permissions on the directory from the group (g) and others (o)
2.
The chown command changes the file’s owner
chown user1 file1 change a file's owner attribute
chown -R user1 directory1 change a directory's owner attribute and at the same time change the attributes of all files in that directory
chown user1:group1 file1 change a file's owner and group attributes
3.
The chgrp command changes the user group a file belongs to
chgrp group1 file1 change the file's group
- Text processing
1.
The grep command analyzes the information on a line: if the information we need is in it, the line is displayed. This command is usually used with pipes, to filter and process the output of some commands, and so on
grep Aug /var/log/messages search for the keyword "Aug" in the file '/var/log/messages'
grep ^Aug /var/log/messages search for words starting with "Aug" in the file '/var/log/messages'
grep [0-9] /var/log/messages select all lines in the file '/var/log/messages' that contain digits
grep Aug -R /var/log/* search for the string "Aug" in the directory '/var/log' and the directories after it
sed 's/stringa1/stringa2/g' example.txt replace "string1" in the example.txt file with "string2"
sed '/^$/d' example.txt delete all blank lines from the example.txt file
2.
The paste command
paste file1 file2 merge the contents of two files or two columns
paste -d '+' file1 file2 merge the contents of two files or two columns, separated by "+"
3.
The sort command
sort file1 file2 sort the contents of two files
sort file1 file2 | uniq take the union of two files (duplicate lines are kept only once)
sort file1 file2 | uniq -u delete the intersection and leave the other lines
sort file1 file2 | uniq -d take the intersection of two files (leaving only the lines that exist in both files)
4.
The comm command
comm -1 file1 file2 compare the contents of two files and delete only what 'file1' contains
comm -2 file1 file2 compare the contents of two files and delete only what 'file2' contains
comm -3 file1 file2 compare the contents of two files and delete only the part the two files share
- Packaging and compressing files
1.
The tar command packages files; by default it does not compress, but if the corresponding arguments are specified it will also call the corresponding compression program (such as gzip and bzip) to compress and decompress
-c :create a new package file
-t :view which file names the package file contains
-x :unpack or decompress; can be combined with -C (uppercase) to specify the extraction directory. Note that -c, -t and -x cannot appear in the same command at the same time
-j :compress/decompress with bzip2 support
-z :compress/decompress with gzip support
-v :during compression/decompression, display the file names being processed
-f filename :filename is the file to process
-C dir :specify the directory dir for compression/decompression
2.
Compress: tar -jcv -f filename.tar.bz2 the name of the file or directory to process Query: tar -jtv -f filename.tar.bz2 Extract: tar -jxv -f filename.tar.bz2 -C the directory to extract to
bunzip2 file1.bz2 decompress a file called 'file1.bz2'
bzip2 file1 compress a file called 'file1'
gunzip file1.gz decompress a file called 'file1.gz'
gzip file1 compress a file called 'file1'
gzip -9 file1 maximum compression
rar a file1.rar test_file create a package called 'file1.rar'
rar a file1.rar file1 file2 dir1 compress 'file1', 'file2' and the directory 'dir1' at the same time
rar x file1.rar extract the rar package
zip file1.zip file1 create a zip-format archive
unzip file1.zip extract a zip-format archive
zip -r file1.zip file1 file2 dir1 compress several files and directories into a single zip-format archive at the same time
System and shutdown (shutdown, restart and logout)
shutdown -h now shut down the system (1)
init 0 shut down the system (2)
telinit 0 shut down the system (3)
shutdown -h hours:minutes & shut down the system at a scheduled time
shutdown -c cancel a scheduled system shutdown
shutdown -r now restart (1)
reboot restart (2)
logout log out
time measure the execution time of a command (that is, a program)Process-related commands
1.
The jps command displays the current system’s java processes and their id numbers
jps (Java Virtual Machine Process Status Tool) is a command provided by JDK 1.5 that displays the pid of all current java processes. It is simple and practical, and very well suited to simply checking some basic information about the current java processes on a linux/unix platform.
2.
The ps command is used to select and output the running state of processes at a certain point in time; it means process
-A :display all processes
-a :all processes not related to a terminal
-u :processes of the effective user
-x :usually used together with the a argument, lists more complete information
-l :list PID information in a longer, more detailed form
ps aux # view all process data on the system
ps ax # view all processes not related to a terminal
ps -lA # view all process data on the system
ps axjf # view the state together with part of the process tree
3.
The kill command is used to send a signal to a job (%jobnumber) or a PID (a number); it is usually used together with the ps and jobs commands
Command format : kill[command arguments][process id]
Command arguments:
-l signals; if you don't add a signal number argument, using the "-l" argument will list all signal names
-a when handling the current process, do not restrict the correspondence between command names and process numbers
-p specify that the kill command only prints the process number of the relevant process without sending any signal
-s specify the signal to send
-u specify the user
Example 1: list all signal names Command: kill -l Output:
[root@localhost test6]# kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE
9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT
17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU
25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH
29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN
35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4
39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6
59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
Explanation:
Only the 9th signal (SIGKILL) can terminate a process unconditionally; processes are entitled to ignore all the other signals. The commonly used signals are listed below:
HUP 1 terminal hangup
INT 2 interrupt (same as Ctrl + C)
QUIT 3 quit (same as Ctrl + \)
TERM 15 terminate
KILL 9 force terminate
CONT 18 continue (the opposite of STOP, fg/bg commands)
STOP 19 pause (same as Ctrl + Z)
Example 2: get the numeric value of a specified signal
[erik@erik ~]# kill -l KILL
[erik@erik ~]# kill -l SIGKILL
[erik@erik ~]# kill -l TERM
[erik@erik ~]# kill -l SIGTERM
[erik@erik ~]#
Example 3: first use ps to find the process, then use kill to kill it
Command: kill 3268
[erik@erik ~]# ps -ef|grep vim
root 3268 2884 0 16:21 pts/1 00:00:00 vim install.log
root 3370 2822 0 16:21 pts/0 00:00:00 grep vim
[root@localhost test6]# kill 3268
Example 4: kill a process completely
Command: kill –9 3268 // -9 forcefully kills the process
The killall command sends a signal to the processes started by a command, and is used to kill processes with a specified name
Command format : killall[command arguments][process name]
Command arguments:
-Z kill only processes that have an scontext
-e require the process name to match
-I ignore lowercase
-g kill the process group instead of the process
-i interactive mode, ask the user before killing the process
-l list all known signal names
-q do not output warning messages
-s send the specified signal
-v report whether the signal was sent successfully
-w wait for the process to die
--help display help information
--version display version information
Examples
1: kill all processes with the same name
killall nginx
killall -9 bash
2. send a specified signal to a process
killall -TERM ngixn or killall -KILL nginx
The top command is a commonly used performance analysis tool under Linux. It can display the resource usage of each process in the system in real time, similar to Windows Task Manager.
How to kill a process:- Via the graphical interface
- kill -9 pid (-9 means force close)
- killall -9 the name of the program
- pkill the name of the program
View a process’s port number:
netstat -tunlp|grep port number
Reprinted from: https://mp.weixin.qq.com/s/qItOvBZDMUnAi5wntMeaZg

