I. Basic concepts of files and directories

1. Linux directory structure

Advantages of Linux system: Multi-user and multi-task system is more secure, open source and free

In Linux, all directories and file data are organized into a tree directory structure. There is only one root directory in the entire system, and all partitions, directories, and files are under the same root directory.

2. Command prompt

[root@localhost~]#

Root: indicates the current login user name

Localhost: indicates the localhost name

~: indicates the current user directory. ~ indicates the home directory

The home directory of user root is /root

The home directory for the average user is /home

# : The current user is an administrator

$: The current user is a common user

3. Run the su command to switch users

You do not need to enter a password to switch from the root user to a common user

To switch from a common user to another user, you must enter a password

4. Display the full path of the current directory: PWD

5, directory switch command: CD

CD /etc Switch to the /etc/directory

cd .. Returns the upper directory

CD returns home directory

Root directory: /

Home directory: The home directory of a common user is under /home. The home directory of root is /root

6. Absolute and relative paths

Relative path: is a file or path in the current directory

Absolute path: Indicates the complete path of a directory or file

7. Several auxiliary operations for command line editing

Tab key: Automatic completion

Semicolon “;” : Enter more than one command on the same line, with an intermediate command. separated

Backslash “\” : the command is followed by \ to continue on the next line. Enter up and down arrow keys: Find the history command that has been executed. View the history command: history

Shortcut key Ctrl+U: Clear to the beginning of the line

Shortcut key Ctrl+K: Clear to the end of the line

Shortcut Ctrl+L: Clear the screen

Shortcut key Ctrl+C: Terminates the command execution

8, shutdown command: shutdown (safely shutdown the system)

Shutdown Parameters:

[-t] Tells init how long to shut down before changing to other runlevels.

[-r] Restart the calculator.

[-k] does not actually shut down, but sends a warning signal to each login user.

[-h] Power off after shutdown [HALT].

[-n] Instead of init, shutdown itself. Use of this option is discouraged, and the consequences of this option are not always what you expect.

[-c] Cancel current Process Cancel the current shutdown process. So this option of course has no time parameter, but can enter a message for interpretation, and this message will be sent to each user.

[-f] Ignore FSCK when restarting the calculator.

[-f] Force FSCK on reboot.

[-time] Specifies the time before shutdown.

Such as:

/ sbin/shutdown -h now—–

/ sbin/shutdown -h 20:49 —- 20:49

/sbin/shutdown -h +10—– the server is shutdown after 10 minutes

/sbin/shutdown -r now —– Restart immediately

/ sbin/shutdown -r +10 ‘The system will reboot’—- The system will reboot after 10 minutes

Restart and notify each logged in user

/ sbin/shutdown -k now ‘The system will reboot’—- only for each login

Users send notifications that are not really off

reboot init 6

2. Common shell commands

Shell command format: command name [option] [parameter]

1. File management commands

Ls — Lists the contents of directories

Ls displays the result in different colors to distinguish file categories.

Blue represents directories (folders)

Gray indicates ordinary files

Green represents executable files

Red represents compressed files

Light green for linked files (shortcuts)

Options:

-a Displays all files, including hidden files. Files beginning with a.).

-l Displays detailed information about a file or directory in a long format.

-h Displays the file size in K, M, and G (

Defaults to bytes) to improve readability

-lh

2, the use of wildcards:

“?” Can match any character in the file name,

* can match any number of characters in the file name.

Example: Ls -l /etc/ns*.conf displays details about files whose file names start with ns and whose extension is conf in the /etc directory

Ls -l /etc/v?? Displays the details about the files whose names start with v in the /etc directory.

3, create a directory (that is, create folder) : to consider the permission problem mkdir -p m

Example 1: Create a subdirectory named test in the current directory

[root@localhost /]:#mkdir /mnt/test

Example 2: Create a subdirectory named public in the root directory

[root@localhost /]:#mkdir /public

Example 3: Create three subdirectories in the current directory

[root@localhost /]:#mkdir mp3 mp4 mp5

