Linux private dishes -- I. Basic arrangement of Linux

Posted by -Karl- on Mon, 20 Dec 2021 07:33:38 +0100

This article comes from the study and arrangement of "bird brother's Linux private dishes", which is only for learning and use

·1, Basic command

1.1 date

[root@iZbp1fki8n8k5f1j3acuhzZ ~]# date
Tue Jul 20 21:46:55 CST 2021

format:

[root@iZbp1fki8n8k5f1j3acuhzZ ~]# date '+%Y-%m-%d %H:%m'
2021-07-20 21:07

1.2 man

View command help

Enter man ls and a description of the LS command will appear

LS(1)                                                            User Commands                                                           LS(1)

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List  information  about  the FILEs (the current directory by default).  Sort entries alphabetically if none of -cftuvSUX nor --sort is
       specified.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --all
              do not ignore entries starting with .
...............

Among them, 1 in the first line LS(1) represents the commands that can be used by general users.

Other figures are described below. Here are some important ones

Serial numberexplain
1The user loads commands or executable files that can be operated in the shell environment
2Functions and tools that can be called by the system kernel
3Some common functions and function libraries
8There are management commands available for system management

In man, you can find keywords through the following commands:

  • /string

    Find the string in [down]

  • ?string

    Look up the string

  • N or n

    With / or? When searching, n represents a downward search, and N represents a reverse search

man -f man -- Enumeration and man Related commands
man -k man -- As long as it contains man The description document of this keyword is listed

1.3 info page

Similar to man, but the content is listed in the form of paragraphs. There is a directory. Click the directory to jump to the specified paragraph

  • n

    Page to next node

  • p

    To previous node

  • u

    Move up one layer to the overview screen of the upper layer

  • s or/

    Find on current page

1.4 shutdown related

1.4.1 sync

Writes memory data to disk. The current shutdown commands such as shutdown and reboot will execute the sync command.

When ordinary users execute the sync command, they can only refresh the data under their own account.

1.4.2 shutdown

Relevant parameters:

  • -k

    Only send warning information, not true shutdown

    shutdown -k now 'I'm going to shut down. Pay attention to saving data'
    
  • -r

    Restart the system after the system service is stopped

  • -h

    Shut down the system immediately after the system service is stopped

  • -c

    Cancel shutdown content

  • time

    Specify the number of minutes after shutdown. The default is 1 minute. now or 0 is shutdown immediately

1.4. 3 restart

reboot

halt

poweroff

The three commands are similar. They are for restart operation

su - switch to root

2, File permission related

2.1 authority basis

[root@iZbp1fki8n8k5f1j3acuhzZ softwares]# ls -al
total 0
drwxr-xr-x   5 root root  49 Jul  4 16:20 .
drwxr-xr-x. 19 root root 239 Jan  1  2021 ..
drwxr-xr-x   6 root root 107 Dec  8  2020 fastdfs
drwxr-xr-x   3 root root  23 Dec 25  2020 node
-rw-r--r--   1 root root   0 Jul 20 22:18 test1
drwxr-xr-x   8 root root 158 Jan  1  2021 zkServer

The first character in the first column represents the file type

  • [d]

    catalogue

  • [-]

    file

  • [l]

    Link file

  • [b]

    Equipment file, peripheral equipment

  • [c]

    Serial port device in device file

The numbers immediately following each group of three represent file permissions.

  • first group

    File owner permissions

  • Group 2

    Permissions for user groups

  • Group 3

    Permissions of others

Each group of permissions consists of * * Read (r), write (w) and executable (x) * * permissions, corresponding to the number 4 (r) 2 (w) 1 (x) respectively

The number in the second column indicates how many file names are linked to this node (inode)

The third column represents the owner's account of the document

The fourth column indicates the user group name to which the file belongs

Column 5: file size [bytes]

Column 6: creation time or modification time

2.2 modify file attributes and permissions

2.2. 1. Modify user group chgrp

Format: chgrp -R groupName fileName/dirName

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-zwditpnz-1629598798326) (/ users / xiaoyunshi / library / Application Support / typera user images / image-20210721211013637. PNG)]

