欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第二章 命令行管理文件

程序员文章站 2022-05-29 22:04:40
...

1 Linux 一切皆文件

2 Linux目录结构:所有文件起始于 / 根目录

[[email protected] ~]# cd /  
[[email protected] /]# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
[[email protected] /]# 

[[email protected] /]# ls -l
total 32
lrwxrwxrwx.   1 root root    7 May  7  2014 bin -> usr/bin  #软链接文件(windows快捷方式)
dr-xr-xr-x.   4 root root 4096 Jul 11  2014 boot
drwxr-xr-x.  19 root root 3100 Jun 15 10:08 dev
drwxr-xr-x. 133 root root 8192 Jun 15 10:15 etc
drwxr-xr-x.   4 root root   35 Jun 15 10:15 home
lrwxrwxrwx.   1 root root    7 May  7  2014 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 May  7  2014 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 Mar 13  2014 media
drwxr-xr-x.   2 root root    6 Mar 13  2014 mnt
drwxr-xr-x.   3 root root   15 Jul 11  2014 opt
dr-xr-xr-x. 484 root root    0 Jun 15  2019 proc
dr-xr-x---.  14 root root 4096 Jun 15 10:39 root
drwxr-xr-x.  36 root root 1160 Jun 15 10:02 run
lrwxrwxrwx.   1 root root    8 May  7  2014 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 Mar 13  2014 srv
dr-xr-xr-x.  13 root root    0 Jun 15  2019 sys
drwxrwxrwt.  12 root root 4096 Jun 15 11:01 tmp
drwxr-xr-x.  13 root root 4096 May  7  2014 usr
drwxr-xr-x.  22 root root 4096 Jun 15  2019 var
[[email protected] /]# 

目录结构:

/ 根目录,所有文件起始
/etc 保存配置文件 *****
/var 保存日志,缓存文件,邮件队列等等 **
/boot 系统引导 引导文件 内核文件,启动镜像 等等 ** /boot只能是 普通分区 /dev/sda1 /dev/sda2 逻辑卷:/dev/mapper/centos-root
/home 保存普通用户的家目录 **
/dev device 设备文件保存目录 磁盘 字符文件 等等
/proc process 进程文件,内存映射文件, 内存中的, 动态 # 内存文件
/sys 将/proc内容归类存储,内存文件 # 内存文件
/mnt 空间 挂载目录
/usr/lib64 库文件 .so 驱动,动态链接库文件等等
/root root管理员家目录
/run rhel7 新的目录 保存日志 缓存等 内存文件 # 内存文件
/tmp 临时文件 每10天清除一次 ,缓存 **
/bin 普通用户可执行的命令 binary
/sbin supper binary 超级用户的管理命令
[[email protected] /]# cd /proc/
[[email protected] proc]# ls
1     14    177   219  262    37    4643  532  58    85         dma
10    140   178   22   263    3701  4649  533  59    86         driver
100   141   179   220  264    38    4653  534  60    87         execdomains
1008  142   18    221  265    39    4662  535  600   88         fb
101   143   180   222  266    40    4686  536  61    89         filesystems
102   144   181   223  267    41    4698  537  612   893        fs
103   145   182   224  268    4101  47    538  6170  895        interrupts
1033  146   183   225  269    4102  4704  539  6173  898        iomem
104   147   184   226  27     4129  4717  54   6174  899        ioports
105   148   185   227  271    4135  4718  540  618   9          irq
106   149   186   228  272    4147  4721  541  619   90         kallsyms
107   1494  187   229  273    4161  4744  542  62    900        kcore
108   1499  188   23   274    42    4766  543  620   904        keys
109   15    189   230  275    4215  4788  544  621   905        key-users
11    150   19    231  276    426   4789  545  622   907        kmsg
110   1501  190   232  277    4269  4792  546  623   908        kpagecount
111   151   191   233  278    43    48    547  624   91         kpageflags
1112  1512  192   234  279    4346  49    548  625   913        loadavg
112   1516  193   235  28     4354  490   549  63    919        locks
113   152   194   236  280    4356  491   55   64    92         mdstat
114   153   1948  237  284    4364  493   550  65    927        meminfo
115   154   195   238  285    4365  5     551  66    928        misc
116   155   196   239  286    44    50    552  67    929        modules
117   156   197   24   287    4430  500   553  68    93         mounts
118   157   198   240  288    4434  501   554  69    931        mpt
119   158   199   241  29     45    502   555  7     935        mtrr
12    159   2     242  297    4501  503   556  70    94         net
120   16    20    243  299    4518  507   557  71    946        pagetypeinfo
121   160   200   244  3      4525  508   558  72    95         partitions
122   161   201   245  30     4529  51    559  73    96         sched_debug
123   162   202   246  300    4533  516   56   738   963        scsi
124   163   203   247  301    4543  517   560  74    964        self
125   164   204   248  3067   4555  518   561  75    97         slabinfo
126   165   205   249  31     4570  519   562  759   977        softirqs
127   166   206   25   32     4575  52    563  76    98         stat
128   167   207   250  320    4577  520   564  77    99         swaps
129   168   208   251  33     4585  521   565  78    991        sys
13    169   209   252  34     4590  522   566  79    acpi       sysrq-trigger
130   17    21    253  34077  4594  523   567  798   asound     sysvipc
131   170   210   254  34095  4598  524   568  8     buddyinfo  timer_list
132   171   211   255  34136  46    525   569  80    bus        timer_stats
133   172   212   256  34137  4601  526   57   81    cgroups    tty
134   1728  213   257  34193  4606  527   570  816   cmdline    uptime
135   173   214   258  34218  4608  528   571  82    consoles   version
136   1734  215   259  34223  4615  529   572  827   cpuinfo    vmallocinfo
137   174   216   26   347    4629  53    573  83    crypto     vmstat
138   175   217   260  35     4632  530   574  831   devices    zoneinfo
139   176   218   261  36     4636  531   575  84    diskstats
[[email protected] proc]# 

