[Notes] Basic knowledge and common commands of linux commands

Basic knowledge and common commands

Linux file and directory structure

Linux includes ordinary files, directories, and special files (such as symbolic link files, device files). Linux does not indicate its type according to the file extension, but recognizes its type according to the content of the file header. Extensions can be used for readability, but have no practical purpose. The Linux system has only one root directory, represented by a slash "/" .

Table of contents describe
/bin The place where the main executable files are stored, most of these executable files are the most commonly used commands in the Linux system
/boot Contains the kernel and other files used by the system to boot
/dev Store device files. The Linux system regards all peripherals as a file, and the operation on the file representing a certain device means the operation on the device
/etc Store system configuration files. There are many text files about configuration in this directory and subdirectories, such as files with the suffix conf
/home The system's default home directory for ordinary users is /home/<account_name>, which stores user files, including the user's own configuration files, documents, data, etc.
/lib Contains many shared library files used by programs in /bin and /sbin. /usr/lib contains more library files for user programs
/lost+found Files recovered during system repair
/mnt File system mount point. Generally used to install removable media, partitions of other file systems, network shared file systems, or any mountable file system
/opt Stores files and programs that can be optionally installed. Mainly used by third-party developers to install and uninstall their packages
/proc The files in this directory are not actually stored on the disk. The file system in this directory is called the memory image file system, which is the image of the system kernel. The files in this directory are stored in the system memory, you can check the running status of the system by viewing these files/sbin
/root The default home directory of the system administrator and superuser root
/sbin Executable files are stored here. The executable files here are mainly used by superusers to manage the system. Ordinary users have almost no authority to execute programs in them. /usr/sbin also contains many system commands
/tmp Temporary directory, when the system restarts, the files in this directory will be automatically emptied
/usr Including files and directories directly related to system users, some major applications are also stored in this directory
/was Contains files that change frequently, such as spool directories, log directories, lock files, temporary files, etc.

help, man (help)

  • 命令 --help, for example date --help, to obtain the detailed options and parameters of the date command. In the syntax of the command, the options or parameters in square brackets are optional (not mandatory), and the vertical bar means or.

  • man 命令, the help information provided by the man command is displayed in pages, press the Enter key to display the next line, press the space bar to display the next page, press the key backwards) to the previous page, and press the key to exit (quit)

ls (file listing command)

  • lsThe command is used to list (list) the files and subdirectories contained in the current directory, which is equivalent to the dir command of DOS. The option -l of the ls command is the most commonly used, and -l means long, which is used to list detailed information.

  • ls -lThe information listed has seven sections.

    • The first part has 10 characters, the first character indicates the type, " d " indicates the directory, " - " indicates the ordinary file;
    • The second part, for a file, indicates the number of hard links (see the ln command), and for a directory, indicates the number of subdirectories it contains, including hidden directories;
    • The third part indicates the owner of the file;
    • The fourth part indicates the group it belongs to;
    • The fifth part indicates the size, that is, the number of bytes;
    • The sixth part is the modification time of the file (what ls -luyou see is the access time , and ls -lcwhat you see is the state change time );
    • The seventh part is the file name itself.
  • The option -i or –inode of the ls command is used to display the inode number of the file, which is like a non-repeatable label, and each file or directory corresponds to an inode number.

  • ls -aOr --alluse it to view all files and directories under the directory, you will see hidden files, and there are two special things in hidden files, one is ". " and the other is " ... ". " . " indicates the current directory, " ... " indicates the parent directory of the current directory, that is, the upper level directory. Any directory under Linux must contain these two special hidden directories. These options can be used in combination. The option -t of ls means to list files from new to old according to the modification time. The ls -latfunction is to list all files including hidden files and hidden directories according to the modification time from new to old. Directory details.

insert image description here

Not all options of Linux commands can be combined arbitrarily, some options cannot be used together, and some options depend on other options.

cat, more, less, head, tail (browse file commands)

  • cat /etc/passwd, The cat command is to display the entire content of the file in one go. When the file is long, it is inconvenient to use the cat command to view the long file. It is best to use the more command.

  • The more command will display long files in pages, and each time a page is displayed, the more command will wait for further instructions from the user. When displaying files with more paging, the user’s commonly used commands are: press the Enter key to display the next line; press the space bar to display the next page; press the B key to return to the previous page; press the Q key to exit and no longer Check out the rest.

  • The less command, the use of the less command is similar to the more command, and can display long files in pages.

  • The head command is used to view the beginning of the file. By default, the first 10 lines of the file are displayed. You can use the option -n plus parameter K to display the first K lines of the file. -n K can be abbreviated as -K. For example, head -n 5 /etc/passwdequivalent to head -5 /etc/passwd.

  • The tail command echoes the head command. It displays the last 10 lines of the file by default. Use -n K or -K to display the last K lines of the file. The tail command has a more commonly used option -f, using it, you can see the growth or change of the content at the end of the file. When the content of a file is constantly changing (such as log files, when debugging scripts, debugging information printed by scripts, etc.), it can be run, and it will tail -f filenamedisplay the last content in filename on the screen and keep refreshing to display latest file content

