BASH Shell 對檔案進行管理
========================================================
創建、復制、洗掉、移動、查看、編輯、壓縮、查找
內容提要:
Linux目錄結構
檔案定位 路徑
檔案管理
一、Linux目錄結構
Windows: 以多根的方式組織檔案 C:\ D:\ E:\
Linux: 以單根的方式組織檔案 /
/目錄結構: FSH (Filesystem Hierarchy Standard)
[root@CentOS ~]# 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
注:下圖為Centos7目錄結構
bin 普通用戶使用的命令 /bin/ls, /bin/date
sbin 管理員使用的命令 /sbin/service
dev 設備檔案 /dev/sda,/dev/sda1,/dev/tty1,/dev/tty2,/dev/pts/1, /dev/zero, /dev/null, /dev/random
root root用戶的HOME
home 存盤普通用戶家目錄
lost+found fsck修復時,存盤沒有鏈接的檔案或目錄
proc 虛擬的檔案系統,反映出來的是內核,行程資訊或實時狀態
usr 系統檔案,相當于C:\Windows
/usr/local 軟體安裝的目錄,相當于C:\Program
/usr/bin 普通用戶使用的應用程式
/usr/sbin 管理員使用的應用程式
/usr/lib 庫檔案Glibc
/usr/lib64 庫檔案Glibc
boot 存放的系統啟動相關的檔案,例如kernel,grub(引導裝載程式)
etc 組態檔(系統相關如網路/etc/sysconfig/network,應用相關組態檔如/etc/ssh/sshd_config...)
lib 庫檔案Glibc
lib64 庫檔案Glibc
tmp 臨時檔案(全域可寫:行程產生的臨時檔案)
var 存放的是一些變化檔案,比如資料庫,日志,郵件....
mysql: /var/lib/mysql
vsftpd: /var/ftp
mail: /var/spool/mail
cron: /var/spool/cron
log: /var/log
臨時檔案: /var/tmp(行程產生的臨時檔案)
設備(主要指存盤設備)掛載目錄
media 移動設備默認的掛載點
mnt 手工掛載設備的掛載點
misc automount行程掛載
net automount行程掛載
命令存盤位置
/bin 普通用戶使用的命令 /bin/ls, /bin/date RHEL7淘汰
/sbin 管理員使用的命令 RHEL7淘汰
/usr/bin 普通用戶使用的應用程式
/usr/sbin 管理員使用的應用程式
庫檔案存盤位置
/lib 庫檔案Glibc RHEL7淘汰
/lib64 庫檔案Glibc RHEL7淘汰
/usr/lib 庫檔案Glibc
/usr/lib64 庫檔案Glibc
檔案時間
ls -l 檔案名 僅看的是檔案的修改時間
Linux檔案有四種時間:
# stat anaconda-ks.cfg //查看檔案的詳細屬性(其中包括檔案時間屬性)
訪問時間:atime,查看內容 //RHEL6會延后修改atime
修改時間:mtime,修改內容
改變時間:ctime,檔案屬性,比如權限
洗掉時間:dtime,檔案被洗掉的時間
[root@CentOS ~]# ls -l install.log
-rw-r--r-- 1 root root 34793 10-23 13:49 install.log
[root@CentOS ~]# stat install.log
File: “install.log”
Size: 34793 Blocks: 80 IO Block: 4096 一般檔案
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
注:RHEL6開始relatime,atime延遲修改,必須滿足其中一個條件:
1. 自上次atime修改后,已達到86400秒;
2. 發生寫操作時;
檔案型別
通過顏色判斷檔案的型別是不一定正確的!!!
Linux系統中檔案是沒有擴展名!!!
方法一:
ls -l 檔案名 //看第一個字符
- 普通檔案(文本檔案,二進制檔案,壓縮檔案,電影,圖片,,,)
d 目錄檔案(藍色)
b 設備檔案(塊設備)存盤設備硬碟,U盤 /dev/sda, /dev/sda1
c 設備檔案(字符設備)列印機,終端 /dev/tty1
s 套接字檔案
p 管道檔案
l 鏈接檔案(淡藍色)
[root@CentOS ~]# 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
方法二:file
[yang@CentOS ~]$ file /etc/hosts
/etc/hosts: ASCII text
[yang@CentOS ~]$ 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
[yang@CentOS ~]$ file /dev/sda
/dev/sda: block special
[yang@CentOS ~]$ file /dev/tty1
/dev/tty1: character special
[yang@CentOS ~]$ file /etc/grub2.cfg
/etc/grub2.cfg: broken symbolic link to `../boot/grub2/grub.cfg'
[yang@CentOS ~]$ file /home
/home: directory
[yang@CentOS ~]$ file /run/dmeventd-client
/run/dmeventd-client: fifo (named pipe)
二、路徑 (定位檔案)
你要在哪兒創建檔案?
你要將什么檔案復制到什么地方?
你要洗掉什么地方的什么檔案?
/home/alice/file1 和 /home/bob/file1 不是同一檔案!!!
/abc/file5 和 abc/file5 有可能是不一樣的!!!
絕對路徑: 從/開始的路徑 /home/alice/file1
相對路徑: 相對于當前目錄開始 a.txt ./a.txt ../bob/b.txt [假如:此時在目錄/home/alice]
# useradd alice
# touch /home/alice/file1
# touch ~/file2
# touch ~alice/file3
# pwd
# mkdir abc
# touch ../file3
# touch file4
# touch abc/file5
三、檔案管理
1. 檔案管理之:cd 改變目錄
cd 絕對路徑 cd /home/alice cd ~alice
cd 相對路徑 cd Desktop/abc cd .. cd .
=======================================================
cd 專用:
cd - 回傳上次目錄
cd 直接回家
=======================================================
2. 檔案管理之:創建/復制/移動/洗掉
==創建
檔案 touch
# touch file1.txt //無則創建,有則修改時間
# 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} //{}集合,等價touch yanga yangb yangc
目錄 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 //包括其父母的創建,不會有任何訊息輸出
# mkdir -pv /home/{yang/{dir1,111},CentOS}
==復制 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 //沒有/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 //將多個檔案拷貝到同一個目錄
# 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
[root@CentOS ~]# type -a cp
cp is aliased to `cp -i'
cp is /bin/cp
[root@CentOS ~]# cp -rv /etc/sysconfig/network-scripts/{ifcfg-eth0,ifcfg-eth0.bak}
[root@CentOS ~]# cp -rv /etc/sysconfig/network-scripts/ifcfg-eth0{,-org}
[root@CentOS ~]# cp -rv /etc/sysconfig/network-scripts/{ifcfg-eth0,yang-eth0}
==移動 mv
Usage: mv [OPTION]... [-T] SOURCE DEST
# mv file1 /home/dir3 將file2移動到/home/dir3
# mv file2 /home/dir3/file20 將file2移動到/home/dir3,并改名為file20
# mv file4 file5 將file4重命名為file5,當前位置的移動就是重命名
==洗掉 rm
示例1:洗掉/home/dir1
# cd /home
# rm -rf dir1
-r 遞回
-f force強制
-v 詳細程序
示例2:
[root@CentOS ~]# mkdir /home/dir10
[root@CentOS ~]# touch /home/dir10/{file2,file3,.file4}
[root@CentOS ~]# rm -rf /home/dir10/* //不包括隱藏檔案
[root@CentOS ~]# ls /home/dir10/ -a
. .. .file4
示例3:
[root@CentOS ~]# rm -rf file*
[root@CentOS ~]# rm -rf *.pdf
3. 檔案管理之:查看檔案內容
==文本檔案 (cat tac less more head tail tailf 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 //設定系統環境變數
/etc/bashrc //影響bash shell環境
/var/log/messages //系統主日志檔案
/var/log/secure //跟安全相關的日志如ssh登錄,本地登錄...
cat
-n 顯示行號
-A 包括控制字符(換行符/制表符)
linux $
Windows ^M$
less more head tail tailf
[root@CentOS ~]# head /etc/passwd
[root@CentOS ~]# head -2 /etc/passwd
[root@CentOS ~]# tail /etc/passwd
[root@CentOS ~]# tail -1 /etc/passwd
[root@CentOS ~]# tail /var/log/messages
[root@CentOS ~]# tail -20 /var/log/messages
[root@CentOS ~]# tail -f /var/log/secure //-f 動態查看檔案的尾部
[root@CentOS ~]# tail -F /var/log/secure //-F 動態查看檔案的尾部
注:vim, gedit編輯檔案時,索引號會改變
grep 針對檔案內容進行過濾
# grep 'root' /etc/passwd
# grep '^root' /etc/passwd
# grep 'bash$' /etc/passwd
# grep 'failure' /var/log/secure
4. 檔案管理之:修改檔案內容
[root@CentOS ~]# ll -a > list.txt
檔案編輯器 gedit
檔案編輯器 vi, vim, nano 
命令模式:
a. 游標定位
hjkl
0 $
gg G
3G 進入第三行
/string (n N 可以回圈的) 快速定位到某一行
/^d
/txt$
b. 文本編輯(少量)
y 復制 yy 3yy ygg yG (以行為單位)
d 洗掉 dd 3dd dgg dG (以行為單位)
p 粘貼
x 洗掉游標所在的字符
D 從游標處洗掉到行尾
u undo撤銷
^r redo重做
r 可以用來修改一個字符
c. 進入其它模式
a 進入插入模式
i 進入插入模式
o 進入插入模式
A 進入插入模式
: 進入末行模式(擴展命令模式)
v 進入可視模式
^v 進入可視塊模式
V 進入可視行模式
R 進入替換模式
插入模式:
可視塊模式:
塊插入(在指定塊前加入字符): 選擇塊,I 在塊前插入字符, ESC
塊替換: 選擇塊,r 輸入替換的字符
塊洗掉: 選擇塊,d | x
塊復制: 選擇塊,y
擴展命令模式:
a. 保存退出
:10 進入第10行
:w 保存
:q 退出
:wq 保存并退出
:w! 強制保存
:q! 不保存并退出
:wq! 強制保存退出
:x 保存并退出 ZZ
b. 查找替換
:范圍 s/old/new/選項
:1,5 s/root/yang/ 從1-5行的root 替換為yang
:5,$ s/root/yang/ $表示最后一行
:1,$ s/root/yang/g = :% s/root/yang/g %表示全文 g表示全域
:% s#/dev/sda#/var/ccc#g
:,8 s/root/yang/ 從當前行到第8行
:4,9 s/^#// 4-9行的開頭#替換為空
:5,10 s/.*/#&/ 5-10前加入#字符 (.*整行 &參考查找的內容)
c. 讀入檔案/寫檔案(另存為)
:w 存盤到當前檔案
:w /tmp/aaa.txt 另存為/tmp/aaa.txt
:1,3 w /tmp/2.txt
:r /etc/hosts 讀入檔案到當前行后
:5 r /etc/hosts 讀入檔案到第5行后
d. 設定環境
臨時設定:
:set nu 設定行號
:set ic 不區分大小寫
:set ai 自動縮進
:set list 顯示控制字符
:set nonu 取消設定行號
:set noic
永久的環境:
/etc/vimrc 影響所有系統用戶
~/.vimrc 影響某一個用戶
# vim ~/.vimrc
set ic
set nu
$ sudo vim -O /etc/hosts /etc/grub2.cfg ^ww切換
$ cp /etc/hosts hosts1
$ vimdiff /etc/hosts hosts1
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/160730.html
標籤:Linux
上一篇:Nginx實作防盜鏈的方式
下一篇:用戶管理