All user groups can be queried in / etc/group

2.2. 2 modify file owner chown

Format:

chown [-R] username fileName/dirName, change the owner of fileName to username, or change the owner of all files in the subdirectory of dirName to username through - R

Chown can also modify user groups directly: chown [-R] username:groupName fileName/dirName

[root@localhost ~]# chown xiaoyunshi testtxt
[root@localhost ~]# ls -l
 Total consumption 4
-rw-------. 1 root       root 1417 7 June 13-21:28 anaconda-ks.cfg
drwxr-xr-x. 2 root       test   17 7 May 21:09 testdir
-rw-r--r--. 1 xiaoyunshi root    0 7 May 21:08 testtxt	

2.2. 3 modify file permissions chmod

Basic syntax: chmod [-R] num1num2num3 file or directory

As mentioned earlier, file permissions are divided into r(4), w(2) and x(1), where numx represents the sum of these numbers. num1 is the permission of the file owner, num2 is the permission of the user group, and num3 is the permission of other users

For example, the permissions given to the testtext file are the file owner: readable, writable and executable, the user group: readable and writable, and other users can only read

[root@localhost ~]# chmod 764 testtxt
[root@localhost ~]# ls -l
-rwxrw-r--. 1 xiaoyunshi root    0 7 May 21:08 testtxt

In addition, the three identities of permission: user, group and others can be replaced by u, g and o respectively, plus four identity types of a (owner). You can use ugoa to modify permissions on behalf of the four identities.

If the above example is changed to user: read / write, user group: read-only, others: read-only, it can be modified as follows:

[root@localhost ~]# chmod u=rw,go=r testtxt
[root@localhost ~]# ls -l
-rw-r--r--. 1 xiaoyunshi root    0 7 May 21:08 testtxt

If you only want to add or remove a permission to the file, you can replace = with + or -:

# Add executable permissions to the file owner
[root@localhost ~]# chmod u+x testtxt
[root@localhost ~]# ls -l
-rwxr--r--. 1 xiaoyunshi root    0 7 May 21:08 testtxt

#Add write permissions to user groups
[root@localhost ~]# chmod g+w testtxt
[root@localhost ~]# ls -l
-rwxrw-r--. 1 xiaoyunshi root    0 7 May 21:08 testtxt
#Remove read permissions from other users
[root@localhost ~]# chmod o-r testtxt
[root@localhost ~]# ls -l
-rwxrw----. 1 xiaoyunshi root    0 7 May 21:08 testtxt
# Add executable permissions to all users             
[root@localhost ~]# chmod a+x testtxt
[root@localhost ~]# ls -l
-rwxrwx--x. 1 xiaoyunshi root    0 7 May 21:08 testtxt

2.3 role of authority

2.3. 1. Effect of permissions on files

  • r

    You can read the actual contents of the file

  • w

    The file can be edited (written, modified, etc.), but cannot be deleted!

  • x

    The permissions executed by the system have nothing to do with the file name, but only with the file content

2.3. 2. Effect of permissions on directories

Directory is mainly a list of record files

  • r

    You can read the list of directory files. That is, you can use commands such as ls to view the contents of the file list in the directory

  • w

    1. Create new files and directories
    2. Delete existing files and directories (regardless of file permissions)
    3. Rename an existing file or directory
    4. Move the location of files and directories in the directory
  • x

    Determines whether the current directory can be used as a working directory, that is, whether it can be accessed through cd

That is, for a directory, even if it has w permission, but does not have x permission, it is still unable to operate the files and directories in the directory.

[xiaoyunshi@localhost local]$ ls -l
drwxr-xr--. 2 root root  6 7 September 21-22:03 test
[xiaoyunshi@localhost local]$ ls test/
[xiaoyunshi@localhost local]$ cd test/
-bash: cd: test/: insufficient privilege

As mentioned above, the test directory has only read permission for other users, so you can ls test, but you can't cd enter the directory

3, Directory and path

3.1 directory operation

3.1.1 pwd

pwd: displays the current directory

[root@localhost test]# pwd
/usr/local/test

3.1.2 mkdir

mkdir: create directory