wc (file statistics command)

The wc command is used to count the number of lines, word counts, and bytes of a file. Continuous non-blank characters are a word (blank characters include spaces, keys, newlines, etc.), and each letter, space, newline (\n) and punctuation mark occupies one byte.

  • wc /etc/passwd, to display the line, word book, number of bytes, and file name in sequence, for example: 28 50 1306 /etc/passwd , which means 28 lines, 50 words, and 1306 bytes.
  • wc -l /etc/passwd, showing the number of lines in the file, for example: 28 /etc/passwd , indicating 28 lines.
  • wc -w /etc/passwd, to display the number of words in the file, for example: 50 /etc/passwd , indicating 50 words.
  • wc -c /etc/passwd, to display the number of bytes in the file, for example: 1306/etc/passwd , indicating 1306 characters.
  • wc -m /etc/passwd, to display the number of characters in the file, and the number of characters is consistent with the number of bytes. If a file contains full-width punctuation or Chinese characters, its number of characters and bytes will be inconsistent.

cd (change current working directory command)

The cd command is used to change the current working directory (change working directory), or the cd command is used to enter a certain directory.

  • Assuming that the current directory is **/home/kiramario**, run it cd shell(provided that there is a shell directory under the current directory), it will enter **/home/kiramario/shell**.
  • "..." indicates the parent directory, cd ..which means entering the parent directory. For example, the current directory is **/home/kiramario/shell**. After running the command, the current directory is **/home/kiramario**, .../... indicates the parent directory The parent directory of , can be deduced all the time.
  • The parameters of cd are all relative paths, and absolute paths (full paths starting from the root directory) can also be used, for example , directly enter /usr/bin** cd /usr/bin/from the current directory **/home/kiramario **.
  • Use the parameter "-" to return to the previous path. In the previous example, before entering **/usr/bin**, the path was **/home/kiramario**, so the operation cd -returned to the **/home/kiramario** directory.
  • The cd command without any options and parameters means to enter the home directory of the current account. Suppose the current account name is user, and the home directory is **/home/user**. For example, my user name is kiramario , and my home directory is **/home/kiramario**.
  • ~<account_name> means the main directory of the account account_name, for example cd ~kiramario, means to enter the main directory of the account. cd ~and cdhave the same effect, but in a script, cd ~ has obvious meaning and better readability.

The cd command can use relative paths and absolute paths, depending on the situation. The concepts of relative paths and absolute paths are relatively simple and can be grasped with a little searching and thinking.

mkdir (create directory command)

  • The mkdir command is used to create directories (make directories). For example, create a directory named dir1 under the current directory mkdir dir1.

  • The mkdir command can create two or more directories at the same time, for example, create directories dir1, dir2, dir3, under the current directory mkdir dir1 dir2 dir3.

  • When the mkdir command creates a deep directory, it needs to be created layer by layer, and the steps are cumbersome. At this time, you can speed up the option -p or –parents to quickly create, for example: , **/home/kiramario/ will be generated in mkdir -p /home/kiramario/test1/test2/test3sequence test1**, /home/kiramario/test1/test2 , /home/kiramario/test1/test2/test3 directories, all levels of directories in the mkdir -p command may not exist, and some levels or levels may already exist , or they may already exist. In these cases, the mkdir -p command will not make an error. From the upper-level directory to the last-level directory, create any level of directory that does not exist. If they already exist, it is equivalent to doing a no-op with mkdir -p.

The parameter of mkdir can be a relative path, or it can be a felt path.

cp (copy command)

The cp command is used to copy files and directories, also called "copy". Its most basic command format has two parameters, namely "source" and "destination". Use the option **-f** or **–force** to overwrite forcibly without encountering a prompt of insufficient permissions. Use the option -i or –interactive to get a prompt before a file is overwritten. At this time, input y or Y, press the Enter key, and the copying operation will continue; press Enter directly or press the <Ctrl+C> key to cancel the copying action.

  • Copy (save) a.txt as b.txt in the same directory: cp a.txt b.txt .

  • Copy a.txt to the /tmp directory: cp a.txt /tmp . It should be noted that when copying a.txt to the /tmp/dir1 directory, run it cp a.txt /tmp/dir1. When the directory /tmp/doc exists, copying is fine. When the directory /tmp/dir1 does not exist (and /tmp definitely exists), a.txt is copied as a file named dir1 under the /tmp directory.

  • Copy a.txt to the /tmp directory, and change the name to e.txt: cp a.txt /tmp/e.txt .

  • Copy files in other directories to the current directory: cp /tmp/a.txt .or cp /tmp/a.txt ./.

  • Copy multiple files distributed in different directories to a certain directory, for example: cp ../mike/a.txt ../jack/b.txt /tmp/c.txt /home/kiramario/shell.

  • The command cp -r dir1 dir2is used to copy the directory, and copy the contents of the directory dir1 to the directory dir2. -r stands for recursively, which is to copy recursively. The option -r of the cp command can also be replaced with -R or –recursive. These three options have the same effect. There may not only be files in dir1, but also subdirectories, and even multi-layer subdirectories, which will be copied to dir2 intact. If dir2 does not exist, it will be created first.

  • The option -b of the cp command has a backup function (backup), and the name of the existing target file will automatically add a tilde ~ and be backed up. Consider this scenario, the file b.txt exists, after executing cp a.txt b.txt, the original content of b.txt disappears and is replaced by the content of a.txt. After execution cp -b a.txt b.txt, b.txt is backed up as b.txt~, and the content of a.txt overwrites b.txt.

