2. File management and input and output under Linux

2. File management under Linux

1. Basic file management commands:
(1) touch command
[1] New system file: "touch /.../.../file name (file creation time is the current time)" Note: multiple different files can be created at the same time
[2] Time to modify the system file: "touch -t modification date (for example: 202001010101) File name (file1...)"
Insert picture description here as shown in the figure: Create wenjian1 on the desktop, and change its creation time to 00:00 on September 9, 1990. Note: Don't forget to add the file name when executing touch -t.

(2) The mkdir command
[1] "mkdir dir1 dir2 …" means to create an empty directory. Note: Multiple different directories can be created at the same time
[2] "mkdir -p /home/lin/..." means to create a home directory under the root and Continue to create a lin directory under it... (Create multi-layer directories)
Insert picture description here as shown in the figure: Create a folder dir1 containing two directories dir3 and dir2 on the westos desktop.

(3) The rm command
[1] "rm /.../.../ file name (file1, file2...)" means to delete ... files in the /.../.../ directory (with prompt) Note: Multiple files can be created at the same time
[2 】"Rm -f file name (file...)" means that the target is forced to delete the file (no prompt)
[3] " rm -r directory name (dir dir1...)" means there is a prompt to delete the directory
[4] "rm -fr directory name (Dir...)” means that the directory is forcibly deleted without prompt. The
Insert picture description here
figure shows that multiple files and folders are created at the same time, and then all files and folders on the westos user's desktop are forcibly deleted. Note: /* means any content under the directory (rm -fr /* means delete all content under the root directory).

(4) gedit command
[1] "gedit file name (can exist or not)" means to edit the file with a graphical interface (if the file does not exist, create it first and then edit) Note: gedit must have a graphical interface when it is used, gedit The shell command line is occupied during execution. If you need to use the command line, you can end the command with "ctrl c" or use "ctrl shift z" to pause the command in the background.

(5) vim command
[1] vim file name (file1…) means to enter the browse mode of vim (the file cannot be edited)
press "i" in the browse mode to enter the insert mode (the file can be edited)
and the mouse cannot be controlled in vim Use the cursor to move the cursor up, down, left, and right.
Press to exit the insert mode.
Press: wq to save changes, press q to exit without saving (wq!, q! means mandatory execution of the command)
[2] Vim's abnormal exit problem
When vim is writing a file, the changes are not saved When it is forced to close, an abnormal display will appear when vim is opened again (the reason is that the hidden .swp file will be opened during vim editing, and the .swp file will not exist after the abnormal close):
Insert picture description here

vim出现异常
[O]	只读打开文件
(E)	继续编辑
(R)	恢复未保存内容编辑
(D)	删除.swp文件进入编辑
(Q)	退出当前编辑的文件,可继续编辑下一个文件(不退出vim文本编辑)
(A)	退出vim文本编辑模式

Note: When an exception occurs, only d (delete) operation can be performed, and other operations cannot solve the problem.

(6) cat command
[1] "cat file name (file1...)" means: display the entire content of the file (displayed in the shell)
[2] "cat -b file name (file...)" means: display the file The content and display the line number, the line number that does not display the blank line
Insert picture description here[3] "cat -n file name (file...)" means to display the file content and display the line number (including blank lines, the result is shown in the figure:)
Insert picture description here
(7) head, The tail command
[1] "head file name (file...)" means: view the number of lines before the file (the first 10 lines of the file are displayed by default)
[2] "head -n file name (file...)" n stands for the number display file before Line n
[3] "tail file name (file...)" means: view the number of lines after the file (the last 10 lines of the file are displayed by default)
[4] "tail -n file name (file...)" n represents the back of the digital display file n rows

(8) Less page browsing command
[1] "less file name (file1...)" Enter the less mode to view all the contents of the file (less page, not displayed in the shell)
[2] In less mode <up><down> View file content line by line
[3] Use "pgup" and "pgdn" to view file content page by page
[4] "/keyword" means search keyword n ​​matches downward N matches upwards
[5] "v" means less mode Enter the vim text editing mode, after editing: wq exit and return to less
[6] Use "q" to exit in less

Insert picture description hereThe picture above shows less paging browsing files, the search keyword /cat is highlighted and displayed

(9) cp copy file command
[1] "cp file name destination file name" means: copy the file to the target file (the original things in the target file will be lost)
[2] "cp file 1 file 2 destination directory "Means copying files to the destination (multiple files can be copied at once)
[3] "cp -r directory 1 directory 2 destination directory" means copying the directory file to the destination

(10) mv move file command
[1] "mv file name (file file1…) destination directory (dir)" means to move the target file to the destination directory (multiple files can be moved at the same time)
[2] "mv Directory name (dir dir1...) Destination directory (dir)" means to move the target directory to the destination directory (multiple directories can be moved at once)
[3] "mv file name directory name (file dir...) does not exist File name directory name (file dir…)” means: rename a file or directory
[4] Movement principle: The movement of files in the same device partition is a process of renaming, and the movement of files in different partitions is the process of copying and establishing first, and then deleting The process of "ls -i file" means: view the file node number (equivalent to the file ID number)

(11) File file type view command
[1] "file file name" means: view the true type of the file
Insert picture description here
(12) wc file capacity statistics command
[1] "wc -l file name" means display the number of lines of the file
[ 2] "wc -m file name" means the number of characters in the file
[3] "wc -w file name" means the number of words in the file
[4] "wc -c file name" means the bytes of the file The number
Insert picture description hereis shown in the figure: the number of lines in file1 is 384, the number of characters is 1167, the number of words is 7031, and the number of bytes is 7043.

2. File addressing and addressing commands in linux :
(1) Relative path and absolute path: Relative path is the abbreviation of absolute path name, omitting the user's current system location. This name is only used in the management of the current system directory It can only be used when it is a file. The file name in the system that does not start with / is a relative path. When the program is operated, it will automatically recognize the current path information and associate the operation target; the absolute path is the real location of the file in the system, in any scenario Both can be used, usually starting with /, regardless of the current system location. Note: Everything in linux exists in the form of files.

(2) Directory in linux: "/" is the first-level directory, also called the root directory, the directory under the root is called the second-level directory, the default information of the second-level directory is as follows:
/bin --------- ---##System General Command
/sbin ----------##System Management Command
/dev -----------##Device File
/home ------ --##Ordinary user home directory collection, the subdirectories in this directory are ordinary user home directory
/root ----------##Super user home directory
/lib64 --------- ##64-bit function library
/lib ------------##System function library
/proc ---------##Process information, hardware information recognized by the system
/run - --------##Currently running hardware and system modules
/srv ----------##System data (constant)
/var ----------## System data (change)
/tmp ---------##System temporary file storage directory
/mnt ---------##Temporary device mount point
/media ------# #cdrom Temporary mount point
/etc ----------##Configuration directory of most programs of the system
/opt ----------## Third-party software installation location
/boot - ------##System startup partition, the files read during system startup are in this directory
/sys ----------##About the kernel setting directory
/usr ----------##unix system resource is second only to "/" in importance, and most system resources are in this directory.

(3) File addressing commands in linux
[1] pwd ------display the current working directory
[2] cd /.../.../ ------ switch the current working directory ("." means This level directory, "..." means the upper level directory)
"cd ~-" = "cd -" ------- means switch to the previous working directory
[3]

    # ls-------------------------显示当前目录中文件的名称
	# ls file--------------------显示指定文件名称
	# ls dir	-----------------显示指定目录中内容的名称
	# ls -d dir	-----------------显示目录本身的名称
	# ls -l file	-------------显示文件属性
	# ls -l dir	-----------------显示目录中内容的属性
	# ls -ld dir	-------------显示目录本身属性
	# ls -a dir	----------------显示目录中所有文件名称包括隐藏文件案
	# ls -s---------------------显示文件大小
	# ls -R dir----------------递归显示目录中的内容

Insert picture description hereNote: "ls -a dir" has the same function as "ls", but "ls -a dir" has a more comprehensive function (it can display hidden files); "ls -d dir" means to display the attribute information of the directory itself (not Including directories and files in the directory); "ls -l dir" means to display the attribute information of all contents in the directory (if there are other directories in the directory, only the directory information is displayed); "ls -R dir" is called recursively, means to display Attribute information of all hierarchical directories and files under this directory.

4. Batch processing of linux files
(1) Batch processing files with the same characteristics

"*"------------------------Match 0 to any character
"?"--------------- ---------Match a single character
[[:alpha]] --------------- Match a single letter
[[:lower:]]------- ---------Match a single lowercase letter
[[:upper:]]---------------Match a single uppercase letter
[[:digit:]]---- -------------Match a single number
[[:alnum:]]--------------Match a single number or letter
[[:punct:]]- -------------Match a single symbol
[[:space:]]--------------Match a single space
Insert picture description hereas shown in the figure: delete the dirrr1 "Wes number tos", "wes symbol tos", "wes all", "wes letter or space or number tos" file.
Note: The [[:alpha:][:space:][:alnum:]] in the middle of the last line means "or" and only represents one character bit.

(2) Character set representation method
[1] "[ ]" means fuzzy matching, the expression or relationship in [], the number of objects and several matches are operated on
[] [1-10] means 1 or ... or 10, [ !df] | or [^df] means in addition to df
[2] "{ }" means exact match, roll call mechanism, precisely specify each element in the set; {1...10} means 1 to 10.
Insert picture description here
As shown in the figure: first create 15 files (1a, 1b, 1c, 2a...), delete everything between 2 and 5, delete all files except a (! and ^ mean except); "{ }" operation Will report an error, "[ ]" will not report an error (operate if there is, skip if not).

Attachment: [1] cat command: mainly has three functions

1) Display the entire file at once: $ cat filename

2) Create a file from the keyboard: $ cat> filename

3) Combine several files into one file: $ cat file1 file2> file

File output

1 Understand what is the definition of input and output:

1) Character device The
character device is the device file that displays characters on the screen

2) stdin
standard input
number 0
keyboard mouse typewriter

3) The
standard output of stdout is correct and the
label is 1

4) stderr
standard error output
label is 2

Insert picture description here

Output redirection:
Insert picture description here

Output, multi-line input:
Insert picture description here

Single-line output redirection: tr'a-z''A-Z' <file; Redirect the content in file to the tr command, which is equivalent to cat file | tr'a-z''A-Z';

find /etc -name passwd 2>

Guess you like

Origin blog.csdn.net/lb1331/article/details/109180794