1. 程式人生 > 其它 >檔案基礎命令

檔案基礎命令

檔案基礎命令

系統目錄

windows是多根目錄

Linux是單根目錄

ls

[root@localhost ~]# ls /    檢視 當前資料夾下的檔案目錄
a  bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@localhost ~]# ls -l /
總用量 20
drwxr-xr-x.   4 root root   64 6月  23 10:42 a
lrwxrwxrwx.   1 root root    7 6月  22 01:02 bin -> usr/bin  # 普通使用者使用的命令
dr-xr-xr-x.   5 root root 4096 6月  22 04:59 boot            # 啟動檔案
drwxr-xr-x   19 root root 3100 6月  23 14:30 dev            # 裝置目錄檔案
drwxr-xr-x.  88 root root 8192 6月  23 14:30 etc            # 配置檔案
drwxr-xr-x.   5 root root   39 6月  22 14:41 home            # 普通使用者目錄
lrwxrwxrwx.   1 root root    7 6月  22 01:02 lib -> usr/lib     # 庫檔案
lrwxrwxrwx.   1 root root    9 6月  22 01:02 lib64 -> usr/lib64 # 庫檔案
drwxr-xr-x.   2 root root    6 4月  11 2018 media
drwxr-xr-x.   2 root root    6 4月  11 2018 mnt
drwxr-xr-x.   3 root root   16 6月  22 04:57 opt
dr-xr-xr-x  118 root root    0 6月  23 14:30 proc
dr-xr-x---.   4 root root  192 6月  23 11:08 root           # root使用者
drwxr-xr-x   29 root root  840 6月  23 14:30 run
lrwxrwxrwx.   1 root root    8 6月  22 01:02 sbin -> usr/sbin # 管理員使用的命令
drwxr-xr-x.   2 root root    6 4月  11 2018 srv
dr-xr-xr-x   13 root root    0 6月  23 14:30 sys
drwxrwxrwt.  24 root root 4096 6月  23 14:30 tmp             # 存放臨時檔案,會過段時間刪掉
drwxr-xr-x.  13 root root  155 6月  22 01:02 usr             # 系統檔案 相當於 c:\windows
drwxr-xr-x.  20 root root  282 6月  22 04:57 var             # 存放一些可變的檔案,,資料庫,日誌檔案
[root@localhost ~]# ls -l
總用量 8
-rw-------. 1 root root 1756 6月  22 01:16 anaconda-ks.cfg
-rw-r--r--. 1 root root   19 6月  22 10:54 a.sh
[root@localhost ~]# 

'''
/tmp   系統的臨時檔案,系統會定時刪除改目錄下長時間沒有訪問的檔案

/etc/sysconfig/network-script/ifcfg-*,網路配置檔案
/etc/hostname,系統主機名配置檔案
/etc/resolv.conf,dns客戶端配置檔案
/etc/hosts,本地域名解析配置檔案
/etc/fstab    系統掛載目錄 開機自啟動掛載列表
/etc/passwd  系統使用者檔案
'''

更改主機名的兩種方法:
   1.修改系統主機的配置檔案
   2.hostnamectl set-hostname 主機名


ls             # 預設瀏覽當前目錄
ls -l 目錄      # 瀏覽目錄下的子目錄檔名的詳細資訊
ls -al 目錄     # 瀏覽的結果中包含隱藏檔案
ls -dl 目錄     # 檢視目錄

[root@localhost ~]# ll /boot
總用量 98896
-rw-r--r--. 1 root root   153591 10月 20 2020 config-3.10.0-1160.el7.x86_64
drwxr-xr-x. 3 root root       17 6月  22 01:02 efi
drwxr-xr-x. 2 root root       27 6月  22 01:04 grub
drwx------. 5 root root      113 6月  22 10:15 grub2
-rw-------. 1 root root 62500154 6月  22 01:11 initramfs-0-rescue-39e1259751d9445c8bb1864ed271e37e.img
-rw-------. 1 root root 21129085 6月  22 04:59 initramfs-3.10.0-1160.el7.x86_64.img
-rw-r--r--. 1 root root   320648 10月 20 2020 symvers-3.10.0-1160.el7.x86_64.gz
-rw-------. 1 root root  3616707 10月 20 2020 System.map-3.10.0-1160.el7.x86_64
-rwxr-xr-x. 1 root root  6769256 6月  22 01:11 vmlinuz-0-rescue-39e1259751d9445c8bb1864ed271e37e
-rwxr-xr-x. 1 root root  6769256 10月 20 2020 vmlinuz-3.10.0-1160.el7.x86_64
[root@localhost ~]# 