mv (rename or move command)

The basic format of the mv command is mv file1 file2, mv means move, and file1 and file2 are "source" and "destination" respectively. Source and destination can be files and files, files and directories, or directories and directories, but not directories and files .

When executing the command mv file1 file2, you may also encounter the same situation as the cp command due to insufficient permissions. The processing method and the use of options **-i** and **-f** are exactly the same as cp. For example, when moving the file ac into the directory sourcecode, if there is already a file with the same name in the directory sourcecode, use the option **-i** and you will be prompted.

  • mv a.txt b.txt: Rename the file a.txt to b.txt.
  • $ mv a.c sourcecode: To move the file ac into the sourcecode directory, first ensure that the directory sourcecode exists, otherwise, ac will be renamed to the file sourcecode.
  • mv a.txt b.txt c.txt /tmp : Move three files into a certain directory.
  • mv ../mike/a.txt ../jack/b.txt /tmp/c.txt /home/maggie/document: Move three files distributed in different directories into a certain directory.
  • mv test1 test2This command may have two results: if the directory test2 does not exist, the directory test1 is renamed to test2; if the directory test2 exists, the directory test1 is moved into test2, and test1 becomes a subdirectory of test2.

ln (Create symbolic links and hard links command)

  • ln -s a.txt sym_a.txt: ln -s target file (source file) link file, when creating a symbolic link, the option -s or –symbolic is required. Create a symbolic link named sym_a.txt with the target file a.txt. Use the ls command to check that the first character of the line where sym_a.txt is located is a lowercase letter l, and l indicates that the file type is a symbolic link. If the source file a.txt does not exist (this situation is called broken link), the command can also be executed successfully.

  • ln -s /home/kiramario/shell sys_shell: Create a symbolic link against a directory

insert image description here

  • ln a.txt a_hard.txt: means to create a hard link of a.txt, modify the content of a.txt (or a_hard.txt), then a_hard.txt (or a.txt) will also change accordingly, and their content and modification time are always consistent , in effect, they are hard links to each other. If ls -lyou check it, you will find that the value of the second part of the information is 2, indicating the number of hard links. The first character of the information in a_hard.txt is "-" instead of a lowercase letter l, indicating that it is an ordinary file. If you create another hard link to a.txt, this number will become 3. It should be noted that before establishing a hard link, the "source" file must exist, otherwise an error will occur.

insert image description here

If a file is accidentally deleted, but its hard link file still exists, then the content of the "source" can be easily retrieved. As for symbolic links, if the "source" is accidentally and completely deleted, its contents are almost impossible to retrieve. If you want the content of two or more files that are not necessarily in the same directory to always be consistent, and you want to back up each other, you can use hard links. A hard link is: one file, multiple file names, each time a linked file is deleted, the name will be reduced by one (the number of links will be reduced by 1), and the file will not be deleted until the last link is deleted.

Hard links have a limitation, the files linked together must be in the same file system *(view with df)*, otherwise, when linking them, the ln command will make an error, and symbolic links do not have this limitation, some Linux does not allow Create a hard link to the directory. Some Linux only allows super accounts (such as root) to create a hard link to the directory.

pwd (display current directory command)

Useful options for the pwd command, -P and -L. pwd -P shows the actual path, that is, the physical path; pwd -L shows the "apparent" path, that is, the logical path. If the current directory and its parent directories at all levels are not symbolic links, then the current physical path and logical path are the same, otherwise they are different.

If it is a symbolic link of a directory, enter this directory, run pwd -Lto display the directory of this symbolic link, and pwd -Pdisplay the real path. For example, sys_shell shown in the picture above is a symbolic link to /home/kiramario/shell.
insert image description here

