20 commands commonly used in linux (Part 2)

file command

This command is used to judge the basic data of the file after the file command. Because the file type is not divided later under Linux, this command is very useful to us.

> file rumenz.txt  #View the file type of rumenz.txt
rumenz.txt: ASCII text

> file /usr/bin/ls #View the file type of the command ls
/usr/bin/ls: ELF 64-bit LSB executable

> file -i rumenz.txt  #View the MiME type of the file rumenz.txt
rumenz.txt: text/plain; charset=us-ascii

> file -z redis-5.0.8.tar.gz #Try 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, files are not compressed. If corresponding parameters are specified, it will also call corresponding compression programs (such as gzip and bzip) to compress and decompress them.

  • -c create a new tar package, which is the abbreviation of create
  • -TVIEW t he package (tar) file
  • -x unpack tar package or decompress (tar.gz), which can be combined with - C (uppercase) to specify the decompressed directory, the abbreviation of extract
  • -j compress or decompress through bzip2
  • -J compressed or decompressed by xz
  • -z compress or decompress through gzip
  • -v displays the execution process. Abbreviation for verbose
  • -f filename: filename is the file to be processed
  • -C specifies the compressed and decompressed directory
--- tar

> tar -cvf rumenz.tar rumenz.txt. #Package rumenz.txt into rumenz.tar, which is not compressed by default
> tar -xvf rumenz.tar             #Unpack to current directory
> tar -xvf rumenz.tar -C /tmp     #Unpack to tmp directory

--- tar.gz
> tar -czvf rumenz.tar.gz  rumenz.txt #Compress rumenz.txt into rumenz.tar.gz
> tar -xzvf rumenz.tar.gz             #Unzip rumenz.tar.gz to the current directory
> tar -xzvf rumenz.tar.gz -C /tmp     #Unzip rumenz.tar.gz to the tmp directory
> tar -xzvf rumenz.tar.gz rumenz.txt  #Unzip only 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             #Unzip rumenz.tar.bz2 to the current directory
> tar -xjvf rumenz.tar.bz2 -C /tmp     #Unzip rumenz.tar.bz2 to the tmp directory
> tar -xjvf rumenz.tar.bz2 rumenz.txt  #Unzip only 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              #Unzip rumenz.tar.xz to the current directory
> tar -xJvf ruemnz.tar.gz -C /tmp      #Unzip rumenz.tar.xz to the tmp directory
> tar -xJvf rumenz.tar.xz rumenz.txt  #Unzip only the rumenz.txt file

cat command

This command is used to view the contents of a text file, followed by the file name to view. It is usually used with more and less

> cat rumenz.txt  #View the contents of the rumenz.txt file
> cat rumenz.txt | more #When there are many contents in the file, you can view it through the more page

Using 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 add content to the file, cat > >

> cat >> rumenz.sh << EOF
> 111
> 222
> EOF

chgrp command

This command is used to change the user group to which the file belongs

> chgrp rumenz rumenz.txt     #Change the group of rumenz.txt file to rumenz
> chgrp rumenz -R ./tmp       #Recursively change the group of all files (including directories) in the tmp directory to rumenz 

chown command

Modify the owner and group of the 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) in the rumenz directory to rumenz and 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

The command is used to change the permissions of a file. chmod can use u (user), g (group), o (other), a (all), and + (join), - (delete), = (set) with rwx to change the permissions of a file.

There are three types of digital permissions: read (r=4), write (w=2) and execute (x=1)

> chmod a+r rumenz.txt    #Add read permissions to all users of rumenz.txt
> chmod a-x rumenz.txt    #Delete the execution permission of all users of rumenz.txt
> chmod a+rw rumenz.txt   #Add read and write permissions to all users of rumenz.txt
> chmod +rwx rumenz.txt   #Add read / write execution permission to all users of rumenz.txt
> chmod +x rumenz.sh      #Add executable permissions to rumenz.sh

> chmod -R a+r rumenz/    #Add read permission to all files (including directories) in rumenz directory
> chmod -R a-x rumenz/    #Delete the execution permission of all files (including directories) under 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 one

vim command

  • i insert in front of the cursor
  • I is inserted at the beginning of the line
  • a inserts after the cursor
  • Insert A to the last position of the line
  • o. O new line
  • Esc turn off insert mode
  • : w save
  • : WQ,: X save and close
  • : q close (saved)
  • :q! Force close
  • /String search string string. If you want to ignore case, set ic
  • n searches for the next position where the specified string appears
  • :% s/old/new/g full text replacement of the specified string old --- > New
  • : n1,n2s/old/new/g replace within a certain range, n1,n2 indicates the number of rows
  • dd delete a row
  • dw delete a word
  • x deletes the last character
  • X deletes the previous character
  • D delete the last character of a line
  • [N]yy copy one or n rows
  • yw copy a word
  • p paste

top command

top command is a commonly used performance analysis tool under Linux. It can display the resource occupation of each process in the system in real time, similar to the task manager of Windows.

Common shortcut keys after entering the top command

d Specify the time interval between screen information refreshes. Of course, users can use s Interactive commands to change it.
p Monitor processes by specifying ID To monitor only the status of a process.
q This option will make top Refresh without any delay. If the caller has superuser privileges, then top Will run at the highest possible priority.
S Specify cumulative mode
s send top The command runs in safe mode. This removes the potential danger posed by interactive commands.
i send top No idle or dead processes are displayed.
c Displays the entire command line, not just the command name
m Toggles the display of memory information. 
t Toggle display process and CPU Status information. 
c Toggles the display of the command name and the full command line. 
M Sort by resident memory size. 
P according to CPU Sort using percentage size. 
T According to time/Sort by cumulative time. 
q Exit the program. 
> top -p pid  # Displays the specified process information
> top -Hp pid # Displays the resource usage of all threads in the pid process

time command

Measure the execution time of a command (i.e. program)

> time ls #Count the execution time of ls
real    0m0.004s
user    0m0.001s
sys     0m0.001s

Original link: https://rumenz.com/rumenbiji/...
WeChat official account: entry station

Keywords: Linux

Added by php_gromnie on Wed, 03 Nov 2021 19:39:36 +0200