"""
各部分的解析
   許可權第一個代表檔案型別
       -   普通文字
       d   目錄檔案
       b   裝置檔案
       c   裝置檔案
       s   套接字檔案
       p   管道檔案
       l   連線檔案
   rwxr-xr-x.    代表權限,後面的點代表在selinux開啟的情況下建立的檔案
   1             硬連結數
   root          屬主
   root          屬組
   0             檔案大小,單位位元組
   10月 20 2020  檔案的建立時間
   efi           檔名
"""

pwd和cd

[root@localhost ~]# pwd
/root
pwd檢視當前目錄

cd 絕對路徑/相對路徑
cd -  返回上次目錄
cd    相當於cd ~
cd .. 返回上一級目錄

檔案的建立

建立檔案
[root@localhost ~]# touch /home/lyh/a.txt             無則建立,有則修改時間
[root@localhost ~]# touch /home/lyh/{1..4}.txt        建立 1.txt  2.txt  3.txt  4.txt
[root@localhost ~]# touch /home/lyh/{q,w,e}.txt       建立 a.txt  e.txt  q.txt  w.txt
[root@localhost ~]# ls /home/lyh/
1.txt  2.txt  3.txt  4.txt  a.txt  e.txt  q.txt  w.txt
[root@localhost ~]# 

建立資料夾
[root@localhost ~]# mkdir /home/lyh/a                   在/home/lyh/下建立a資料夾
[root@localhost ~]# mkdir /home/lyh/b /home/lyh/c       在/home/lyh/下分別建立 b資料夾和c資料夾
[root@localhost ~]# mkdir -v /home/lyh/{1..3}           在/home/lyh/下建立1,2,3資料夾,因為這裡用的-v是快捷方式,所以下面會有一個互動的訊息
mkdir: 已建立目錄 "/home/lyh/1"                            
mkdir: 已建立目錄 "/home/lyh/2"
mkdir: 已建立目錄 "/home/lyh/3"
[root@localhost ~]# mkdir -v /home/lyh/{q,w}           在/home/lyh/下建立q,w兩個資料夾    
mkdir: 已建立目錄 "/home/lyh/q"
mkdir: 已建立目錄 "/home/lyh/w"
[root@localhost ~]# mkdir -p /home/lyh/mmm/nnn/aaa     遞迴建立,因為/home/lyh/下沒有mmm這個資料夾,所以這裡用遞迴,連同aaa的父即目錄一起建立
[root@localhost ~]# \mkdir /home/lyh/asd               在命令前面加一個\,讓命令按照最原始的方式執行
[root@localhost ~]# tree /home/lyh/                    檢視檔案目錄的樹狀結構
/home/lyh/
├── 1
├── 2
├── 3
├── a
├── asd
├── b
├── c
├── mmm
│ └── nnn
│     └── aaa
├── q
└── w

12 directories, 0 files
[root@localhost ~]# 

檢視目錄樹
tree -a 目錄      #顯示所有檔案,包括隱藏檔案(以.開頭的是隱藏檔案)
tree -d 目錄      #只顯示子目錄
tree -L 1 目錄    # -L 遍歷目錄的最大層數,-L後面就是你要遍歷的層數
tree -F 目錄      # -F 在條目錄後加上檔案型別的指定符號
tree--help        # 檢視幫助

檔案的刪除

rm -rf 目錄  記住這一個就行,,萬物可刪,,如果要刪檔案下下的東西,在目錄後面加一個*,但是一般不要輕易執行這個,因為這個是直接刪,如果不要的檔案可以放到temp資料夾中,這個資料夾一段時間清理一次。重要事情說三遍,,這條命令不要隨便你執行。

檔案的複製

cp  源路徑  目標路徑

[root@localhost ~]# cp /etc/passwd /home/ 
[root@localhost ~]# ls /home/
asd  lyh  passwd  qwe
[root@localhost ~]# 
[root@localhost ~]# cp -v /etc/passwd /home/   這個是展示了一個拷貝的詳細過程
cp:是否覆蓋"/home/passwd"? y
"/etc/passwd" -> "/home/passwd"
[root@localhost ~]# touch /home/lyh/aa/1.txt
[root@localhost ~]# cp /home/lyh/aa/1.txt /home/qwe/
[root@localhost ~]# ls /home/qwe/
1.txt
'''
拷貝呢這裡只能只能拷貝檔案,,不能拷貝資料夾
'''

