linux基本文件目錄的結構及管理

一,linux目錄結構
windows:以多根的方式組織文件 C:\ D:\ E:\
Linux:以單根的方式組織文件 / (一切目錄的起點都是 / )

/目錄結構: FHS
[root@localhost /]# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr

root root用戶的HOME

home 存儲用戶家目錄

dev 設備文件目錄 /dev/sda, /dev/tty, /dev/pts/1, /dev/zero, /dev/null, /dev/random

proc 虛擬文件系統,反映出來的是內核,進程信息或實時狀態,只存在於內存中,大小爲0

sys 僞文件系統(硬件信息,內核參數,進程在內存中的信息)類似於proc目錄

bin 普通用戶的命令存儲的目錄

sbin 管理員用戶的命令存儲的目錄

lib 庫文件,存放32位的應用程序所需要的共享庫,類似於windows裏的.dll

lib64 庫文件 ,存放64位的應用程序所需要的共享庫

usr 系統文件(應用程序目錄),相當於C:\Windows
例如: /usr/local 軟件安裝的目錄,相當於C:\Program
/usr/bin 普通用戶使用的應用程序
/usr/sbin 管理員使用的應用程序
/usr/lib 32 bit庫文件Glibc 注:Glibc是系統底層的api ,而api(英文全稱是application Programing Interface)應用程序接口
/usr/lib64 64bit庫文件Glibc
boot 存放的系統啓動相關的文件,例如kernel,grub(引導裝載程序)

etc 配置文件(系統相關的文件)
例如:/etc/sysconfig/network 網絡相關的配置文件

tmp 臨時文件(主要是程序產生的臨時數據)

var 存放的是一些變化的文件,比如數據庫,日誌,郵件.....
例如:/var/lib/mysql mysql數據庫文件
/var/vsftp ftp共享文件
/var/spool/mail 郵件的文件
/var/spool/cron 設置定時任務所存放的文件
/var/log 運行所有程序存放的的日誌文件
/var/tmp (程序產生的臨時文件)

srv 服務器數據目錄

opt 第三方軟件 (flush,splunk,openoffice)

==設備(主要指存儲設備)掛載目錄==
/run/media 移動默認的掛載的目錄
/mnt 手動掛載設備的掛載目錄

df -Thp 查看系統中的掛載項

例如:
[root@localhost ~]# df -ThP
文件系統 類型 容量 已用 可用 已用% 掛載點
/dev/mapper/centos-root xfs 18G 4.3G 14G 25% /
devtmpfs devtmpfs 470M 0 470M 0% /dev
tmpfs tmpfs 487M 0 487M 0% /dev/shm
tmpfs tmpfs 487M 7.7M 479M 2% /run
tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 xfs 497M 173M 324M 35% /boot
tmpfs tmpfs 98M 36K 98M 1% /run/user/0
/dev/sr0 iso9660 603M 603M 0 100% /run/media/root/CentOS 7 x86_64

二,命令存儲位置
/bin 普通用戶使用的命令 /bin/ls, /bin/date

/sbin 管理員使用的命令

/usr/bin 普通用戶使用的應用程序
/usr/sbin 管理員用戶使用的應用程序

庫文件存儲位置:
/lib 32位庫文件Glibc

/lib64 64位庫文件Glibc
/usr/lib 32bit應用程序庫文件 Glibc
/usr/lib64 64位應用程序庫文件 Glibc

小知識:
Linux文件有三種時間:
訪問時間:atime,查看內容
修改時間:mtime,修改內容
改變時間:ctime,文件屬性,比如權限

例如:
[root@localhost ~]# touch gg.log
[root@localhost ~]# ll gg.log 僅查看文件修改時間
-rw-r--r--. 1 root root 0 8月 28 06:17 gg.log

[root@localhost ~]# stat gg.log 查看文件的詳細屬性(其中包括文件時間屬性)
文件:"gg.log"
大小:0 塊:0 IO 塊:4096 普通空文件
設備:fd00h/64768d Inode:73215767 硬鏈接:1
權限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
環境:unconfined_u:object_r:admin_home_t:s0
最近訪問:2019-08-28 06:17:16.030170315 +0500
最近更改:2019-08-28 06:17:16.030170315 +0500
最近改動:2019-08-28 06:17:16.030170315 +0500
創建時間:-

