Bash SHELL file management

Time:2020-1-2

Bash shell manages files

========================================================
Create, copy, delete, move, view, edit, compress, find

Summary:
Linux directory structure
File location path
file management

1、 Linux directory structure

Windows: organizing files in a multi root way C: \ D: \ e:\
Linux: organizing files as a single root/

/Directory structure: FSH (filesystem Hierarchy Standard)
[[email protected] ~]# ls /
bin dev lib media net root srv usr
boot etc lib64 misc opt sbin sys var
cgroup home lost+found mnt proc selinux tmp

Note: the figure below shows the directory structure of centos7

Bin commands used by ordinary users / bin / LS, / bin / date
SBIN commands used by administrators / SBIN / service
Dev device files / dev / SDA, / dev / sda1, / dev / tty1, / dev / tty2, / dev / PTS / 1, / dev / zero, / dev / null, / dev / random
Root user’s home
Home stores the home directory of ordinary users
When lost + found fsck is repaired, the unlinked files or directories are stored
Proc virtual file system, reflecting the kernel, process information or real-time status

Usr system file, equivalent to C: \ windows
/The directory where the usr / local software is installed, equivalent to C: \ program
/Usr / bin application used by ordinary users
/Application used by usr / SBIN administrator
/Usr / lib library file glibc
/Usr / lib64 library file glibc
Boot files related to system startup, such as kernel, grub (boot loader)
Etc configuration file (system related such as network / etc / sysconfig / network, application related configuration file such as / etc / SSH / sshd_config…)
Lib library file glibc
Lib64 library file glibc
TMP temporary file (Global writable: temporary file generated by process)
Var stores some change files, such as database, log, mail
mysql: /var/lib/mysql
vsftpd: /var/ftp
mail: /var/spool/mail
cron: /var/spool/cron
log: /var/log
Temporary file / var / TMP (temporary file generated by the process)

Device (mainly storage device) mount directory

Media the default mount point for mobile devices
Mount point of MNT manual mount device
Misc automount process mounting
Net automount process mount

Command storage location

/Bin commands used by ordinary users / bin / LS, / bin / date rhel7 is obsolete
/Command used by SBIN administrator rhel7 obsolete
/Usr / bin application used by ordinary users
/Application used by usr / SBIN administrator

Library file storage location

/Lib library file glibc rhel7 obsolete
/Lib64 library file glibc rhel7 obsolete
/Usr / lib library file glibc
/Usr / lib64 library file glibc

File time

LS – L filename only looks at the modification time of the file

There are four times for Linux files:
#Stat anaconda-ks.cfg / / view the detailed attributes of the file (including the file time attribute)
Access time: atime, view content / / rhel6 will delay modifying atime
Modification time: mtime, modification content
Change time: CTime, file attributes, such as permissions
Delete time: dtime, when the file was deleted

[[email protected] ~]# ls -l install.log
-rw-r–r– 1 root root 34793 10-23 13:49 install.log

[[email protected] ~]# stat install.log
File: “install.log”
Size: 34793 blocks: 80 IO block: 4096 general file
Device: 802h/2050d Inode: 65282 Links: 1
Access: (0644/-rw-r–r–) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2012-10-23 16:53:20.000000000 +0800
Modify: 2012-10-23 13:49:46.000000000 +0800
Change: 2012-10-23 13:49:52.000000000 +0800

Note: rhel6 starts to modify relative, which must meet one of the following conditions:
1. Since the last atime modification, it has reached 86400 seconds;
2. When a write operation occurs;

file type

Judging the type of file by color is not necessarily correct!!!
There is no extension for files in Linux system!!!

Method 1:
LS – L filename / / look at the first character
-Normal files (text files, binary files, compressed files, movies, pictures…)
D. catalog file (blue)
B. hard disk of device file (block device), U disk / dev / SDA, / dev / sda1
C device file (character device) printer, terminal / dev / tty1
S socket file
P. pipeline documents
L linked files (light blue)

[[email protected] ~]# ll -d /etc/hosts /bin/ls /home /dev/sda /dev/tty1 /etc/grub2.cfg /dev/log /run/dmeventd-client
-rwxr-xr-x. 1 root root 117616 Nov 20 2015 /bin/ls
srw-rw-rw- 1 root root 0 Mar 14 2017 /dev/log
brw-rw—- 1 root disk 8, 0 Mar 14 09:03 /dev/sda
crw–w—- 1 root tty 4, 1 Mar 14 09:03 /dev/tty1
lrwxrwxrwx. 1 root root 22 Dec 21 01:40 /etc/grub2.cfg -> ../boot/grub2/grub.cfg
-rw-r–r– 1 root root 392 Feb 22 15:51 /etc/hosts
drwxr-xr-x. 10 root root 4096 Mar 14 11:00 /home