cp-r  源路徑  目標路徑
[root@localhost etc]# cp -r /home/lyh/aa/ /home/qwe/    遞迴拷貝,從父級目錄到要拷貝的那一層
[root@localhost etc]# ls /home/qwe/
1.txt  aa
[root@localhost etc]# 

'''
root使用者使用cp,這其實是一個別名,會顯示提示資訊,如果不想顯示提示資訊,在命令前面加個\, 
-f 是強制命令,
'''

檔案的移動

mv 源目錄 目標目錄

[root@localhost etc]# mkdir /home/lyh/aaa
[root@localhost etc]# mkdir /home/qwe/bbb
[root@localhost etc]# mv /home/lyh/aaa/ /home/qwe/bbb/   # 資料夾移動,檔案的移動也是一樣的
[root@localhost etc]# ls /home/qwe/bbb/
aaa

-r 遞迴
-f 強制
-v 詳細過程
[root@localhost etc]# touch /home/lyh/{1..3}.txt
[root@localhost etc]# mv /home/lyh/* /home/qwe/bbb/    # 移動這個資料夾下面的 1.txt  2.txt  3.txt
[root@localhost etc]# ls /home/qwe/bbb/
1.txt  2.txt  3.txt  aaa
[root@localhost etc]# 


'''
檔案移動後,原始檔就沒有了,,檔案複製後,原始檔還是存在的
'''

檢視檔案內容

文字檔案
=====>cat
[root@localhost etc]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=ba23abb3-a975-41c3-9d8d-0154e7a61579
DEVICE=ens33
ONBOOT=yes
[root@localhost etc]# 

=====>less
=====>more
[root@localhost etc]# more /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes                       # 使用more命令之後,每次在螢幕上顯示一屏(一頁)的檔案內容,並且在螢幕的尾部將會出現“—More--(n%)”的資訊,其中,n%是已經顯示檔案內容的百分比。按空格下一頁,按b上一頁,more +40 a.txt從第40行開始檢視
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=ba23abb3-a975-41c3-9d8d-0154e7a61579
DEVICE=ens33
ONBOOT=yes
[root@localhost etc]# 

=====>head
[root@localhost etc]# head -3 /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE=Ethernet
PROXY_METHOD=none            # head -n 表示打印出前面三行,預設是10
BROWSER_ONLY=no
[root@localhost etc]# 
=====>tail
[root@localhost etc]# tail -3 /etc/sysconfig/network-scripts/ifcfg-ens33 
UUID=ba23abb3-a975-41c3-9d8d-0154e7a61579
DEVICE=ens33                  #tail -n 表示列印屁股後面三行,預設是10
ONBOOT=yes
[root@localhost etc]# 

[root@localhost etc]# tail -3 /etc/sysconfig/network-scripts/ifcfg-ens33 
UUID=ba23abb3-a975-41c3-9d8d-0154e7a61579
DEVICE=ens33
ONBOOT=yes                   #tail與head聯合使用
[root@localhost etc]# 

tail -f 可以用來動態檢測日誌
=====>grep
[root@localhost etc]# grep 'IPV6' /etc/sysconfig/network-scripts/ifcfg-ens33 
IPV6INIT=yes
IPV6_AUTOCONF=yes                      #過濾
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
[root@localhost etc]# 

修改檔案內容

vim 時一款編輯器

命令模式:
   在這個模式中可以執行修改,複製,移動,貼上,刪除
編輯/插入模式:插入a,i,o
   a: 進入插入模式並在游標之後進行新增
   i:進入插入模式,並在游標之前進行插入
   o:進入插入模式,並在當前(游標所在位置)開啟新的一行
   ctrl+v: 進入可視塊模式
   R: 進入替換模式
    
在vi編輯器中游標的移動
   $   游標移動到當前行的結尾
   0   游標移動到當前行的開始
在命令模式下的刪除與複製操作
   dd  刪除游標所在一行
   ndd 刪除游標所在向下n行
   yy  複製游標所在一行
   nyy 複製游標所在向下n行
貼上命令
   p         將已複製的資料在游標下一行貼上
   P(大寫)   將已複製的資料在游標上一行貼上
復原和重做命令
   U     復原前一個操作
   ctrl + r 重做上一次操作
儲存退出
   :w  儲存
   :q  退出
   :q! 強制退出
   :wq 儲存退出
游標中快速移動
   G:  游標移動到這個檔案的最後一行
  nG:  游標移動到這個檔案的第n行
  gg:  移動到這個檔案的第一行