Format: mkdir -[mp] [permissions] dirName

  • -m

    Create a directory with custom permissions

    [root@localhost test]# mkdir -m 721 dir1
    [root@localhost test]# ls -l
    drwx-w---x. 2 root root 6 7 June 22-17:39 dir1
    

    Create a directory with permission rwx-w---x

  • -p

    Recursive creation, such as:

    [root@localhost test]# mkdir -p dir2/dir21/dir22
    

    The dir2 directory is created in the current directory, and dir2 contains the dir21 directory and dir21 contains the dir22 directory

If no permission is specified, the default permission is used. The default permission of the directory is related to umask [described later]

3.1.3 rmdir

Delete directory

The same syntax as mkdir, but only non empty directories can be deleted.

[root@localhost test]# rmdir -p dir2/dir21/dir22/

If you want to delete the directory and all contents in the directory, you can directly rm -rf dir and force deletion.

3.1.4 ls

List directory contents, sorted by file name by default

Options and parameters:
-a: All files are listed together with hidden files (files starting with.)
-A: All files, together with hidden files, but excluding And... These two directories
-F: According to the file, directory and other information, additional data structures are given, for example: on behalf of cocoa execution file; /: Representative directory; =: Represents the socket file; |: Represents FIFO file;
-h: List the file size in a friendly way (such as GB, KB, etc.);
-i: List inode number [later];
-l: Including data such as file attributes and permissions; (common)
-n: Lists UID s and GID S, not the names of users and groups
-r: File reverse sort
-R: Recursively enumerate all files in the directory;
-S: Sort by file size*
-t: Sort by time

3.2 copy, delete and move related

3.2.1 cp

cp: for copying files

  • -i

    The target file exists. You will ask first to overwrite it

  • -p

    Copy along with the attributes of the file (permissions, user, and time) instead of using the default attributes

  • -r

    Recursive replication

  • -s

    Copy as symbolic link file

  • -u

    Only when the target file does not exist can it be copied. Only when the target file exists but is older than the source file can it be updated (overwritten) [for backup]

  • -a

    Equivalent to - dr, – preserve=all

    --preserve=all: in addition to the permissions contained in - p, SELinux attribute is added, and links and xattr are also copied

  • -l

    Establish hard linked [later] link files

  • -d

    If the source file is a linked file attribute, the linked file attribute is copied instead of the file itself

[example: - p]

By default, when cp is operated without any options, the permissions, time and other attributes of the target file will be modified to the current operator.

As follows, there is a file testcp whose owner is xiaoyunshi. After using root to perform cp operation, it is found that the owner of the copied target file becomes root.

[root@localhost test]# ls -l
 Total consumption 0
-rw-rw-r--. 1 xiaoyunshi xiaoyunshi 0 7 June 22-21:11 testcp
[root@localhost test]# cp testcp /home/xiaoyunshi/
[root@localhost test]# cd /home/xiaoyunshi/
[root@localhost xiaoyunshi]# ls -l
 Total consumption 0
-rw-r--r--. 1 root root 0 7 June 22-21:12 testcp

If you want to copy with permissions, etc., you can use the - p or - a option:

[root@localhost test]# cp -p testcp  /home/xiaoyunshi/testcp2
[root@localhost test]# cd /home/xiaoyunshi/
[root@localhost xiaoyunshi]# ls -l
-rw-rw-r--. 1 xiaoyunshi xiaoyunshi 0 7 June 22-21:11 testcp2

So I'll pass the test

[link example: - s, - l, - d]

Establish a symbolic link file for testcp (it can be understood as a shortcut in windows, not the file itself, which points to the file itself through - > file)

[root@localhost test]# ls -l
 Total consumption 0
-rw-r--r--. 1 root root 0 7 June 22-21:21 testcp
[root@localhost test]# cp -s testcp testcplink
[root@localhost test]# ls -l
 Total consumption 0
-rw-r--r--. 1 root root 0 7 June 22-21:21 testcp
lrwxrwxrwx. 1 root root 6 7 June 22-21:29 testcplink -> testcp  # Established link file to testcp