Method 2: file
[[email protected] ~]$ file /etc/hosts
/etc/hosts: ASCII text

[[email protected] ~]$ file /bin/ls
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=aa7ff68f13de25936a098016243ce57c3c982e06, stripped

[[email protected] ~]$ file /dev/sda
/dev/sda: block special

[[email protected] ~]$ file /dev/tty1
/dev/tty1: character special

[[email protected] ~]$ file /etc/grub2.cfg
/etc/grub2.cfg: broken symbolic link to `../boot/grub2/grub.cfg’

[[email protected] ~]$ file /home
/home: directory

[[email protected] ~]$ file /run/dmeventd-client
/run/dmeventd-client: fifo (named pipe)

2、 Path (location file)

Where do you want to create files?
What files do you want to copy to where?
Where and what files do you want to delete?

/Home / Alice / file1 and / home / Bob / file1 are not the same file!!!
/ABC / file5 and ABC / file5 may be different!!!

Absolute path: path from / to / home / Alice / file1
Relative path: start relative to current directorya.txt ./a.txt ../bob/b.txt[suppose: in the directory / home / Alice]

# useradd alice
# touch /home/alice/file1
# touch ~/file2
# touch ~alice/file3

# pwd
# mkdir abc
# touch ../file3
# touch file4
# touch abc/file5

3、 Document management

1. Document management: CD changeCatalog
CD absolute path CD / home / Alice Cd ~ Alice
CD relative path CD desktop / ABC CD.. CD
=======================================================
CD:
CD – return to last directory
CD go straight home
=======================================================
2. File management: create / copy / move / delete
= = create
File touch
#Touch file1.txt / / if there is none, create it. If there is one, modify it
# touch file3 file4
# touch /home/file10.txt
# touch /home/file5 file6
# touch /home/{zhuzhu,gougou}
# touch file{1..20}
# touch file{a..c}
#Touch Yang {a, B, C} / / {} set, equivalent touch yanga yangb yangc

Directory MKDIR
# mkdir dir1
# mkdir /home/dir2 /home/dir3
# mkdir /home/{dir4,dir5}
# mkdir -v /home/{dir6,dir7}
# mkdir -v /hoem/dir8/111/22
#MKDIR – PV / hoem / dir8 / 111 / 222 / / including the creation of their parents, no message will be output
# mkdir -pv /home/{yang/{dir1,111},CentOS}

= = copy CP
Usage: cp [OPTION]… [-T] SOURCE DEST
# cd
# mkdir /home/dir{1,2}
# cp -v install.log /home/dir1
# cp -v install.log /home/dir1/yang.txt
# cp -rv /etc /home/dir1
#CP – V install.log / home / dir90 / / no / home / dir90
# cp -v install.log /home/dir2
# cp -v anaconda-ks.cfg !$
#CP – RV / etc / sysconfig / network scripts / ifcfg-eth0 / etc / passwd / etc / grub.conf / home / dir2 / / copy multiple files to the same directory
# cp -rv /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/passwd /etc/grub.conf /etc/hosts .
# cp -r /etc /tmp
# cp -rf /etc /tmp
# \cp -r /etc /tmp

[[email protected] ~]# type -a cp
cp is aliased to `cp -i’
cp is /bin/cp

[[email protected]S ~]# cp -rv /etc/sysconfig/network-scripts/{ifcfg-eth0,ifcfg-eth0.bak}
[[email protected] ~]# cp -rv /etc/sysconfig/network-scripts/ifcfg-eth0{,-org}
[[email protected] ~]# cp -rv /etc/sysconfig/network-scripts/{ifcfg-eth0,yang-eth0}

= = mobile MV
Usage: mv [OPTION]… [-T] SOURCE DEST
#MV file1 / home / dir3 move File2 to / home / dir3
#MV File2 / home / dir3 / file20 move File2 to / home / dir3 and rename file20
#MV file4 file5 rename file4 to file5, and the current location is renamed