touch (generate empty file or change file timestamp command)

  • touch z.txt: Run the touch file command, if file does not exist, an empty file file will be generated. For example, make sure that z.txt does not exist, and then run touch z.txt, a file z.txt with 0 bytes will be created. Regardless of whether the file exists in advance, after running the touch file command, the modification time of the file is set to the current time. The touch command without options sets the access time, modification time and state change time of the file to the current time, and the access time is set with the option **-a**; the access time is set with the option **-m** Set the modification time.

    The touch command can take a time parameter to set the modification time of the file to a specific time.

whoami, id, and groups (commands to view account names and groups they belong to)

  • whoami: View the current account name.
  • id -un: View the current account name.
  • groupsThe first group in the output of the :groups command is called the primary group, and the others are called the secondary groups.
  • id -a: Query which groups the current account belongs to, and at the same time check the account number uid and the group number gid of each group.

To query which groups other accounts belong to, just include the account name parameter. For example, to query which groups the account kiramario belongs to, run groups kiramarioor id kiramario.

File and directory permissions

Execute the command ls -l to view the 9 characters after the first part of the file to indicate the file permission. r, w, and x indicate read, write, and execute permissions respectively, and **"-"** indicates no corresponding permission. The 2nd to 4th characters represent the permissions of the file owner; the 5th to 7th characters represent the permissions of the group to which the file belongs; the 8th to 10th characters represent the permissions of all other accounts.

For example, rw-r–r– indicates that the owner has read and write permissions on the file, and other accounts have read permissions; rwxr-x— indicates that the owner has read, write and execute permissions on the file, and members of the group they belong to have read and execute permissions permissions, other accounts do not have any permissions.

chmod (change permissions command)

The chmod command is used to change the permissions of files and directories. The options are: "[ ugoa ] [ ±= ] [ rwx- ]" , where u represents the owner of the file (user), g represents the group to which the file belongs (group), o Indicates other accounts (other) except the owner and group members, a indicates all accounts (all), +, -, = indicate the increase, decrease, and definition of permissions respectively.

  • chmod u+rw a.txt: Add read and write permissions to the owner of the file.
  • chmod o+rwx a.txt: Add read, write and execute permissions to other accounts.
  • chmod o-rwx a.txt : Remove the read, write and execute permissions of other accounts.
  • chmod g=r a.txt: Assign read permission to the group to which the file belongs.
  • chmod a=rwx a.txt: Grant read, write and execute permissions to all accounts.
  • chmod g=rx a.txt: Assign read and execute permissions to the group to which the file belongs.
  • chmod go=- a.txt : Remove all permissions of the group to which it belongs, and remove all permissions of other accounts.
  • chmod a=- a.txt: Remove all permissions of all accounts.

There is also a "value" method to change the permissions of the file. r, w, x, and - can be represented by 4, 2, 1, and 0 respectively, and they can form 8 kinds of values ​​from 0 to 7, which represent different authority controls. For example, 3=2+1 means and only means -wx, 7=4+2+1 means and only means rwx, etc., see Table 2-2. Use chmod ugo filethe command to control the permissions of the file. The ugo here is different from the previous usage. u, g, and o can take any value from 0 to 7, which respectively represent the permissions of the owner, group, and other accounts of the file.

  • chmod 444 a.txt : 4 means r–, then 444 means r–r–r–, assign the read permission of the file to all accounts.

  • chmod 012 a.txt: Cancel all permissions of the current user u , set the execution permission of the group g to which it belongs, and set the write permission of other accounts o .

    value Corresponding permissions
    0
    1 –x
    2 -w-
    3 -wx
    4 r–
    5 r-x
    6 rw-
    7 rwx

If we just want to change the permissions of the directory, use chmod without adding any parameters. If you want to change the files and subdirectories under the directory at the same time, you need to use the **-R** parameter.

SUID (other accounts have the permissions of the file owner when executing)

SUID permission, its manifestation is that s can be seen in the file owner's execution permission, instead of x, it is SUID, SUID is the abbreviation of set uid, SUID only works on executable binary files .

SUID only works for executable binaries. Its role is: other accounts have the authority of the file owner when executing . Let's take the command passwd as an example to understand the function of SUID. Executing the command passwd is to execute /usr/bin/passwd. After changing the password, the file **/etc/shadow** will be updated, because the encrypted password is in /etc/shadow. After checking its permissions, I found that only root has read and write permissions.

-rw-r----- 1 root shadow 991 Feb 5 2021 /etc/shadow 

However, any account can update the content of /etc/shadow (because any account can run passwd to change its own password), because the execution permission of passwd is set to s.

-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /usr/bin/passwd

Therefore, when other accounts execute the passwd command, they obtain root privileges and have write operations on /etc/shadow.

  • chmod u+s a.sh: Add SUID attribute (+s) to file owner (u).
  • chmod 4755 a.sh : In fact, chmod can be followed by four octal numbers. When the first digit is 4, it means that the SUID attribute is set. It is expressed here -rwsr-xr-x.