-d: If the source file is a linked file attribute, copy the linked file attribute instead of the file itself; otherwise, copy the file itself

# 1: Use - d to copy testcp as testcp2: because testcp is not a linked file, the copied testcp2 is also a non linked file
[root@localhost test]# cp -d testcp testcp2
[root@localhost test]# ls -l
 Total consumption 0
-rw-r--r--. 1 root root 0 7 June 22-21:21 testcp
-rw-r--r--. 1 root root 0 7 June 22-21:32 testcp2
lrwxrwxrwx. 1 root root 6 7 June 22-21:29 testcplink -> testcp

#2: Use - d to copy testplink to testplink2: because testplink is a linked file, the copied testplink2 is also a linked file and points to the original file
[root@localhost test]# cp -d testcplink testcplink2
[root@localhost test]# ls -l
 Total consumption 0
-rw-r--r--. 1 root root 0 7 June 22-21:21 testcp
-rw-r--r--. 1 root root 0 7 June 22-21:32 testcp2
lrwxrwxrwx. 1 root root 6 7 June 22-21:29 testcplink -> testcp
lrwxrwxrwx. 1 root root 6 7 June 22-21:32 testcplink2 -> testcp # Point to testcp

#3: Copy testcplink to testcp3: because - d is not used, cp will only copy the file itself by default, regardless of whether the source file is a linked file or not
[root@localhost test]# cp testcplink testcp3
[root@localhost test]# ls -l
 Total consumption 0
-rw-r--r--. 1 root root 0 7 June 22-21:21 testcp
-rw-r--r--. 1 root root 0 7 June 22-21:32 testcp2
-rw-r--r--. 1 root root 0 7 June 22-21:32 testcp3 # The result of copying a linked file is still the file itself
lrwxrwxrwx. 1 root root 6 7 June 22-21:29 testcplink -> testcp
lrwxrwxrwx. 1 root root 6 7 June 22-21:32 testcplink2 -> testcp

3.2.2 rm

rm is a delete operation.

Parameters:

  • -r

    Recursive deletion

  • -f

    Force delete

  • -i

    You will be prompted to delete

3.2.3 mv

Move file or modify file name

Parameters:

  • -f

    Force, force overwrite when the target file exists

  • -i

    I'll ask you the following questions

  • -u

    Overwrite only when the target file exists and is older

Get file name: basename file

Get the directory name of the file: dirname file

3.3 viewing file content

3.3.1 cat,tac

cat useful parameters:

  • -b: List line numbers
  • -n: List line numbers, including blank lines
  • -T: The Tab key is displayed in ^ t

tac is reverse viewing

Both commands are viewed at one time, so you should be careful if the file is large when viewing online.

3.3.2 more

Page view

  • Space: page turning
  • Enter: next line
  • /String: find a string in the currently displayed content
  • q: Exit

3.3.3 less

Same as more, but supports more functions.

  • /String: find string down
  • ? String: find string up
  • pageUp/Down: turn page
  • n/N: n: forward repeat / or? Lookup, N: reverse lookup
  • g: Navigate to the first row of data
  • G: Navigate to the last row of data

3.3.4 head

Format: head [-n number] file

head displays the first 10 lines of the file from the beginning by default

head -n number shows the first number line

Number is negative: the following number lines are not displayed, that is, the last number line and the previous ones are listed. For example, if there are 50 lines in total, head -n -20, only lines 1 to 30 will be listed

3.3.5 tail

The number line is displayed from the tail. If it is not specified, the default is 10 lines

tail [-n number] file

-f: Continuously refresh the last content

3.4 document time and modification mtime, ctime and atime

The time of documents is divided into three categories:

  • Modification time (mtime): updated when the file content is modified
  • Status time (ctime): updated when the file permission attribute is modified
  • Access time (atime): if the file is read, it will be updated

ll shows the modification time by default. If you want to view other times, you can specify them through: ls -l --time=ctime

You can create an empty file or modify the file time through touch

touch:

Parameters:

  • -a: Modify atime
  • -c: Only modify the file time. If the file does not exist, no new file will be created
  • -t: Custom time format YYYYMMDDhhmm
  • -m: Modify mtime
  • -d: Modify atime and mtime at the same time. The format can be: '2021-07-22 11:11' or * * '2 days ago' *