查看内存状态:

[[email protected] proc]# 
[[email protected] proc]# cat /proc/meminfo 
MemTotal:        3869044 kB
MemFree:         2126324 kB
MemAvailable:    2953240 kB
Buffers:            4308 kB
Cached:           922800 kB
SwapCached:            0 kB
Active:           700236 kB
Inactive:         640800 kB
Active(anon):     416404 kB
Inactive(anon):    15768 kB
Active(file):     283832 kB
Inactive(file):   625032 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Dirty:                20 kB
Writeback:             0 kB
AnonPages:        413828 kB
Mapped:            98284 kB
Shmem:             18244 kB
Slab:             251852 kB
SReclaimable:     171456 kB
SUnreclaim:        80396 kB
KernelStack:        5824 kB
PageTables:        20344 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     1934520 kB
Committed_AS:    2047364 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      192224 kB
VmallocChunk:   34359476828 kB
HardwareCorrupted:     0 kB
AnonHugePages:    206848 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:      130880 kB
DirectMap2M:     3014656 kB
DirectMap1G:     3145728 kB
[[email protected] proc]# 

查看磁盘:

[[email protected] sda]# pwd
/sys/block/sda
[[email protected] sda]# ls
alignment_offset  device             events_poll_msecs  power      ro      stat
bdi               discard_alignment  ext_range          queue      sda1    subsystem
capability        events             holders            range      size    trace
dev               events_async       inflight           removable  slaves  uevent
[[email protected] sda]# 

实时刷新磁盘:

[[email protected] class]# cd scsi_host/
[[email protected] scsi_host]# ls
host0   host11  host14  host17  host2   host22  host25  host28  host30  host4  host7
host1   host12  host15  host18  host20  host23  host26  host29  host31  host5  host8
host10  host13  host16  host19  host21  host24  host27  host3   host32  host6  host9
[[email protected] scsi_host]# ls -l |wc -l
34
[[email protected] scsi_host]# cd host0/
[[email protected] host0]# ls
active_mode  eh_deadline  proc_name          sg_prot_tablesize  supported_mode
can_queue    host_busy    prot_capabilities  sg_tablesize       uevent
cmd_per_lun  host_reset   prot_guard_type    state              unchecked_isa_dma
device       power        scan               subsystem          unique_id

部署生产系统:

建议划分分区: LV: Logical Volume
准备磁盘: 60GB

/: 独立文件系统 逻辑卷(在线扩容) : 10GB LV

/boot: 只能是普通分区 不能是逻辑卷 : 2GB 普通分区 标准分区

/var: 逻辑卷(在线扩容) : 5GB LV

/home: 逻辑卷(在线扩容) : 10 GB LV

/tmp : 逻辑卷(在线扩容) : 10GB LV

swap: 虚拟内存 2GB LV

安装之后:

