Linux directory and file management

Keywords: Linux Operation & Maintenance vim

1. Linux directory structure

1.2, Root Directory

Location starting point for all partitions, directories, files, etc.
Use a separate'/'for the entire tree directory structure

1.2. Tree Catalog Structure

Common subdirectories: /root/bin/boot/dev/etc/home/var/usr/sbin/opt/mnt

1.3. Common subdirectories and their functions

/(root)The root (/) corresponds to an entry, the total entry to the file, and the only entry
rootSuperior directory of system administrator root
homeHost directory for normal users
bootSystem Kernel, Startup File
derDevice Files
etcconfiguration file
binCommands that all users can execute
sbinCommands that administrators can execute
userapplication program
varlog file
optDirectory where third-party applications are installed
mntDefault mount point

2. View and retrieve documents

2.1. View the contents of the file - cat

Format: cat [Options) File Name
 Example: Find opt 1 in Catalog.xtx File Content
[root@localhost opt]# ls
100m.img  99m.img  b.txx  ddd    jishiben.tar.gz  mulu.tar.gz  saaa2
1.txt     aaa      data   g.txt  ky15             passwd       s.txt
[root@localhost opt]# cat 1.txt 
1111
2222

333


444

Example: Find multiple files
[root@localhost opt]# cat 1.txt  g.txt 
1111                  Who shows who in front now, 1.txt stay g.txt Show 1 before.txt file
2222

333


444

ddd(g.xtx File Content)                                
ccc

Common options:

-nShow line numbers including blank lines
-bShow line numbers to skip blank lines
-sCompress multiple empty lines into one empty line
-AShow invisible characters (hidden characters)

-n Display line numbers including blank lines

[root@localhost opt]# cat 1.txt 
1111
2222

333


444

[root@localhost opt]# cat -n 1.txt 
     1	1111
     2	2222
     3	
     4	333
     5	
     6	
     7	444
     8	

-b Display line number to skip blank lines

[root@localhost opt]# cat -n 1.txt 
     1	1111
     2	2222
     3	
     4	333
     5	
     6	
     7	444
     8	
[root@localhost opt]# cat -b 1.txt 
     1	1111
     2	2222

     3	333


     4	444

-s Compress multiple empty lines into one empty line

[root@localhost opt]# cat 1.txt 
1111
2222

333


444

[root@localhost opt]# cat -s 1.txt 
1111
2222

333

444


-A Show invisible characters (hidden characters)

[root@localhost opt]# cat -A 1.txt 
1111$
2222$
$
333$
$
$
444$
$

2.2. View the contents of the file - more

Format: more [Options) File Name...
[root@localhost opt]# more 1.txt 
1111
2222

333


444
ssss
sss
aaa
szsd
ccc
111
qq
sssa

asada

adsdad


dadada
adada
dada
sss
adadas
adadada
adsada
adsads
asdsad
ada
dad
ada
das
--More--(78%)

Method of user action:
Press Enter to scroll down line by line
Press Space to turn down a screen
Press b to turn up a screen
Press q to exit
The more command has a disadvantage that pressing the last line will exit automatically

2.3. View the contents of the file - less

less is the same as the more command, but extends a lot.

Format: less [option]file name...
[root@localhost opt]# less 1.txt 



































1111
2222

333


444
ssss
sss
aaa
szsd
ccc
111
qq
sssa

asada

adsdad


dadada
adada
dada
sss
adadas
adadada
adsada
adsads
asdsad
ada
dad
ada
das
1.txt

Method of user action:
Page Up pages up, Page Down pages down
Press/Find Content
N Next Content N Previous Content
Press Enter to scroll down line by line
Press Space to turn down a screen
Press b to turn up a screen
Press q to exit
Usually used in conjunction with pipe characters;Pipeline |: Pass the result of the previous command to the next command as a parameter

[root@localhost opt]# ls /mnt/ | less



































-
10.txt
[123]
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
aa
aa{1...10}
aaa
AAA
a.txt
A.txt
b.txt
B.txt
c.txt
C.txt
DDD
d.txt
D.txt
e.txt
E.txt
f.txt
F.txt
g.txt
G.txt
hpasswd
h.txt
H.txt
:

2.4. Look at the beginning and end - head, tail

head View a portion of the beginning of the file (default is 10 lines)
Format: head -n Line number file name
[root@localhost opt]# head -n 10 1.txt 
1111
2222

333


444
ssss
sss
aaa
[root@localhost opt]# head -n 5 1.txt
1111
2222

333

[root@localhost opt]# 