In any case, the ctime of the file cannot be changed

3.5 default and hidden permissions of files and directories

3.5. 1 default permission umask

If no permission is specified when creating a file or permission, such as mkdir -m, the default permission will be used. What is the default permission? The details can be viewed through the umask command:

[root@localhost test]# umask
0022
[root@localhost test]# umask -S
u=rwx,g=rx,o=rx

Among them, umask -S will format and output the default permissions, showing the specific default permissions. As mentioned above, the default permissions u are read-write and executable, and user groups and other users have only read and executable, but no write permissions.

The umask command outputs 4 bits. The first bit is the special permission. The last three bits are related to the permission, that is, 022. This number corresponds to the permission to be subtracted from the default permission:

  • 0: none of the file owner permissions are removed. They are: r(4), w(2), x(1)
  • 2: The write permission is removed from the permissions of user groups and other users. Because W - > 2, the default permissions are: r(4) and executable x(1)

What are the default permissions? The default permissions for files and directories are different:

  • file

    Because most files do not require executable permissions, it defaults to 666: - RW RW RW RW-

  • catalogue

    The directory must have executable permissions to enter, so it defaults to 777: drwxrwxrwx

Therefore, after subtracting the permissions to be subtracted in umask from files and directories, the actual default permissions are:

  • Actual file permissions

    -RW RW RW minus ----- w – W - the result is: - rw-r – R--

  • Directory actual permissions

    drwxrwxrwx minus ----- w – W - the result is: drwxr-xr-x

Test:

We can see that it is consistent with our calculation.

#Create directory
[root@localhost test]# mkdir defaulDir
[root@localhost test]# ls -l
 Total consumption 0
drwxr-xr-x. 2 root root 6 7 June 23-14:28 defaulDir

# create a file
[root@localhost test]# touch defaultTxt
[root@localhost test]# ls -l
-rw-r--r--. 1 root root 0 7 June 23-14:30 defaultTxt
Modification of default permissions

It's also easy to modify the default permission. Just add the number corresponding to the permission to be removed after umask, such as:

[root@localhost test]# umask 0002
[root@localhost test]# rm -rf ./*
[root@localhost test]# touch test
[root@localhost test]# mkdir dir
[root@localhost test]# ls -l
 Total consumption 0
drwxrwxr-x. 2 root root 6 7 June 23-14:42 dir
-rw-rw-r--. 1 root root 0 7 June 23-14:42 test

We only remove the write permission of other users, and keep the rest. As above, the permission to create new files and directories is indeed the same.

3.5. 2 file hiding attribute chatr