[[email protected] ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        10G  3.4G  6.7G  34% /
devtmpfs                devtmpfs  974M     0  974M   0% /dev
tmpfs                   tmpfs     991M     0  991M   0% /dev/shm
tmpfs                   tmpfs     991M   11M  980M   2% /run
tmpfs                   tmpfs     991M     0  991M   0% /sys/fs/cgroup
/dev/sda1               xfs       2.0G  166M  1.9G   9% /boot
/dev/mapper/centos-home xfs        10G   33M   10G   1% /home
/dev/mapper/centos-var  xfs       5.0G  152M  4.9G   3% /var
/dev/mapper/centos-tmp  xfs        10G   33M   10G   1% /tmp
tmpfs                   tmpfs     199M   56K  199M   1% /run/user/0
[[email protected] ~]# 

操作文件: 复制文件 移动文件 删除文件 创建目录

操作目录:

复制文件: cp :copy 默认复制单个文件,多个文件 cp file1 file2 /backup 复制目录 cp -r # -r 递归

复制/etc 到 /backup/etc

[[email protected] /]# mkdir /backup

[[email protected] /]# cp -r /etc /backup/  # 复制 默认不带权限  

[[email protected] /]# cp -r -a /etc /backup/  #保存原有文件的属性复制

移动文件: mv 保存原有文件属性

[[email protected] etc]# pwd
/backup/etc
[[email protected] etc]# mv /backup/etc/chrony.keys /tmp/
[[email protected] etc]# ll /tmp/chrony.keys 
-rw-r-----. 1 root chrony 62 Jul 11  2014 /tmp/chrony.keys
[[email protected] etc]# 

删除文件 : rm -rf #强制删除 ,不提示

[[email protected] backup]# rm /backup/etc
rm: cannot remove ‘/backup/etc’: Is a directory
[[email protected] backup]# rm -r /backup/etc
rm: descend into directory ‘/backup/etc’? y
rm: remove regular file ‘/backup/etc/fstab’? y
rm: remove regular empty file ‘/backup/etc/crypttab’? y
rm: remove symbolic link ‘/backup/etc/mtab’? 

[[email protected] backup]# rm -rf /backup/etc

查看文件: cat head tail

[[email protected] backup]# cat /etc/resolv.conf   #查看文件    
\# Generated by NetworkManager
domain example.com
search example.com
nameserver 172.25.254.254
[[email protected] backup]# 


[[email protected] backup]# 
[[email protected] backup]# tail -n 20 /var/log/messages 
Jun 15 14:43:27 localhost dhclient[1948]: DHCPACK from 172.25.0.254 (xid=0x1b184de5)
Jun 15 14:43:27 localhost NetworkManager: DHCPACK from 172.25.0.254 (xid=0x1b184de5)
Jun 15 14:43:27 localhost dhclient[1948]: bound to 172.25.0.11 -- renewal in 8795 seconds.
Jun 15 14:43:27 localhost NetworkManager[1112]: <info> (eth0): DHCPv4 state changed renew -> renew
Jun 15 14:43:27 localhost NetworkManager[1112]: <info>   address 172.25.0.1



[[email protected] backup]# 
[[email protected] backup]# head -n 5 /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

172.25.254.254 classroom.example.com
172.25.254.254 content.example.com
[[email protected] backup]# head -n 3 /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

[[email protected] backup]# 

命令别名:

[[email protected] /]# alias 
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

解除命令的别名

[[email protected] /]# unalias ll
[[email protected] /]# ll
bash: ll: command not found...

添加命令的别名

[[email protected] /]# alias ll='ls -l --color=auto'
[[email protected] /]# ll

查看系统的磁盘: * :通配符

[[email protected] Desktop]# ll /dev/sd*
brw-rw----. 1 root disk 8,  0 Jun 15  2019 /dev/sda
brw-rw----. 1 root disk 8,  1 Jun 15  2019 /dev/sda1
brw-rw----. 1 root disk 8, 16 Jun 15  2019 /dev/sdb
[[email protected] Desktop]# 

查看系统的磁盘:[] :每次取其中的一个字母

[[email protected] Desktop]# ll /dev/sd[a-z]  # A到Z 的26个字母 
brw-rw----. 1 root disk 8,  0 Jun 15  2019 /dev/sda
brw-rw----. 1 root disk 8, 16 Jun 15  2019 /dev/sdb
[[email protected] Desktop]# 

使用一条命令 创建 databases websites ftpshare

[[email protected] Desktop]# cd /tmp
[[email protected] tmp]# mkdir {databases,websites,ftpshare}
[[email protected] tmp]# ls
bigfile  chrony.keys  databases  ftpshare  websites
[[email protected] tmp]# 

使用一条命令 创建 file1 到 file100的 一百个文件

[[email protected] tmp]# touch file{1..100}

[[email protected] tmp]# ls file1?
file10  file11  file12  file13  file14  file15  file16  file17  file18  file19
[[email protected] tmp]# ls file1??
file100
[[email protected] tmp]# 
相关标签: linux