If the current directory is /, create two subdirectories y1 y2 under/MNT [root@localhost /]:#mkdir/MNT /y1/ y2/y3

Example 4: Create a continuous directory

Example 5: Create a directory and set permissions at the same time

[root@localhost /]:#mkdir – m 775 test

4. Delete directories or files

Delete the empty directory: rmdir

Example 1: Delete the empty directory mytest [root@localhost /]:#rmdir mytest

Example 2: Delete a continuous directory

[root@localhost /]:#rmdir – p /media/cdrom/aa

Deleting files: Rm deletes only files but not directories by default

Example 1: Delete the 1.txt file from the current directory

[root@localhost /]:#rm 1.txt An error message is displayed. To confirm the deletion, enter y [root@localhost /]:#rm -f 1.txt Without an error message

Example 2: Delete the student directory in the current directory

[root@localhost /]:#rm -rf /student

5. Copy files or directories

Copy the file

[root@localhost ~]# cp /bin/touch /root/test/toch1

[root@localhost ~]# cp /etc/inittab /etc/inittab.bat Back up the file

Copy directory

[root@localhost ~]# cp -r/TMP /root/ MNT

Move (cut-and-paste) files or directories

[root@localhost ~]# mv /root/test/test2.txt rename [root@localhost ~]# mv /root/test/test2.txt /

7. Create a file

TXT Create a 1. TXT file in the/directory

Three, view the file content command

1. Cat — Displays the contents of the file

Cat -n Displays the line number

Cat does not pause when displaying the contents of a text file, so it is not suitable for viewing long files.

2. File contents are displayed in more or less pages

Use full-screen paging display file content, when the content is full screen will pause, press the space bar to continue to display the next screen, or press q to jump away, suitable for reading long files.

The difference between:

When the file content is displayed at the end of the file, the more command automatically exits the reading environment, but the less command does not. You need to press q at the: prompt to exit the file.

Head and tail– Look at the beginning or end of the file

By default, head displays the first 10 lines and tail displays the last 10 lines.

For example, view the first three lines of the /etc/inittab file

Look at the last five lines of the /etc/inittab file

4, wc command — file content statistics

Function: Statistics the number of lines, words, and bytes in a specified file.

Syntax: WC (option)(parameter)

Common options:

-l Indicates the number of rows

-w Indicates the number of words

-c Indicates the number of bytes

For example, count the number of lines, words, and bytes in the/MNT /test/1. TXT file

Find command

1. Run find to search for files and directories

Function: to find a file or directory we need in a specified path.

Similar to the “search” operation performed on Windows.

Syntax: find [search range] [search condition expression]

Common options:

-name Searches by name. Wildcard characters are allowed.

-type Searches by file type The file type can be common file (f), directory (D), or block device

File (b), Character Device File (C)

-user Searches for files by file owner based on whether a file belongs to a target user.

-size Searches for files by size. Use + and – to set the file size to larger or smaller than the specified size. Common capacity units include k (lowercase), M, and G

# find /etc -size +1024k -name “init*” #find /etc -name 1.txt

2. Grep command to find the file content

Function: to find a certain part of the content we need in a file, the target is a string. Is similar to in

The “find” operation performed in WORD.

Syntax: grep [option] Finds conditional object files

Common options:

-c: counts the number of times the ‘search string’ is found

-i: ignores the difference of case, so case is regarded as the same

-n: Outputs the line number

-v: reverse selection, that is, show the line that has no ‘search string’ content!

–color=auto: can be found keyword part plus color display!

Note: grep does not support wildcards and uses regular expressions instead:

^ What character does it start with

What character does $end with

For example, “^word” indicates that it begins with “word”

“Word $” indicates the end of” word”

“^$” indicates an empty line

5. Other auxiliary commands

1. View the history command

Function: The Linux history command is used to record commands executed.

Usage: history [n] n is a number that lists the latest n commands

Use! Run the history command.

! Number Number of the command to execute