tail View a small portion at the end of the file (default 10 lines)
Format: tail -n Line number file name
[root@localhost opt]# tail -n 10 1.txt 
da
da
dad
a
d

dasd
a
dq

[root@localhost opt]# tail -n 5 1.txt 

dasd
a
dq

[root@localhost opt]# 
Format: tail -f Real-time view of file names
[root@localhost opt]# ping www.baidu.com > g.txt 
[root@localhost opt]# tail -f g.txt 
64 bytes from 103.235.46.39 (103.235.46.39): icmp_seq=11 ttl=128 time=224 ms
64 bytes from 103.235.46.39 (103.235.46.39): icmp_seq=12 ttl=128 time=225 ms
64 bytes from 103.235.46.39 (103.235.46.39): icmp_seq=13 ttl=128 time=231 ms
64 bytes from 103.235.46.39 (103.235.46.39): icmp_seq=14 ttl=128 time=229 ms
64 bytes from 103.235.46.39 (103.235.46.39): icmp_seq=15 ttl=128 time=228 ms
64 bytes from 103.235.46.39 (103.235.46.39): icmp_seq=16 ttl=128 time=230 ms

--- www.wshifen.com ping statistics ---
16 packets transmitted, 15 received, 6% packet loss, time 15050ms
rtt min/avg/max/mdev = 223.086/228.504/232.884/2.736 ms

2.5. Contents of Statistics File - wc

Count the number of words in the file
 Format: wc [Options)... Target File...
-l Count rows  -w Count Words -c Statistics Bytes
[root@localhost opt]# cat 2.txt 
qqee  
sada
[root@localhost opt]# wc 2.txt 
 2  2 10 2.txt 2 Lines 2 letters qqee sada 8 Visible bytes+2 Hidden Characters=10 Bytes
[root@localhost opt]# cat -A 2.txt 
qqee$
sada$


[root@localhost opt]# cat 2.xt
ss aa            ss Spaces aa
aa ss            aa Spaces ss
[root@localhost opt]# wc 2.xt 
 2  4 12 2.xt 2 Lines 4 letters because there are spaces aa ss aa ss There are 4 letters and 12 bytes in total: spaces are also characters so ss+aa+aa+ss+2 Spaces+2 Hidden Characters=12

2.6, Retrieving and Filtering File Content - grep

Format: grep	[option]... Find Conditions Target File
 Filter out containment root The row target file is passwd
[root@localhost opt]# grep root passwd 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

-oShow only the files you want
-fCompare the same parts of two files
-vReverse
-iIgnore case in search
grep -i ignore case
[root@localhost opt]# grep -i ROOT passwd 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
grep -f Compare the same parts of two files

grep -o Show the file you want
[root@localhost opt]# grep -o root passwd 
root
root
root
root
grep -v Reverse
[root@localhost opt]# grep root passwd (row containing root)
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost opt]# Grep-v root passwd (excluding rows of root)
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

3. Backup and recovery

3.1. Compression and decompression commands - gzip, bzip2

Format: gzip [-9] file name			 Compressed File
     bzip2 [-9] file name		     Compressed File
     gzip -d .gz Compressed file in format     Unzip File
     bzip2 -d .bz2 Format Compressed File Unzip File
-9Compression level, 9 highest and 1 worst
-kFor bzip2 only, source files can be preserved after compression

3.2. Archive Command - tar

Format: tar []... Archive File Name Source File or Directory
     tar []... Archive File Name [-C Target Directory]
-cCreate a tar file, typically with
-vShow Details
-fUsing archive files
-xUnpack, Unpack tar
-pKeep original permissions
-CSpecify extraction directory
-tList view files in the tar package without unpacking it
-zCompress the end of gz with g z ip
-jCompress the end j of bz2 using bzip2
[root@localhost opt]# tar -zcvf bijiben.tar.gz 1.txt 2.txt 
1.txt
2.txt
[root@localhost opt]# ls
100m.img.gz  2.xt     **bijiben.tar.gz** (Created successfully) ddd              ky15         saaa2
1.txt        99m.img  b.txx           g.txt            mulu.tar.gz  s.txt
2.txt        aaa      data            jishiben.tar.gz  passwd
[root@localhost opt]# tar -jcvf csgo.tar.bz2 s.txt 
s.txt
[root@localhost opt]# ls
100m.img.gz  2.xt     bijiben.tar.gz  data   jishiben.tar.gz  passwd
1.txt        99m.img  b.txx           ddd    ky15             saaa2
2.txt        aaa      ***csgo.tar.bz2*** (Created successfully)   g.txt  mulu.tar.gz      s.txt

Posted by Panthers on Thu, 02 Sep 2021 16:29:54 -0700