文件類型:
通過顏色判斷文件類型不一定正確!!
Linux文件沒有擴展名

如何查看文件類型?
方法一:
[root@localhost ~]# ll anaconda-ks.cfg //看第一個字符
-rw-------. 1 root root 955 8月 19 23:36 anaconda-ks.cfg

  • 普通文件(文本文件,二進制文件,壓縮文件)
    如:
    -rw-------. 1 root root 955 8月 19 23:36 anaconda-ks.cfg

d 目錄文件 (一般目錄顯示爲藍色)
如:
drwxr-xr-x. 2 root root 6 8月 26 06:56 桌面

b 設備文件(塊設備)存儲設備硬盤,u盤 例如:/dev/sda,/dev/sda1
如:
brw-rw----. 1 root disk 8, 1 8月 28 05:19 sda1

c 字符設備文件(字符設備)打印機,終端 /dev/tty1
如:
crw--w----. 1 root tty 4, 1 8月 28 05:19 tty1

s 套接字文件
如:/dev/log
srw-rw-rw-. 1 root root 0 8月 28 05:19 log

p 管道文件

l 鏈接文件(淡藍色)

方法二:file
[root@localhost ~]# file anaconda-ks.cfg
anaconda-ks.cfg: ASCII text 文本文件,二進制文件,壓縮文件

[root@localhost ~]# file /bin/cd
/bin/cd: POSIX shell script, ASCII text executable 可執行的文件

[root@localhost ~]# file /home
/home: directory 目錄

[root@localhost ~]# file /dev/sda
/dev/sda: block special 塊設備

[root@localhost ~]# file /dev/tty1
/dev/tty1: character special 字符設備