Setting SUID should also ensure that the file has execute permission for the owner.

SGID (other accounts have the permissions of the group to which the file belongs when executing)

It is short for set gid. It is possible to set the SGID for an executable binary. The role of SGID is: other accounts have the permissions of the group to which the file belongs when executing. For specific understanding, please refer to SUID. Start by looking for executables with the SGID attribute ls -l /usr/bin | grep '^......s'.

---x--s--x    1 root nobody  382216 Aug  9  2019 ssh-agent
-r-xr-sr-x.   1 root tty      15344 Jun 10  2014 wall
-rwxr-sr-x    1 root tty      19536 Apr 13  2017 write

Any account can run the write command, and the group to which /usr/bin/write belongs is tty. When any account executes the write command, it temporarily has the permissions it has when it belongs to the tty group.

SGID can be applied to a directory. At this time, the function of SGID is: if any account can create a new file or a new subdirectory in the directory, the group to which the newly created file or subdirectory belongs is consistent with the group to which the directory belongs .

# 将/home/kiramario/shell加上为SGID。
drwxrwsr-x 2 kiramario kiramario 4096 Oct 23 13:02 shell

At this time, when using root to create a file in the shell, the group it belongs to is consistent with the parent directory.

-rw-r--r-- 1 root      kiramario  0 Oct 23 13:05 root_a1.txt # shell目录设置SGID后创建的文件
-rw-r--r-- 1 root      root       0 Oct 23 13:02 root_a.txt	# shell目录未设置SGID之前创建的文件
  • chmod g+s shell: Set the SGID for the shell.
  • chmod 2777 shell, chmod is followed by a four-digit octal number, and when the first digit is 2, it means setting the SGID attribute.

In the same way as SUID, setting the SGID attribute should also ensure that the directory or file has execute permission for the group it belongs to.

SBIT (sticky bit, anti-delete bit)

The attributes of SBIT can only be set for directories . In a directory where everyone has permissions, an account can no longer delete other people's files or directories at will.

For example, the permission of the directory /tmp/picture is 777, and all users can operate. User1 enters the file created by user1, and user2 enters to delete it. When SBIT is set, user2 cannot delete the file belonging to user1.

  • chmod o+t picture: Set SBIT for picture.
  • chmod 1777 picture: chmod is followed by four octal numbers, when the first one is 1, it means setting the SBIT attribute.

Same as SUID and SGID, setting the SBIT attribute first ensures that the directory has execute permissions for other accounts.

SUID, SGID and SBIT can be set at the same time. 4, 2, and 1 represent setting SUID, SGID, and SBIT respectively, and 4, 2, and 1 can form a value between 1 and 7. For example, 3=2+1, 3 means setting SGID and SBIT at the same time; 6=4+ 2, 6 means to set SUID and SGID at the same time. For example:chmod 3777 picture

chown (set file owner and file association group)

  • chown kirammario a.txt: Set the owner of a.txt to kiramario.
  • chown kiramario:kiramarioGroup a.txt: Set the owner of the file a.txt to kiramario, the group user kiramarioGroup.
  • chown -R kiramario:kiramarioGroup*: Set the owner of all files and subdirectories under the current directory to kiramario, the group user kiramarioGroup.
  • chown :1001 a.txt: Set the associated group of a.txt to 1001 (associated group ID), without changing the owner.

chgrp (change group ownership of a file or directory)

  • chgrp kiramario a.txt: Change the group attribute of the file a.txt to kiramario, if the file is a hard link, the corresponding hard link file attribute is also changed.
  • chgrp --reference=a.txt b.txt: Change the group attribute of b.txt to be consistent with that of a.txt. The **-reference=<reference file or directory>** option means to set all the groups of the specified file or directory to be the same as the group of the reference file or directory.

stat (three times to view files)

Access, Modify, and Change represent access time, modification time, and state change time, respectively. You can also use the ls -lu, ls -l, and ls -lc commands to view the access time, modification time, and status change time of the file respectively, but the time is not as accurate as stat.

When the permission, owner, group or hard link number of the file changes, that is, when the chmod, chown, chgrp or ln command is run, the state change time of the file will change. When the content of the file changes, that is, when the modification time changes, the state change time also changes accordingly.

  • stat b.txt

      File: ‘b.txt’
      Size: 0               Blocks: 0          IO Block: 4096   regular empty file
    Device: fd01h/64769d    Inode: 918256      Links: 1
    Access: (0644/-rw-r--r--)  Uid: ( 1000/kiramario)   Gid: ( 1001/kiramario)
    Access: 2021-10-23 11:35:52.422466385 +0800
    Modify: 2021-10-23 11:35:52.422466385 +0800
    Change: 2021-10-23 11:36:11.367606848 +0800
    
  • stat --printf="%x\n%y\n%z\n" b.txt, used to display the three times of the file. Among them, %x, %y and %z correspond to access time, modification time and state change time respectively.

    2021-10-23 11:35:52.422466385 +0800
    2021-10-23 11:35:52.422466385 +0800
    2021-10-23 11:36:11.367606848 +0800
    

