-linux file management

Comparison of directory structure between linux and windows

  • Absolute path and relative path

  • Everything is the design idea of the document

  • File time

    [root@arther-linux ~]# ls -l
     The file name only depends on the modification time of the file
    
    Linux There are three times for files
    
    [root@arther-linux ~]# stat anaconda-ks.cfg 
      File:"anaconda-ks.cfg"
      Size: 3108      	Block: 8          IO Block: 4096 normal files
     Equipment: fd00h/64768d	Inode: 2231463     Hard link: 1
     jurisdiction:(0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
    Environmental Science: system_u:object_r:admin_home_t:s0
     Recent visit: 2020-11-19 16:42:52.617000000 +0800 # Check the content and detect with cat
     Recent changes: 2020-11-19 16:42:52.619000000 +0800 # Modify the content, such as using vim editor
     Recent changes: 2020-11-19 16:42:52.619000000 +0800 # Modify content, modify permission and other attributes, which will change if there is any change
     Created on:-
    
  • File extension

    Linux The file has no extension!!! 
    // Method 1: ls -l file name / / look at the first character 
    
    - Ordinary files (text files, binary files, compressed files, movies, pictures...),For example:/bin/ls 
    d Directory file (blue), for example/home/ 
    b Device file (block device) storage device hard disk, U Disk, for example:/dev/sda 
    c Device file (character device) printer, e.g. terminal/dev/tty1 
    s Socket file, for example:/run/rpcbind.sock 
    p Pipe files, for example:/run/systemd/initctl/fifo l Linked files (light blue), for example:/bin 
    
    Blue indicates the directory file
     Green indicates executable
     Red indicates compressed files
     Light blue indicates linked files
     White indicates other files
     Yellow indicates device files (including block,char,fifo
    ps:Judging the type of file by color is wrong!!! 
      
    // Method 2:[ root@arther-linux ~]# file /etc/grub.conf
    

II. System directory structure and function

Windows: organize files in a multi root manner C:\ D:\ E:\

Linux: Organize files as a single root/

FHS (File System Hierarchy Standard) refers to the hierarchical standard of file system. Most Linux versions adopt this form of file organization, which organizes files in a tree structure.

[root@arther-linux ~]# ls -l /
#  /It is the vertex directory of all linux operating systems. Unlike windows, each partition has a fixed-point directory
# Total dosage 66

-1.Command related directory
/bin -> usr/bin 			# Commands used by ordinary users, such as ls and date
/sbin -> usr/sbin 		# Commands used by the administrator, such as adduser

-2.Startup directory
/boot/ ->  	# Stored startup related files, such as kernel, grub (boot loader)

-3.System file directory
/usr 								# System file, equivalent to C:\Windows
/lib -> usr/lib 		# Library file Glibc
/lib64 -> usr/lib64 # Library file Glibc

-4.User home directory
/home/ 	# Home directory of ordinary users
/root/ 	# HOME of root user, administrator user

-5.Profile directory
/etc/   # Very important, the system level service configuration files are here
    /etc/sysconfig/network-script/ifcfg-* # Network profile 
    /etc/hostname													# System hostname profile 
    /etc/resolv.conf											# dns client profile 
    /etc/hosts														# Local domain name resolution profile 
    /etc/fstab 														# System mount directory boot auto mount list 
    /etc/passwd 													# System user files

-6.Device catalog file
/dev/		# Equipment file
    /dev/cdrom and/dev/sr0 	# System optical disk image device 
    /dev/null							 # Black hole equipment, only in and out. Similar to garbage collection bin 
    /dev/random						 # Device for generating random numbers 
    /dev/zero							 # The energy source constantly generates data, which is similar to the ATM, and withdraws money anytime, anywhere 
    /dev/pts/0			# Virtual Bash Shell terminal,For remote users, 0 represents the first terminal, 1 represents the second terminal, and so on 		 /dev/stderr 					  # Error output 
    /dev/stdin						 # Standard input 
    /dev/stdout						 # standard output 
    
-7.Virtual file system: similar to the dashboard of a car, it can see whether the car is faulty or out of fuel
/proc/	# The virtual file system reflects the kernel, process information or real-time status
		# Reflect the real-time status of the current process of the system 
  	/proc/meminfo: Memory information 
    /proc/cpuinfo: cpu information
    
-8.Variable directory and temporary directory
/var/		# It stores some change files, such as database, log, email
    /tmp   # The temporary directory of the system (similar to the public toilet), the system will regularly delete the files under the directory that have not been accessed for a long time. 
    /var	 # Store some change files as follows:
      			mysql: 				/var/lib/mysql 
      			vsftpd: 			/var/ftp 
        		mail: 				/var/spool/mail 
          	cron: 				/var/spool/cron 
            log: 					/var/log   # Storage directory of system log files
                              /var/log/messages		# system log 
                              /var/log/secure			# System login log 
           /var/tmp 	#Temporary documents (mainly temporary documents generated by the program)
          								[root@arther-linux ~]# tail -f /var/log/messages
            							# This command dynamically checks the log, that is, it can see the real-time changes of the log
              
-9.Device (mainly storage device) mount directory
'''
Mounting is the process of associating a partition to a directory,Mount the partition so that it starts from the specified directory(Commonly known as mount point, mount point)The storage area can be used.
'''
 /media/ # Default mount point for mobile devices
 /mnt/ # Mounting point of manual mounting equipment
 /opt/ # The directory of software stored by early third-party manufacturers
 /tmp/ # Temporarily stored files, similar to the recycle bin, are automatically deleted after more than ten days

-10.Some other directories
/lost+found/ # orphan files  
This directory is standard ext2/ext3 A directory generated only in the file system format. The purpose is to put some missing fragments into this directory when there is an error in the file system. This directory usually exists at the top of the partition slot. For example, you can add a hard disk to it/disk In this system, such a directory will be automatically generated/disk/lost+found 
lost+found This directory is generally empty. After the system is shut down illegally,If you lose some files, you can find them here for storage fsck Of files partially repaired in the process lost+found: Almost every formatted Linux There will be partitions. The files retrieved after an accident are usually in here. This directory is used to store files lost after an accident. only root Users can open

drwxr-xr-x 25 root root 660 Jul 8 17:00 run  # Store the pid file generated after the program runs 
drwxr-xr-x. 2 root root 4096 Apr 11 2018 srv # Some files generated by physical devices 
dr-xr-xr-x 13 root root 0 Feb 16 01:22 sys 	 # Driver information of hardware device

Three absolute paths and relative paths

Absolute path: path starting from / / home

Relative path: starts relative to the current directory

. Link to current directory

.. Link to upper level directory
  		 Home directory of the current user
# 1. Absolute path example 
touch /home/alice/file1 
touch ~alice/file2 
# 2. Relative path example 
touch ../file3 
touch file4 
touch abc/file5

Four basic commands

-1.View the current directory
[root@arther-linux ~]# pwd
/root

-2.Switch directory
cd Absolute path, such as: cd /home/alice cd ~alice 
cd Relative path, such as: cd home/alice cd .. 
cd - 		# Return to last directory 
cd 	 		# cd is equivalent to going home directly~ 
cd . 		# Leave the current directory unchanged 
cd .. 	# Switch to the directory above the current directory

-3.View directory tree:Need to install first tree Command, execute yum install tree -y
tree -a   catalogue # Show all files, including hidden files 
tree -d   catalogue # Show only subdirectories 
tree -L 1 catalogue # -L is the maximum number of layers to traverse the directory. After - L, it should be a positive integer greater than 0 
tree -F 	catalogue # -F adds an indicator of the file type after the entry, such as a left slash after the displayed directory/

Find out tree Command other options 
  -a Displays all files and directories. 
  -A use ASNI The drawing character displays a tree view instead of a ASCII Character combination. 
  -C Add color to the list of documents and directories to distinguish various types. 
  -d Displays the directory name instead of the contents. 
  -D Lists when the file or directory was changed. 
  -f Displays the full relative path name before each file or directory. 
  -F In the execution file, directory, Socket,Symbolic connection, pipe name, each plus"*","/","=","@","|"number. 
  -g List the group name of the file or directory. If there is no corresponding name, the group ID will be displayed. 
  -i File or directory names are not listed in steps. 
  -I File or directory names that conform to the template style are not displayed. 
  -l If you encounter a directory that is a symbolic connection, directly list the original directory that the connection points to. 
  -n Do not add color to the list of documents and directories. 
  -N Directly list file and directory names, including control characters. 
  -p List permission labels. 
  -P Displays only the file or directory names that match the template style. 
  -q use"?"The number replaces the control character and lists the file and directory names. 
  -s Lists the file or directory size. 
  -t Sort with the change time of files and directories. 
  -u List the owner name of the file or directory. If there is no corresponding name, the user ID is displayed. 
  -x Limit the scope to the current file system. If some subdirectories under the specified directory are stored on another file system, the subdirectories will be excluded from the search scope.

-4.browse
ls 						  # Default browse directory 
ls -l catalogue 			# Browse the details of subdirectories and subdirectories under the directory 
ls -al catalogue 		# The result of browsing contains hidden files 
ls -dl catalogue 		# View directory

[root@arther-linux /]# ll bin
lrwxrwxrwx. 1 root root 7 11 June 19-16:30 bin -> usr/bin
  
# Each part is analyzed as follows
lrwxrwxrwx. jurisdiction,The following points represent whether the selinux When on( enforcing perhaps permissive All files created by open)

1 							Number of hard links 
root 						Owner 
root 						Genus group 
7 							File size in bytes 
12 May 15-14:02   Document modification time 
bin 						File name


# The first of the permissions represents the file type
 format 						explain 
- 							Ordinary file(text, Binary, Compressed package, picture, Log, etc) 
d 							Catalog file 
b 							Equipment file(Block device)Storage device hard disk /dev/sda1, /dev/sda2 
c 							Equipment file(Character device)Printer, terminal /dev/tty1, /dev/zero 
s 							s socket , Interprocess communication(socket) 
p 							Pipeline file 
l 							Link file

be careful: Linux The file extension does not mean anything, Just for the convenience of operation and maintenance personnel
  
-5.stat Command (view file time)
	  Linux There are three times for files,use stat see 
	  For example: stat anaconda-ks.cfg

	  Access time: atime,View content with cat testing
	  Modified on: mtime,Modification content
	  Change time: ctime,Modify content, modify permission and other attributes, which will change if there is any change

V. file management: create, copy, move and delete

1 create

-1.Create file: touch

touch file1.txt 						# If there is none, it will be created; if there is one, it will be modified 
touch /home/file10.txt 			
touch file{1..20} 					# Create 1, 2, 3, 4 20 file with file name
touch Ego{n,N}.txt 					# Equivalent touch Egon txt EgoN. txt

-2.Create directory: mkdir
mkdir dir1 
mkdir /home/dir2 /home/dir3 
mkdir /home/{dir4,dir5} 
mkdir -v /home/{dir6,dir7} 
		mkdir: Directory created“/home/dir6" 
  	mkdir: Directory created“/home/dir7" 
mkdir -p /home/dir8/111/222 //Including the creation of their parents


2 copy

-1.Copy: cp
cp Source target

[root@arther-linux /]# cp -v /home/1.txt /home/dir1/
"/home/1.txt" -> "/home/dir1/1.txt"  # Displays the detailed copy process. Folders (directories) cannot be copied

[root@arther-linux /]# cp -r /home/dir1 /home/dir2
# Recursively copy a folder and its contents

[root@arther-linux /]# cp -rv /home/bibibi /home/dir1/
"/home/bibibi" -> "/home/dir1/bibibi"
"/home/bibibi/.bash_logout" -> "/home/dir1/bibibi/.bash_logout"
"/home/bibibi/.bash_profile" -> "/home/dir1/bibibi/.bash_profile"
"/home/bibibi/.bashrc" -> "/home/dir1/bibibi/.bashrc"
# The combination of the two can copy the folder and the files under it, and can also display the detailed process of copying

# Extended usage: 
cp -rfv /etc/hosts{,.old} 
cp -rvf /etc/sysconfig/network-scripts/ifcfg-eth0{,.old}

Tips: root User use cp (There is an alias by default alias cp='cp -i') -i Display prompt (overwrite or not) 
1. /bin/cp -rf /etc /tmp 
2. # unalias cp # cp -rf /etc /tmp 
3. # \cp -rf /etc /tmp 
4.-f The parameter is force copy,For example, where are you A There is a file named B,Then you put C The file inside is called another folder B Copy to A inside,There will be a conflict at this time,You will then be prompted whether to continue copying.add-f You won't be prompted.

Your input-f Or will you be prompted why 
------------------------------------------------------------- 
Some systems are installed,cp - i of alias namely cp Which means you're executing cp In fact, it is implemented cp -i Cancel cp of alias Just fine 

==========================================
# terms of settlement 
unalias cp

3 move and delete

-1.move:mv
[root@arther-linux /]# mv /home/dir1/1.txt /home/dir2/ 
# Will 1 Txt from dir1 to dir2

[root@arther-linux /]# mv /home/dir1/arther.txt /home/dir1/egon.txt
# Moving under the current folder is renaming

-2.delete:rm
 Example: delete/home/dir1 
# cd /home 
# rm -rf dir1 
-r recursion 
-f force force 
-v Detailed process

Vi. document management: check the contents of documents

==text file (cat less more head tail grep) 
/etc/hosts 
/etc/sysconfig/network 
/etc/sysconfig/network-scripts/ifcfg-eth0  # Network card configuration
/etc/passwd 
/etc/shadow 
/etc/group 
/etc/grub.conf 
/etc/resolv.conf 
/var/log/messages # Main log file of the system

cat
-n set number  
-A Include control characters (line breaks) linux $ Windows ^M$

Expand knowledge: 
dos2unix take Windows Convert file format to Unix format 
unix2dos take Unix Convert file format to Windows format 
for example
# unix2dos file1 
# dos2unix file2

less more head tail 
# head /etc/passwd
# head -2 /etc/passwd 
# tail /etc/passwd 
# tail -1 /etc/passwd 
# more /etc/passwd 
# After using the More command, the file content of one screen (one page) will be displayed on the screen each time, and the information of "- More -- (n%)" will appear at the end of the screen, where n% is the percentage of the file content that has been displayed. Press space next page, press b previous page, more +40 a.txt to view from line 40 
# tail -f /var/log/messages //-f view the tail of the file dynamically 
# Logger XXXXXXXX / / generate log information manually


grep Filter for file content 
# grep 'root' /etc/passwd 
# grep --color 'root' /etc/passwd 
# grep --color '^root' /etc/passwd 
# grep --color 'bash$' /etc/passwd

VII. Document management: modify the contents of documents

1 what is vim

It can be understood as a text editor under windows, such as Notepad, such as word document.

vi editor is usually referred to as vi, and vi is the abbreviation of visual editor. Its status on Linux is just like that of Edit program on DOS

Same. It can perform many text operations, such as output, delete, find, replace, block operation and so on, and users can modify it according to their own needs

Customization, which is not available in other editing programs.

vi editor is not a typesetting program. Unlike Word or WPS, it can arrange font, format, paragraph and other attributes. It only

Is a text editor. There is no menu, only commands, and there are many commands.

2 why use vim

Modify configuration

Write script

3. Difference between VI and vim

vi do not highlight color syntax

vim highlight color syntax

Other functions are no different

Therefore, under linux system, using vi or vim depends on personal habits.

vi is installed by default. vim needs to be installed via yum install vim -y

4 operation mode of vim editor

![image-20201122194829991](/Users/arther_wan/Library/Application Support/typora-user-images/image-20201122194829991.png)

-1. Command number mode: press ESC key in edit mode to return to command line mode

vi's default mode. In this mode, all inputs are interpreted as vi commands, which can execute commands such as modify, copy, move, paste and delete text, and can also move the cursor, search the string and exit vi.

-2. Edit / insert mode: enter one of a, i and o

In the edit mode, you can input text into a file. In this mode, each character entered will be interpreted as the input text by the vi editor. Use ESC to return to command line mode.
a: Enter insert mode and add after the cursor.
i: Enter the insertion mode and enter the insertion mode before the cursor.
o: Enter the insertion mode and open a new line under the current line (where the cursor is located).

-3. Extended / last line mode; Input:

It is also called last line mode on some UNIX systems. In this mode, you can use some advanced editing commands, such as searching and replacing strings, saving or exiting the vi editor. To enter the last line mode, you need to enter a colon (:) in the command line mode, which will move the cursor to the last line of the screen.

-4. Cursor movement in vi editor

Need to be in command line mode

Key combination (command) Cursor movement
$ The cursor moves to the end of the current line
^ Move the cursor to the beginning of the current line

-5. Delete and copy in command line mode

Need to be in command line mode

Key combination (command) meaning
dd Delete the line where the cursor is located
ndd n is a number; Delete the downward n line where the cursor is located, such as 20dd; Is to delete 20 lines down
yy Copy the line where the cursor is located
yy n is a number; Copy the downward n line where the cursor is located, such as 20dd; Is to copy down 20 lines

-6. Paste command

Key combination (command) meaning
p Paste the copied data in the next line under the cursor
P Paste the copied data in one line on the cursor

-7. Recovery and redo orders

Key combination (command) meaning
U Restore previous operation
ctrl+r Redo previous operation

-8. Master the common commands in the extended mode

command meaning
:w Save / write files to disk
:q Exit (quits)vi editor (without saving)
:wq Save / write files to disk and exit the (quits)vi editor

-8. Master the command to quickly move the position of the cursor in the file

command meaning
G Move the cursor before the first character on the last line of the file
nG n is a number, and the cursor moves to the nth line of the file. For example, 200G can be moved to line 200 of this file.
gg Move the first line of this file

-9. Master the command to quickly move the cursor position in the screen

command meaning
H Move the cursor to the first character of the top line of the screen
M Move the cursor to the first character of the line in the center of the screen
L The cursor moves to the first character of the bottom line of the screen

-10. Operation summary and supplement under three modes

Command mode: a. Cursor positioning 
						hjkl 
  					0 $ 
    				gg G 
      			3G Enter the third line 
        		/string(n N Recyclable) Quickly navigate to a row 
          b. Text editing (small amount) 
          	y 		copy yy 3yy ygg yG (In behavioral units) 
            d 		.delete dd 3dd dgg dG (In behavioral units) 
            p 		paste x Delete the character of the cursor 
            D 		Delete from cursor to end of line u undo revoke
            ^r 		redo redo 
            r 		Can be used to modify a character
          c. Enter other modes 
          	a Enter insert mode 
            i Enter insert mode 
            o Enter insert mode 
            A Enter insert mode 
            : Enter last line mode 
              ^v Enter visual block mode 
              R Enter replacement mode
              
Insert mode:
			^p Unique complement 
  
Last line mode: 
			a. Save exit 
  				:10 Go to line 10 
      		:w preservation 
          :q sign out 
          :wq Save and exit 
          :w! Force save 
          :q! Exit without saving 
          :wq! Force save exit 
          :x Save and exit
            
       b. Find replace 
      		:Range s/old/new/option 
          :1,5 s/Note/yyy/ From 1-5 OK Note Replace with yyy 
          :5,$ s/Note/yyy/ $Represents the last line 
          :1,$ s/to/xxx/g = :% s/to/xxx/g %Represent full text g Represent global 
          :4,9 s/^#//Replace lines 4-9 with empty ones starting with # 
          :% s#/dev/sda#/var/ccc#g 
          :,8 s/to/xxx/ From current line to line 8
            
       c. Read in file/Write file 
      		:w
          :w /aaa.txt Save as/aaa.txt
          :1,3 w /2.txt 
          :1 w! /root/ccc.txt If the target file already exists, you need to add it! number 
          :r /etc/hosts
            
       d. Setting environment 
      		Temporary settings: 
        		:set nu set nu  
            :set ic Case insensitive 
            :set ai Auto indent 
            :set list :set nonu Unset line number 
            :set noic 
          Permanent environment: 
          /etc/vimrc Affect all system users 
          ~/.vimrc Affect a user 
          # vim ~/.vimrc # Add a new file in the home directory and enter the following 
          set ic 
          set nu

-12. Edit multiple files at the same time

#1, 
vim -o File 1 file 2 # split horizon  
vim -O File 1 file 2 # For vertical separation, use ctrl + ww to switch 

#2, 
vim -p /etc/hosts /etc/grub.conf 
use ^PgUp, ^PgDn switch

-13. Document content comparison

# The difference comparison between the same files is usually used to compare the differences before and after modification. 
diff 				#File comparison (No) 
vimdiff 		#Open two files in vim mode and compare them to highlight different contents 
# patch experiment: 
touch a.txt b.txt [root@localhost ~]# echo 111 >> a.txt 
[root@localhost ~]# echo 222 >> b.txt 
[root@localhost ~]# diff -u a.txt b.txt > patch.diff 
[root@localhost ~]# patch a.txt patch.diff patching file a.txt 
[root@localhost ~]# cat a.txt 
222

Posted by websesame on Mon, 02 May 2022 23:34:53 +0300