! Command Displays the command that starts with command from the latest command. Execute the previous entry

2. Run the help command to view the help information of internal commands only

Example: View the help information about the PWD command

pwd –help

If you run the help command to view the help information of an external command, an error message is displayed

Use the –help option to look up help information for external commands

3. Man command: View the help manual of the command

Use arrow keys ↑ and ↓ to scroll the text

Use the Page Up and Page Down keys to turn pages

Press Q or Q to exit the reading environment, and press/to search for content. For example, view the LS help

4. I/O redirection

In Linux, the standard input device is the keyboard and the standard output device is the screen, but there are situations where you want to read data from an input device other than the keyboard, or send data to an output device other than the screen. This situation is called redirection.

Input redirection: <

Output redirection: > or >>

Output redirection redirects the output of a command to a file instead of displaying it on the screen.

If the file specified after > does not exist, the system creates the file first and saves the command result to the file.

If the file specified after > exists, the system clears the file content and saves the command result to the file.

The command result can be redirected and appended to the end of a specified file without overwriting the original content of the file.

Example: Redirect the output of 1.txt content to a.txt

Cat 1.txt > a.txt

Append the contents of 1.txt to a.txt

Output error information to a file

ls /etc/hhh 2> erro

cat > newfile

ls > newfile

cat > newfile < 1.txt

5. The pipe “|”

Through the pipe “|”, can connect more than one simple command to realize more complex functions.

Pipeline operators are used to “|” left command execution results as “|” right of the command input.

[root@localhost ~]# ls -lh /etc | more

Paging displays detailed information about all files and subdirectories in the /etc directory.

[root@localhost ~]# ls -lh /etc | grep net

Displays details about all files and subdirectories that contain the net keyword in the /etc directory.

/ root @ localhost ~ # ls – l/etc / *. Conf | wc – l

Count the number of files in the /etc directory that end in. Conf.

/ root @ localhost ~ # grep -v “^ #”/etc/HTTPD/conf/HTTPD. Conf | gerp – v ^ “$” view/etc/HTTPD/conf/HTTPD. Conf The contents of the file except for lines starting with “#” and empty lines.

6. Clear command — Clear the screen

Ctrl+l

Vi. File permissions

1. Description of file properties

Group 1: file type, where the first character represents the file category.

– Common file d directory L symbolic link

C character device B block device

File permissions

R: read w: write x: execute – : no permission

Group 2: number of connections of file pieces Group 3: file owners

Group 4: File ownership group

Group 5: file size, default in bytes Group 6: file creation time

Group 6: file names

2, modify the file permission: chmod

Change permission in alphabetical mode

File owner permissions (U)

Owning Group Rights (g)

Other User Rights (O)

All Users (a)

For example, the/MNT permission is drwxr-x–

The chmod u-w/MNT permission changes to dr-xr-x–

The chmod o+rx/MNT permission changes to dr-xr-xr-x

The chmod g+w/MNT permission changes to dr-xrwxr-x

The chmod a+w/MNT permission changes to DRWXRWXRWX

3. Modify permissions digitally

r=4 w=2 x=1 -=0

4. Change the owner and group of the file to chown

Example: chown teacher:teacher /test1 Change the owner and the owning group at the same time

Chown root /test1 Changes the owner

Chown :student /test1 Changes the owning group

5. Special Permissions:

1) ACL: access control [set special permissions for part of other users or individual users]

setfacl -m u:stu1:rwx A

Create an ACL rule to give user STU1 RWX permissions on directory A

Getfacl A Displays ACL rules

setfacl -x u:stu3 A

Delete the ACL permission of STU3 on directory A. Only one rule setfacl -b A is deleted

Example Delete all ACL rules of directory A

(2) SBIT: sticky bit permission (forced bit permission) [for other users, can only delete their own files, can not delete other users’ files]

chmod o+t A

(3) SGID: Set for other users and execute it as the owning group.

hmod g+s A

(4) SUID: Execute the file as the owner of the file, only for executable files.

chmod u+s A