rm and rmdir (delete commands)

  • rm a.txt b.txt c.log: Delete multiple files at once.
  • rm -f a.txt b.txt c.log: When you delete a read-only file, you will encounter a prompt. If you don’t want to see the “read-only/write-protected” prompt, that is, force delete after confirming, use **-f or –force ** .
  • rm -i a.txt b.txt c.log: When deleting a file, no matter what the permissions of the file are, if you want to always be prompted, you can use the option **-i** or **-interactive**.
  • rmdir temp: The rmdir command is used to delete directories, but it can only be used to delete empty directories.
  • rm -r temp: Use the rm command with options -r, or -R, or –recursive to delete non-empty directories directly. If you don't want to see the "read-only/write-protected" prompt, add the option -f, and the two options are combined into -rf: rm -rf temp .

vim, vi (text editor)

vim means vi improved, that is, the enhanced version of vi. Generally, vi is enough, if you want to use code highlighting, you can choose vim. Now, the vi of some Linux systems is vim.

vi has 3 modes: command mode, insert mode and last line mode.

Basic operation: Run vi test.txt to enter command mode. When you first enter vi, you are in command mode, press ** to enter insert mode, and you can enter content. Then press the key in the upper left corner of the keyboard to return to the command mode, then press the <:>** key to enter the bottom line mode, enter wq, and press the Enter key to save and exit.

  • Commands to save the file and exit vi :

    • :w : Save the file without exiting vi.

    • :w: readme.txt : Save to (save as) the readme.txt file.

    • :q: Exit the vi editor (if the file is not modified).

    • :q! : Exit the vi editor without saving.

    • :wq: Exit the vi editor and save the file.

  • Command mode to move the cursor:

    ** Key left, key down, key up, key right; space bar right, key left, key to move to the beginning of the next line, <->** key to move to the beginning of the previous line.

  • Common delete and undelete operations:

    • x: Delete the current character.

    • nx: Delete n characters starting from the cursor, n is an integer.

    • dd: Delete the current row.

    • ndd: Delete n rows including the current row, n is an integer.

    • u: Undo the last operation.

    • U: Undo all operations on the current row.

  • Operations for inserting text or lines:

    • a: Add text to the right of the current cursor position.
    • i : Add text to the left of the current cursor position.
    • A : Add text at the end of the current line.
    • I : Add text at the beginning of the line with non-blank characters in the current line.
    • O : Create a new line above the current line.
    • o : Create a new line below the current line.

insert image description here

echo (command for display)

  • echo -n morning, when there is option -n, its function is to remove the newline character at the end of the line (no trailing newline). When using echo to display parameters, there is a newline character at the end of the line by default.

    [kiramario@izm115efh0abcdefg31tccccz shell]$ echo Morning
    Morning
    [kiramario@izm115efh0abcdefg31tccccz shell]$ echo -n Morning
    Morning[kiramario@izm115efhabcdefg31tccccz shell]$ # Monring和随后的提示符在同一行
    
  • echo -e "123\c456789": When using the option -e, some backslash escape characters take effect in the echo command.

    123$ # 因为没有换行符,123 和命令提示符在同一行
    

    Escape characters for the echo command:

    • \a: Ring the bell.
    • \b:backspace.
    • \c: Cancel the output after this character.
    • \e:key.
    • \f: page change.
    • \n: new line.
    • \r:Enter key.
    • \t : key (horizontal).
    • \v: Vertical key.
    • \ : the backslash itself.
    • \0NNN ASCII: The code is a character corresponding to one to three octal numbers.
    • \xHH ASCII: The code is a character corresponding to one to two hexadecimal numbers.

file (view file type command)

  • file /bin/ls

    /bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=3d705971a4c4544545cb78fd890d27bf792af6d4, stripped
    

find (find command)