= = delete RM
Example 1: delete / home / dir1
# cd /home
# rm -rf dir1
-r recursion
-F force force
-V detailed process

Example 2:
[[email protected] ~]# mkdir /home/dir10
[[email protected] ~]# touch /home/dir10/{file2,file3,.file4}
[root @ CentOS ~] (RM – RF / home / dir10 / * / / hidden files are not included
[[email protected] ~]# ls /home/dir10/ -a
. .. .file4

Example 3:
[[email protected] ~]# rm -rf file*
[[email protected] ~]# rm -rf *.pdf

3. Document management: View document content
==Cat TAC less more head tail tail grep
/bin/date
/etc/hosts
/etc/sysconfig/network
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/passwd
/etc/shadow
/etc/group
/etc/grub2.cfg
/etc/resolv.conf
/Etc / profile / / set system environment variables
/Etc / bashrc / / affects bash shell environment
/Var / log / messages / / system main log file
/Var / log / secure / / security related logs such as SSH login, local login

cat
-N display line number
-A includes control characters (newline / tab)
linux $
Windows ^M$

less more head tail tailf
[[email protected] ~]# head /etc/passwd
[[email protected] ~]# head -2 /etc/passwd
[[email protected] ~]# tail /etc/passwd
[[email protected] ~]# tail -1 /etc/passwd
[[email protected] ~]# tail /var/log/messages
[[email protected] ~]# tail -20 /var/log/messages
[root @ CentOS ~] ා tail – F / var / log / secure / / – f dynamically view the tail of the file
[root @ CentOS ~] ා tail – F / var / log / secure / / – f dynamically view the tail of the file
Note: when vim and GEDIT edit the file, the index number will change

Grep filters file content
# grep ‘root’ /etc/passwd
# grep ‘^root’ /etc/passwd
# grep ‘bash$’ /etc/passwd
# grep ‘failure’ /var/log/secure

4. Document management: modify document content
[[email protected] ~]# ll -a > list.txt
File editor GEDIT
File editor VI, VIM, nano

 

 

Command mode:
a. Cursor positioning
hjkl
0 $
gg G
3G enters the third line
/String (n n can be looped) to quickly locate a line
/^d
/txt$

b. Text editing (small amount)
Y copy YY 3yy YGG YG in behavior units
D. delete DD 3dd DGG DG in behavioral units
P paste
X delete the character where the cursor is
D delete from cursor to end of line
U undo
^Redo
R can be used to modify a character

c. Enter other modes
A. enter insertion mode
I. enter insertion mode
O enter insertion mode
A. enter insertion mode

: enter last line mode (extended command mode)
V enter visual mode
^V. enter visual block mode
V enters visual line mode
R enter replace mode

Insertion mode:

Visual block mode:
Block insert (add character before specified block): select block, I insert character before block, ESC
Block replace: select a block, R enter the replaced character
Block delete: select block, D | x
Block copy: select block, y

Extended command mode:
a. Save exit
: 10 enter line 10
W preservation
Q exit
: WQ save and exit
: W! Force save
: q! Do not save and exit
: WQ! Force save to exit
: X save and exit zz

b. Find replacement
: range s / old / new / option
: 1,5 s / root / Yang / replace the root of line 1-5 with Yang
: 5, $s / root / Yang / $indicates the last line
: 1, $s / root / Yang / g =:% s / root / Yang / g% means full-text g means global
:% s#/dev/sda#/var/ccc#g
:, 8 s / root / Yang / from current line to line 8
: 4,9 s / ^# / / replace the beginning of line 4-9 with null
: 5,10 s /. * / # & / 5-10 before adding the # (. * whole line & reference to find content)

c. Read in file / write file (save as)
: W store to current file
: w / TMP / aaa.txt save as / TMP / aaa.txt
:1,3 w /tmp/2.txt
: R / etc / hosts after reading the file to the current line
: 5 R / etc / hosts read in the file after line 5

d. Setting up the environment
Temporary settings:
: set Nu set line number
: set IC is case insensitive
: set AI auto indent
: set list display control characters
: set nonu unset line number
:set noic

Permanent environment:
/Etc / vimrc affects all system users
~/. vimrc affects a user

# vim ~/.vimrc
set ic
set nu

$sudo VIM – O / etc / hosts / etc / grub2.cfg ^ WW switch
$ cp /etc/hosts hosts1
$ vimdiff /etc/hosts hosts1