[root@localhost ~]# file /etc/grub2.cfg
/etc/grub2.cfg: symbolic link to `../boot/grub2/grub.cfg' 符號鏈接

[root@localhost ~]# file /var/spool/postfix/public/pickup
/var/spool/postfix/public/pickup: socket 套接字

路徑(定位文件)
絕對路徑: 從 /開頭的路徑 如: /home/file1 , ~/alice
相對路徑: 相對於當前目錄開始 Desktop/file file1 . ..

. 當前目錄
.. 上一級目錄
~ 當前目錄的家目錄

useradd alice

touch /home/alice/file1

touch ~alice/file2

ls /home/alice

mkdir /home/abc

touch ../file3

touch file4 ./file5

touch /home/abc/file5

ls

ls /home/abc

三,文件管理

  1. cd 改變目錄

cd 絕對路徑 cd /home/alice cd ~alice
cd 相對路徑 cd Desktop/abc cd .. cd . cd -

cd - 返回上次目錄
cd 直接回家

  1. 創建/複製/移動/刪除/查看

1)創建
目錄 mkdir

#mkdir [option]<目錄名稱>
[root@localhost ~]# mkdir /tmp/bj
[root@localhost ~]# mkdir sh
[root@localhost ~]# mkdir /linux

選項
-p 層級創建目錄
[root@localhost ~]# mkdir -p /uplooking/linux

1)文件管理

1查看文件
#ls [option] [目錄名稱]

-l 以詳細信息的方式顯示文件信息

[root@localhost ~]# ls -l
總用量 4
-rw-------. 1 root root 955 8月 19 23:36 anaconda-ks.cfg

  -              文件
  d             目錄
   l             軟連接文件(link )
   b            塊設備(block),硬盤,硬盤分區,u盤,光盤
   c            字符設備文件(character)    鍵盤鼠標,顯示器

[root@localhost ~]# ls -l /etc/passwd
-rw-r--r--. 1 root root 2444 8月 20 17:19 /etc/passwd

-h 以人性化的方式顯示出來文件信息

[root@localhost ~]# ls -lh /etc/passwd
-rw-r--r--. 1 root root 2.4K 8月 20 17:19 /etc/passwd

-d 顯示目錄自身的信息

[root@localhost ~]# ls -ldh /etc
drwxr-xr-x. 143 root root 8.0K 8月 30 06:53 /etc

-a 顯示所有文件以及隱藏文件(以.開頭的文件)

[root@localhost ~]# ls -a
. .bash_history .bashrc .cshrc .ICEauthority .tcshrc 視頻 下載
.. .bash_logout .cache .dbus .local 公共 圖片 音樂
anaconda-ks.cfg .bash_profile .config .esd_auth .pki 模板 文檔 桌面

-t 按文件修改時間降序排列

[root@localhost ~]# ls -lt
總用量 4
-rw-r--r--. 1 root root 0 8月 30 07:21 1.txt
drwxr-xr-x. 2 root root 6 8月 30 06:55 公共
drwxr-xr-x. 2 root root 6 8月 30 06:55 視頻
drwxr-xr-x. 2 root root 6 8月 30 06:55 圖片
drwxr-xr-x. 2 root root 6 8月 30 06:55 文檔
drwxr-xr-x. 2 root root 6 8月 30 06:55 音樂
drwxr-xr-x. 2 root root 6 8月 30 06:55 模板
drwxr-xr-x. 2 root root 6 8月 30 06:55 下載
drwxr-xr-x. 2 root root 6 8月 30 06:55 桌面
-rw-------. 1 root root 955 8月 19 23:36 anaconda-ks.cfg

-S 按文件大小降序排列

[root@localhost ~]# ls -lhS
總用量 4.0K
-rw-------. 1 root root 955 8月 19 23:36 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 8月 30 06:55 公共
drwxr-xr-x. 2 root root 6 8月 30 06:55 模板
drwxr-xr-x. 2 root root 6 8月 30 06:55 視頻
drwxr-xr-x. 2 root root 6 8月 30 06:55 圖片
drwxr-xr-x. 2 root root 6 8月 30 06:55 文檔
drwxr-xr-x. 2 root root 6 8月 30 06:55 下載
drwxr-xr-x. 2 root root 6 8月 30 06:55 音樂
drwxr-xr-x. 2 root root 6 8月 30 06:55 桌面
-rw-r--r--. 1 root root 0 8月 30 07:21 1.txt

2)統計目錄大小
[root@localhost ~]# du -sh /etc/passwd
4.0K /etc/passwd

-a 顯示目錄中個別文件的大小
[root@localhost ~]# du -ah /etc/

3)創建文件

#touch <文件名稱>

花括號展{}開:
[root@localhost ~]# touch /tmp/1.txt

[root@localhost ~]# touch /tmp/{1,3,9}.jpg 只創建 1,3,9的jpg格式的文件

[root@localhost ~]# touch /tmp/{1..10}.mp3 創建1到10的.mp3的文件

[root@localhost ~]# touch /tmp/{a,b,c}.txt

[root@localhost ~]# date
2019年 08月 30日 星期五 07:59:13 PKT

[root@localhost ~]# date +%F
2019-08-30

[root@localhost ~]# date +%T
08:02:33

[root@localhost ~]# date +%H 只顯示小時
08

[root@localhost ~]# date +%M 只顯示分鐘
05

[root@localhost ~]# date +%m 只顯示秒
08

[root@localhost ~]# date +%F-%T 具體年月日-具體時間
2019-08-30-08:08:31

命令引用:

方法1:$(command)

[root@localhost ~]# touch /tmp/$(date +%F-%T).log
[root@localhost ~]# ls /tmp
2019-08-30-08:13:42.log

方法2:反引號

[root@localhost ~]# touch /tmp/date +%F-%T.log
[root@localhost ~]# ls /tmp
2019-08-30-08:23:08.log

4)複製文件目錄

#cp [option] 源文件 目標文件

選項:

-r 複製目錄時

[root@localhost ~]# cp -r /linux/ /windows/

-f 強制覆蓋

[root@localhost ~]# cp -fn /linux/1.txt /windows/

5)移動文件目錄

#mv 源文件 目標文件

[root@localhost ~]# mv /linux/1.txt /linux/66.txt

6)刪除文件目錄

#rm [option] 文件名稱

[root@localhost ~]# rm /linux/66.txt
rm:是否刪除普通空文件 "/linux/66.txt"?

-r 刪除目錄時

[root@localhost ~]# rm -r /linux/66.txt
rm:是否刪除普通空文件 "/linux/66.txt"?

-f 強制刪除目錄
[root@localhost ~]# rm -rf /linux/66.txt

7)統計文件的行數

[root@localhost ~]# wc -l /etc/passwd
46 /etc/passwd

[root@localhost ~]# wc /etc/passwd
46 96 2444 /etc/passwd 查看文件行數,字數,字節數

8)查看文件內容 ------cat/more/less/head/tail

  1. cat

[root@localhost ~]# cat /etc/fstab

#

/etc/fstab

Created by anaconda on Tue Aug 20 02:30:01 2019

#

Accessible filesystems, by reference, are maintained under '/dev/disk'

See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=72251e67-7f60-4598-99cd-67f415c4496d /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0

[root@localhost ~]# cat -n /etc/fstab 顯示所有航
1
2 #
3 # /etc/fstab
4 # Created by anaconda on Tue Aug 20 02:30:01 2019
5 #
6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 /dev/mapper/centos-root / xfs defaults 0 0
10 UUID=72251e67-7f60-4598-99cd-67f415c4496d /boot xfs defaults 0 0
11 /dev/mapper/centos-swap swap swap defaults 0 0

[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)

  1. more/less分頁內容

[root@localhost ~]# more /etc/fstab 用b翻頁

[root@localhost ~]# less /etc/fstab 能用上下鍵翻頁

head/tail

head -n 具體要顯示幾行 從頭開始第幾行

[root@localhost ~]# head -n 3 /etc/passwd 顯示前3行
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

[root@localhost ~]# head /etc/passwd 默認顯示前10行
root:x:0:0:root:/root:/bin/bash
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
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost ~]# tail -n 3 /etc/passwd 後3行
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
roo:x:1000:1000:root:/home/roo:/bin/bash

[root@localhost ~]# tail /etc/passwd 默認提示後10行
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
saned:x:988:982:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
sssd:x:987:981:User for sssd:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:986:980::/run/gnome-initial-setup/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
roo:x:1000:1000:root:/home/roo:/bin/bash

9)查看文件類型
[root@localhost ~]# file /etc/passwd
/etc/passwd: ASCII text

[root@localhost ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls

[root@localhost ~]# file /usr/bin/ls
/usr/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

管道符的使用: |

[root@localhost ~]# ls -hlS /etc/ |head -n 5
總用量 1.5M
-rw-r--r--. 1 root root 655K 6月 7 2013 services
-rw-r--r--. 1 root root 79K 8月 20 23:52 ld.so.cache
-rw-r--r--. 1 root root 25K 8月 6 2015 dnsmasq.conf
-rw-r--r--. 1 root root 22K 4月 11 2018 brltty.conf

[root@localhost ~]# ls -lt /etc/ |head -n 11
總用量 1496
drwxr-xr-x. 5 root lp 4096 8月 30 12:46 cups
-rw-r--r--. 1 root root 73 8月 30 06:53 resolv.conf
drwxr-xr-x. 6 root root 4096 8月 30 06:53 sysconfig
drwx------. 5 root root 4096 8月 21 00:12 libvirt
-rw-r--r--. 1 root root 80791 8月 20 23:52 ld.so.cache
drwxr-xr-x. 2 root root 4096 8月 20 23:52 alternatives
drwxr-xr-x. 2 root root 4096 8月 20 23:52 bash_completion.d
drwxr-xr-x. 2 root root 54 8月 20 23:52 cron.daily
drwxr-xr-x. 2 root root 21 8月 20 23:52 cron.hourly
drwxr-xr-x. 6 root root 95 8月 20 23:52 yum

10)查找文件目錄

find 路徑 查找方式

按文件名查找

[root@localhost ~]# find /etc/ -name "*.conf"

[root@localhost ~]# find /etc/ -name "*.conf" |wc -l
452

按文件大小查找

[root@localhost ~]# find /etc/ -size +500k

[root@localhost ~]# find /etc/ -size +1M

按文件修改時間查找
[root@localhost ~]# find /etc -mtime +5 修改時間超過5天的

[root@localhost ~]# find /etc -mtime 5 修改時間等於5天的

[root@localhost ~]# find /etc/ -mtime -5 修改時間爲5天內的

文件類型查找

[root@localhost ~]# find /dev/ -type b 查找塊設備類型的
/dev/dm-1
/dev/dm-0
/dev/sr0
/dev/sda2
/dev/sda1
/dev/sda

[root@localhost ~]# find /dev/ -type l 查找鏈接類型的文件

擴展知識:

dos2unix 將windows格式的文件轉換成unix

unix2dos 將unix格式的文件轉換成windows

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章