What are the hidden attributes of the file? It can be modified through chatr [+ - = [aasscditu] file or directory.

Specific parameters are as follows:

  • [±=]

    This function is similar to that of chmod+ Is to add a special parameter, - is to subtract a special parameter, = is to connect a fixed parameter

  • A

    This parameter ensures that the access time (atime) is not modified when reading files or directories==

  • a

    Only data can be added, not modified or deleted. This parameter can only be set by root

  • S

    Synchronous write to disk (usually asynchronous write. Sync will be executed to refresh the disk when shutdown, or sync will be executed manually to refresh the disk)

  • s

    Remove disk space completely when deleting, irreparable

  • u

    In contrast to s, it remains on the disk after being deleted and can be retrieved through x

  • c

    The file is automatically compressed, automatically decompressed when reading, compressed before storage

  • d

    Avoid being dumped during dump

  • i

    Let the file not be deleted, renamed, linked and added data (only root can operate)

[example] -i:

# Add i attribute
[root@localhost test]# chattr +i test
[root@localhost test]# lsattr test
----i----------- test
[root@localhost test]# rm -rf test
rm: Cannot delete"test": Operation not allowed

# Remove the hidden attribute
[root@localhost test]# chattr -i test
[root@localhost test]# lsattr test
---------------- test
# Can be deleted normally
[root@localhost test]# rm test
rm: Delete normal empty file "test"?y

lsattr hide attributes for viewing

[example] - a:

[root@localhost test]# echo "haha" -> testtxt
[root@localhost test]# cat testtxt
haha -
# Add a attribute
[root@localhost test]# chattr +a testtxt
[root@localhost test]# rm testtxt
rm: Delete normal file "testtxt"?y
rm: Cannot delete"testtxt": Operation not allowed
[root@localhost test]# echo "hehei" -> testtxt
-bash: testtxt: Operation not allowed
[root@localhost test]# echo "hahahehei" -> testtxt
-bash: testtxt: Operation not allowed

It cannot be modified or deleted after adding

3.5. 3. Special permissions for documents

SUID,SGID,SBIT

3.6 search of commands and files

3.6.1 which

which is used to find the script file. It is used to find the file name of the execution file according to the PATH specified by the PATH environment variable. For example, find the location of the ls command

Format: which [-a] command

-a: list all commands that can be found in the PATH directory

[root@localhost ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls

However, if it is a built-in command, such as history, you can't use which to find it. Here, you can use type to find it [later]

[root@localhost ~]# which history
/usr/bin/which: no history in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)

[root@localhost ~]# type history
history yes shell Embedded

3.6. 2 file search

Usually use the whereis and locate commands.

whereis

Format: whereis [-bmsu] file or directory

whereis lookup is fast because it only looks for a few specific directories

  • -l

    List several main directories that will be queried

  • -b

    Binary only

  • -m

    Only files under the manual path of the description file can be found

  • -s

    Only source files found

  • -u

    Find other special files that are not in the above three projects

[root@localhost ~]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz
find

Syntax: find [PATH] [option] [action]

option has the following parameters:

1. Time dependent

-Mtime, - atime and - ctime, take mtime as an example:

  • -Mtime n: the document modified n days ago (within the same day)
  • -Mtime + N: files modified n days ago (excluding the day n)
  • -Mtime - N: modified files within n days (including day n)

Example: n=4:

-------(+4)----|--(4)--|----------(-4)----------
---7-----6-----5-------4-----3-----2-----1-----now

2. User or user group related

  • -uid n

    Number corresponding to account name

  • -gid n

    Number corresponding to user group ID

  • -user name

    name is the user account

  • -group name

    Name is the user group name

  • -nouser

    Find that the owner of the file is not in / etc/passwd

  • -nogroup

    Find the user group to which the file belongs that is not in / etc/group

Find all files with xiaoyunshi in the / home directory

[root@localhost ~]# find /home -user xiaoyunshi
/home/xiaoyunshi
/home/xiaoyunshi/.bash_logout
/home/xiaoyunshi/.bash_profile
/home/xiaoyunshi/.bashrc
/home/xiaoyunshi/.bash_history
/home/xiaoyunshi/testcp2

3. Related to file permissions and names

-         Name : Find file named fileName File
-size [+-]SIZE  : check SIZE(Company:c->Bytes,k->1024Bytes)large(+)Or small(-)File
-type TYPE     : Find files of type TYPE[Formal documents( f),Equipment file( b,c),Catalogue( d),Link file( l),socket Documents( s),FIFO(p)Other attributes]
-perm mode : Find file properties equal to mode File
-perm -mode : Find all file attributes including mode File
-perm /mode : Find file properties include mode Documents (not all included)

If you want to query keywords, use the following method:

Add an asterisk before and after the keyword*

[root@localhost ~]# find /home -name "*test*"
/home/wangmaolin/testcp
/home/wangmaolin/testcp2
/home/wangmaolin/test
/home/wangmaolin/test/testtxt
/home/wangmaolin/test/.testtxt.swp
/home/wangmaolin/test/testtxt~
/home/wangmaolin/test/testtxz~

4. Additional operations

-exec command

You can perform additional operations on query results, such as:

find /home -name "test" -exec ls -l {} \;

Find the file named test in the / home directory, put the query results in {}, and execute the ls -l command.

[root@localhost ~]# find /home -name testtxt  -exec ls -l {} \;
-rw-rw-r--. 1 root root 7 7 May 23-15:11 /home/wangmaolin/test/testtxt

Topics: Linux