The usual format of the find command is: find [start directory] option and search condition [operation]. The command find defaults to recursive search, and every level of subdirectory starting from the initial directory will be searched. When no starting directory is specified, the search starts from the current directory by default. The default operation is -print, which is to display the found content on the screen.

  • find . -name "*.txt" -print : Search for files with the suffix .txt from the current directory.
  • find /tmp/code -name "test1.c" -mtime +3 -user kiramaio -perm 755 -type f: Starting from the /tmp/code directory, look for ordinary files (not symbolic links or other types) with the name test1.c, the last modification time is more than 3 days old, the owner is the account kiramaio, and the permission is 755.
  • find ! -newer a.txt : Find files older than a.txt or files with the same modification time as a.txt,! Indicates that the condition is negated, ! The negation function of can be used on other search conditions.
  • find shell -name "*.txt" -exec ls -l {} \;: Find the files with the suffix .txt in the shell directory and list them with the ls -l command. The operation after the find file is found by the command find, in addition to the default -print, the commonly used **"-exec command {} \;", {} represents the found file, the end is \;, pay attention to the difference between }** and \ There must be spaces in between.
  • find shell -name "*.txt" -ok rm {} \; : Delete the found file. When prompted, enter n to not delete the file, and enter y to delete the file.
  • find shell -name "*.txt" | xargs chmod 700: Change the permissions of the found files to 700. Use pipelines and xargs commands to operate on the found files.
  • find /export/Logs/ -type f -name "*log*" -mtime +30 -exec rm -rf {} \; Delete all files with ".log" 30 days ago in the /export/Logs/ directory.

Common options for the find command:

Options and parameters meaning
-name filename Search according to the name filename, you can use wildcards (for example, * represents multiple characters, ? represents one character)
-perm mode Find by permission mode
-user account_name Find files whose owner is an account, -user can also be followed by account number uid
-group group_name Find files whose group belongs to a certain group, -group can also be followed by the group number gid
-mtime {-n|+n} When using -n (+n), find files whose modification time is within n days (before) from now
-type {b|d|c|p|l|f} Find a certain type of file, b, d, c, p, l, and f represent block devices, directories, character devices, pipes, symbolic links, and ordinary files, respectively
-newer file Find files with a modification time newer than a certain file, ! -newer means negate

ps (display the status of the current process)

The command is used to display the status of the current process, similar to the task manager of windows.

  • ps aux | grep kafka
  • ps -ef | grep kafka
  • ps -ef|grep php|grep -v 'grep'

grep (find matching strings in files)

The grep command is used to search for files whose content contains the specified template style. If the content of a file is found to match the specified template style, the default grep command will display the column containing the template style. If no file name is specified, or the file name - is given , the grep command reads data from the standard input device.

  • grep test a*: Find files where files prefixed with "a" contain the string "test".
  • grep -r test/home/kirmario/shell: Recursively search for files containing "test" under "/home/kirmario/shell".
  • grep -v test *a*: Reverse search, find the lines that do not contain test in the file that contains a in the file name.

systemctl (Systemd's main command, used to manage the system)

  • systemctl reboot: Restart the system.
  • systemctl poweroff : Turn off the system and cut off the power.
  • systemctl halt: The CPU stops working.
  • systemctl suspend: Suspend the system.
  • systemctl hibernate: Put the system into hibernation.
  • systemctl hybrid-sleep: Put the system into interactive hibernation.
  • systemctl rescue: Boot into rescue state (single-user state).
  • systemctl enable crawler_test.service : Enable the custom service crawler_test to start automatically.
  • systemctl disable crawler_test.service : Make the custom service crawler_test not start automatically.
  • systemctl status crawler_test.service : View service status.
  • systemctl start crawler_test.service: Start the service.
  • systemctl stop crawler_test.service :Out of service.
  • systemctl restart crawler_test.service: Restart the service.

rpm (management package)

Queries for installed software on the system:

  • rpm -q 软件名:查询系统已安装的软件。
  • rpm -qa |more:分页查看系统中所有已经安装的包。
  • rpm -qf 文件名:查询一个已经安装的文件属于哪个软件包。
  • rpm -ql lynx 或者 rpmquery -ql lynx:查询已安装软件包都安装到何处。
  • rpm -qi 软件名:查询一个已安装软件包的信息。
  • rpm -qc 软件名:查看一下已安装软件的配置文件。
  • rpm -qd 软件名:查看一个已经安装软件的文档安装位置。
  • rpm -qR 软件名:查看一下已安装软件所依赖的软件包及文件。

对于未安装的软件包的查看:

  • rpm -qpi file.rpm:查看一个软件包的用途、版本等信息。
  • rpm -qpl file.rpm:查看一件软件包所包含的文件。
  • rpm -qpd file.rpm:查看软件包的文档所在的位置。
  • rpm -qpc file.rpm:查看一个软件包的配置文件。
  • rpm -qpR file.rpm:查看一个软件包的依赖关系

软件包的安装、升级、删除等:

  • rpm -vih file.rpm :这个是用来安装一个新的rpm 包。
  • rpm -Uvh file.rpm :这是用来升级一个rpm 包。
  • rpm -e lynx:如果有依赖关系,您也可以用–nodeps 忽略依赖的检查来删除。

date(时间查看)

  • date:输出Sun Oct 10 11:53:51 CST 2021

  • date +%F:输出2021-10-10

  • date +%T:输出11:57:08

  • cal: 输出
    insert image description here

scp(传输文件)

  • scp local_file remote_username@remote_ip:remote_folder
  • scp local_file remote_username@remote_ip:remote_file

df,du(查看磁盘空间)

  • df -h
  • du -ach * :这个能看到当前目录下的所有文件占用磁盘大小和总大小。
  • du -sh :查看当前目录总大小。
  • du -sh * :查看所有子目录大小。
  • du -sh | sort -nr :倒序查看文件大小。
  • du -t 100M /var :查看var目录下大于100M的文件。
  • du -t 100M :查看当前目录下大于100M的文件。
  • du -ch logs :查看当前目录下logs文件夹大小。
  • df -h :查看磁盘使用状况。

free(查看内存)

  • free -b :以Byte为单位显示内存使用情况。
  • free -k :以KB为单位显示内存使用情况。
  • free -m:以MB为单位显示内存使用情况
  • free -g:以GB为单位显示内存使用情况。
  • free -o :不显示缓冲区调节列。
  • free -s <间隔秒数>:持续观察内存使用状况。
  • free -t :显示内存总和列。
  • free -V :显示版本信息。

我们运营free的时候,发现了有buffer/cache。这里的buffer指Linux内存的:Buffer cache。这里的cache指Linux内存中的:Page cache。

磁盘的操作有逻辑级(文件系统)和物理级(磁盘块),这两种Cache就是分别缓存逻辑和物理级数据的。假设我们通过文件系统操作文件,那么文件将被缓存到Page Cache,如果需要刷新文件的时候,Page Cache将交给Buffer Cache去完成,因为Buffer Cache就是缓存磁盘块的。也就是说,直接去操作文件,那就是Page Cache区缓存,用dd等命令直接操作磁盘块,就是Buffer Cache缓存的东西。

Buffer(Buffer Cache)以块形式缓冲了块设备的操作,定时或手动的同步到硬盘,它是为了缓冲写操作然后一次性将很多改动写入硬盘,避免频繁写硬盘,提高写入效率。

Cache(Page Cache)以页面形式缓存了文件系统的文件,给需要使用的程序读取,它是为了给读操作提供缓冲,避免频繁读硬盘,提高读取效率。

Linux内核会在内存将要耗尽的时候,触发内存回收的工作,以便释放出内存给急需内存的进程使用。一般伴随系统IO飙高。内核要先对比cache中的数据和对应硬盘文件上的数据是否一致,如果不一致需要写回,之后才能回收。

wc(统计行数、单词数、字符个数,文件个数)

  • wc -l :统计有多少行。
  • wc -w: 统计有多少个单词。
  • wc -c: 统计有多少个字符。
  • ls | wc -w: 查看目录下有多少个文件及文件夹。
  • ls -l |wc -l: 查看文件夹下有多少个文件。
  • ls -l |grep ^-|wc -l: 查看文件的个数。

sudo, su(权限提升)

  • sudo -i:将普通用户提权。
  • su - 用户名:切换到别的用户。
  • sudo [命令]:用来执行需要提升权限(通常是作为 root 用户)的命令,例如sudo rm -f a.txt一个用户要能使用 sudo ,应该在 /etc/sudoers 文件里有一条跟该用户相关的信息。

tar 压缩解压缩文件

  • tar -cvzf xxx.tar myDir/ : 打包压缩一个 tar。
  • tar -xvzf xxx.tar: 解压一个tar。
  • tar -zxvf xxx.tar.gz -C /xxx/: 解压一个tar到目标位置。
  1. z:通过gzip支持压缩或解压缩。还有其他的压缩或解压缩方式,比如j表示bzip2的方式。
  2. x:解压缩。c是压缩。
  3. v:在压缩或解压缩过程中显示正在处理的文件名。
  4. f: f must be followed by the file name to be processed. That is to say, you cannot write tar -zxfv xxx.tar.gz like this.
  5. -C: Specify the directory, followed by the target directory.

cronb timed task

The crond command periodically checks for work to be performed every minute, and executes the work automatically if there is work to be performed. The newly created cron task will not be executed immediately, at least after 2 minutes. Of course, you can restart cron to execute it immediately (root authority is generally required, systemctl restart crond.service).

  • -e : Run a text editor to set the schedule, the default text editor is VI, set your usual editor in your .bashrc file, for example: , export EDITOR=vimother unfamiliar editors will not pop up during subsequent editing up.
  • -r: Delete the current schedule.
  • -l: List the current schedule.

Edit format: f1 f2 f3 f4 f5 program.

  • f1: minutes (0 - 59).
  • f2: hour (0 - 23).
  • f3: the day of the month (1 - 31).
  • f4: month (1 - 12)
  • f5: day of the week (0 - 6) (Sunday is 0)
  • program: Execute commands, such as an executable script /[somepath]/canExecute.py, date >> /tmp/test.txt(record the current date to test.txt).

Or check out the online cron expression editor.

Guess you like

Origin blog.csdn.net/kiramario/article/details/120932696