第一周总结

 ##### 存储网络:
“`
DAS—–直接连接存储(Direct attached storage)
NAS—–网络连接存储(Network attached storage)
SAN—–存储区域网络(storage area network)
“`
“`
DAS优缺点:
直接存储(Direct Attached Storage)。存储设备与主机的紧密相连
1.管理成本较低,实施简单
2.储时直接依附在服务器上,因此存储共享受到限制
3.CPU必须同时完成磁盘存取和应用运行的双重任务,所以不利于CPU的
指令周期的优化,增加系统负担
“`
“`
NAS优缺点:
网络连接存储(Network Attached Storage):
通过局域网在多个文件服务器之间实现了互联,基于文件的协议( NFS、
SMB/CIFS ),实现文件共享
1.集中管理数据,从而释放带宽、提高性能
2.可提供跨平台文件共享功能
3.可靠性较差,适用于局域网或较小的网络
“`
“`
SAN优缺点:
存储区域网络(Storage Area Networks,SAN)
利用高速的光纤网络链接服务器与存储设备,基于SCSI,IP,ATM等多种高
级协议,实现存储共享
1.服务器跟储存装置两者各司其职
2.利用光纤信道来传输数据﹐以达到一个服务器与储存装置之间多对多
的高效能、高稳定度的存储环境
3.实施复杂,管理成本高
“`
##### 操作系统六大功能:
“`
硬件驱动
进程管理
网络管理
安全管理
文件管理
内存管理
“`
##### 命令lsblk(查看块设备情况)
例:
“`
[root@centos7 share]#lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0      2:0    1    4K  0 disk
sda      8:0    0  200G  0 disk
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0   50G  0 part /
├─sda3   8:3    0   30G  0 part /data
├─sda4   8:4    0    1K  0 part
└─sda5   8:5    0    2G  0 part [SWAP]
sr0     11:0    1  8.1G  0 rom  /run/media/root/CentOS 7 x86_64
“`
##### 开发接口标准:
“`
程序员写好的程序在a操作系统可以用,能否在b上应用
编程接口兼容:
posix:portable operating system  可移植的操作系统
任何遵循POSIX标准的api接口,其源代码在a系统上可用,在b上也可用,在abi运行接口上则不兼容,一旦被编译成二进制,就变成对应机器独特的二进制格式
API:application program interface
程序员面对的编程接口    定义了源代码与库之间的接口
ABI:application binary interface
程序应用者面对的运行程序接口    描述了应用程序与os之间的底层接口
“`
#####  自动登录:
“`
修改配置文件vi /etc/gdm/custom.conf
[daemon]
AutomaticLoginEnable=true        ###开启自动登录
AutomaticLogin=root              ###以root身份运行
“`
##### 命令id (查看用户ID号)
“`
-u 只显示id号
例:
[root@centos7 share]#id -u
0
“`
##### 命令tty(查看当前终端)
“`
例:
[root@centos6 ~]# tty
/dev/pts/0
“`
##### 命令who(查看当前所有登录用户,并查看其终端)
“`
例:
[root@centos6 ~]# who
root     tty1         2018-03-28 19:02 (:0)            ###本机登录
root     pts/0        2018-03-28 19:06 (192.168.30.1) ###表示通过远程登录
“`
##### 命令whoami(显示当前用户身份)
“`
例:
[root@centos6 ~]# whoami
root
“`
##### 命令who am i(显示身份,登录终端等信息)
“`
例:
[root@centos6 ~]# who am i
root     pts/0        2018-03-28 19:06 (192.168.30.1)
“`
##### 命令w(查看谁在登录,并查看其正在做什么)
“`
例:
[root@centos7 share]#w
 00:51:42 up  8:39,  4 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     :0       :0               16:12   ?xdm?  10:21   0.69s /usr/libexec/gnome-sess
root     pts/0    :0               18:47    6:03m  0.30s  0.30s bash
root     pts/1    192.168.30.1     22:23    6.00s  1.45s  0.15s w
root     tty2                      18:48    5:58m  0.19s  0.19s -bash
“`
##### 命令init
“`
例:
init 3 关掉图形界面进入字符界面
init 0 关机
init 5 切换到图形界面
备注:init是模式切换,startx 只是开启图形工具,不改变运行等级
“`
##### 命令runlevel(查看运行等级)
“`
例:
[root@centos7 share]#runlevel
N 5
“`
##### 命令chvt
“`
例:
chvt 2  切换到虚拟终端2
“`
##### shell
“`
cat /etc/shells查看当前系统所有shell类型
“`
##### 命令>(创建新文件,清空文件)
“`
例:
> file     ###若文件不存在则创建,若存在则清空文件
“`
##### 命令提示符
“`
相关变量PS1
修改提示符格式,man bash可查看PS1详细用法
PS1=”\[\e[1;5;41;33m\][\u@\h \W]\\$\[\e[0m\]”
\e \033 \u 当前用户
\h 主机名简称 \H 主机名
\w 当前工作目录 \W 当前工作目录基名
\t 24小时时间格式 \T 12小时时间格式
使其永久生效,写入配置文件/etc/profile.d/env.sh,文件名必须是.sh结尾
. 或者source重新加载配置文件
“`
##### 命令help
“`
help 列出所有内置命令
help cd 查看内置命令帮助
ls –help 查看外部命令帮助
“`
##### 命令type(查看命令是否外置内置)
“`
例:
root@centos7 share]#type cd
cd is a shell builtin
“`
##### 命令enable(禁用内部命令)
“`
-n 禁用内部命令
例:
enable -n cd
enable cd       ###启用命令
“`
##### 外部命令查找方式
“`
外部命令按变量$PATH查找
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
例:
cp /usr/bin/yes /usr/local/sbin/yes
执行命令yes
执行的结果用type查看
[root@centos6 ~]#type yes
yes is hashed (/usr/local/sbin/yes)
但要注意清除hash缓存
“`
##### 命令hash
“`
hash显示缓存
hash –l 显示缓存可以作为输入使用
hash –p name 将命令全路径path改名为name
hash –t name 打印缓存中name的路径
hash –d name清除name缓存
hash –r 清除缓存
“`
##### 命令which(查看外部命令对应的路径)
“`
按照$PATH逐个查找。假如在第一个目录中找到了,则不再继续往下查找,要想列出所有执行which –a name。
which –skip-alias ls   不显示别名,只显示路径
例:
[root@centos6 ~]#which –skip-alias ls
/bin/ls
“`
##### 命令whereis(显示路径更为全面,包括一些文档路径)
“`
例:
[root@centos7 share]#whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
“`
##### 命令alias(定义命令别名)
“`
要想一直生效,写入配置文件.bashrc(只针对当前用户有效,对所有用户有效,则写入配置文件/etc/bashrc)
“`
##### 命令unalias(取消别名)
“`
例:
unalias -a    ###取消所有别名
“`
##### 内部命令和别名的优先级
“`
例:alias enable=who
执行命令enable
[root@centos6 ~]#enable
root     tty1         2018-03-29 07:46 (:0)
root     pts/0        2018-03-29 07:53 (:0.0)
root     pts/1        2018-03-29 07:54 (192.168.30.1)
说明别名命令优先级更高
“`
##### 只使用原始命令而不使用别名的方法
“`
‘command’
\command
命令的绝对路径
“`
##### 一条命令分多行输入执行
“`
例:
[root@centos6 ~]#hos\
> t\
> name
centos6
“`
##### 命令date
“`
-d  –date   显示指定日期
例:
[root@centos7 share]#date -d “-2 day” +%F
2018-03-28                         ####显示前两天日期
-s
例:[root@centos7 share]#date -s “-1 day”
Thu Mar 29 01:40:26 CST 2018       ###设置成昨天的时间
练习:
显示前天是星期几
date -d “-2 day” +%A
“`
##### 命令hwclock,clock
“`
-s –hctosys 以硬件时间为准
-w –systohc以系统时间为准
“`
##### 命令timedatectl
“`
timedatectl list-timezones                列出所有时区
timedatectl status                        查看当前时区
timedatectl set-timezone “Asia/Hongkong”  设置时区
时区相关文件/etc/localtime  或者/usr/share/zoneinfo
“`
##### 命令cal(查看日历)
“`
-y
例:
[root@centos7 share]#cal 3 2018
     March 2018
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
“`
##### 命令shutdown
“`
-r reboot
-h halt
-c cancel
TIME:无指定,默认相当于 +1
now: 立刻,相当于+0
+m: 相对时间表示法,几分钟之后;例如 +3
hh:mm: 绝对时间表示,指明具体时间
“`
##### 命令wall
“`
例:
wall “hello”
“`
##### 命令screen
“`
创建新screen会话
screen –S [SESSION]
加入screen会话
screen –x [SESSION]
退出并关闭screen会话
exit
剥离当前screen会话
Ctrl+a,d
显示所有已经打开的screen会话
screen -ls
恢复某screen会话
screen -r [SESSION]
“`
##### 利用screen实现远程协助
“`
1.在本机创建会话screen -S help
2.在远程登录需要帮助的主机,查看会话screen -ls ,然后加入会话screen -x实现同步操作
“`
##### 利用screen备份数据,防止备份中断
“`
1.进入会话(执行screen),然后开始备份操作
2.假若备份中断,进入会话(执行screen),然后执行screen -r进行恢复
“`
##### 命令echo
“`
功能:显示字符
语法:echo [-neE][字符串]
说明:echo会将输入的字符串送往标准输出。输出的字符串间以空白字符隔开
并在最后加上换行号
选项:
 -E (默认)不支持 \ 解释功能
 -n 不自动换行
 -e 启用 \ 字符的解释功能
显示变量
echo “$VAR_NAME“  变量会替换,弱引用
echo ‘$VAR_NAME‘   变量不会替换,强引用
启用命令选项-e,若字符串中出现以下字符,则特别加以处理,而不会将它当成
一般文字输出
\a 发出警告声
\b 退格键
\c 最后不加上换行符号
\n 换行且光标移至行首
\r 回车,即光标移至行首,但不换行
\t 插入tab
\\ 插入\字符
\0nnn 插入nnn(八进制)所代表的ASCII字符
echo -e ‘\033[43;31;5mmagedu\033[0m’
\xHH插入HH(十六进制)所代表的ASCII字
“`
“`
例:
[root@centos6 data]#echo {10..30..2}
10 12 14 16 18 20 22 24 26 28 30        ####按2递增
例:[root@centos6 data]#echo {a..Z}
a ` _ ^ ]  [ Z                          ###结果按照ASCII码顺序显示
例:[root@centos6 data]#echo file{a,b,c}.{log,txt}
filea.log filea.txt fileb.log fileb.txt filec.log filec.txt
“`
##### 登录前提示
“`
配置文件/etc/motd
编辑内容,登录后即可显示登录提示
“`
##### 登录后提示信息
“`
/etc/issue
“`
“`
“`
##### 一个命令调用另一个命令的输出
“`
例:
cp `which –skip-alias ls` /tmp
“`
##### tab键
“`
~2tab  列出所有用户
$2tab  列出所有变量
“`
##### 命令history
“`
echo $HISTSIZE    默认记录1000条,编辑/etc/profile可修改HISTSIZE
命令行历史
保存你输入的命令历史。可以用它来重复执行命令
登录shell时,会读取命令历史文件中记录下的命令~/.bash_history
-c清空历史,但不会清除.bash_history 中的历史
-d 清空指定的历史条目
-a 把内存中的历史写入磁盘中
-r 把磁盘中的历史追加到内存
-w: 保存历史列表到指定的历史文件
-n: 读历史文件中未读过的行到历史列表
-p:不记录历史                  例:history -p `ls`
-s:构造历史,但命令实际不执行   例:history -s `rm -rf *`
“`
“`
登录进shell后新执行的命令只会记录在缓存中;这些命令会用户退出时“追
加”至命令历史文件中
重复前一个命令,有4种方法
 重复前一个命令使用上方向键,并回车执行
 按 !! 并回车执行
 输入 !-1 并回车执行
 按 Ctrl+p 并回车执行
!:0  执行前一条命令(去除参数)
Ctrl + n 显示当前历史中的下一条命令,但不执行
Ctrl + j 执行当前命令
!n 执行history命令输出对应序号n的命令
!-n 执行history历史中倒数第n个命令
!string 重复前一个以“string”开头的命令
!?string 重复前一个包含string的命令
!string:p 仅打印命令历史,而不执行
!$:p 打印输出 !$ (上一条命令的最后一个参数)的内容
!*:p 打印输出 !*(上一条命令的所有参数)的内容
^string 删除上一条命令中的第一个string
^string1^string2 将上一条命令中的第一个string1替换为string2
!:gs/string1/string2 将上一条命令中所有的string1都替换为 string2
使用up(向上)和down(向下)键来上下浏览从前输入的命令
ctrl-r来在命令历史中搜索命令
(reverse-i-search)`’:
Ctrl+g:从历史搜索模式退出
要重新调用前一个命令中最后一个参数
!$  表示
Esc, .(点击Esc键后松开,然后点击 . 键)
Alt+ .(按住Alt键的同时点击 . 键)
调用历史参数:
command !string:^ 从命令历史中搜索以 string 开头的命令,并获取它的第一
个参数
command !string:$ 从命令历史中搜索以 string 开头的命令,并获取它的最后一
个参数
command !string:n 从命令历史中搜索以 string 开头的命令,并获取它的第n
个参数
command !string:* 从命令历史中搜索以 string 开头的命令,并获取它的所有
参数
“`
##### 命令whatis=man -f
“`
例:
[root@centos6 ~]#whatis ls
ls                   (1)  – list directory contents
ls                   (1p)  – list directory contents
centos6制作数据库  makewhatis
centos7制作数据库  mandb
“`
##### man帮助
“`
man -k password  表示查看跟password相关的章节
“`
##### 命令历史相关环境变量
“`
HISTSIZE:命令历史记录的条数
HISTFILE:指定历史文件,默认为~/.bash_history
HISTFILESIZE:命令历史文件记录历史的条数
HISTTIMEFORMAT=”%F %T” 显示时间
HISTIGNORE=“str1:str2*:… “ 忽略str1命令,str2开头的历史
控制命令历史的记录方式:
环境变量:HISTCONTROL
ignoredups 默认,忽略重复的命令,连续且相同为“重复”
ignorespace 忽略所有以空白开头的命令
ignoreboth 相当于ignoredups, ignorespace的组合
erasedups 删除重复命令
export 变量名=”值“
存放在 /etc/profile 或 ~/.bash_profile    最好存放在自己创建的文件中/etc/profile.d/env.sh
“`
##### 命令ldd(查看程序库调用)
“`
例:[root@centos7 share]#ldd /bin/ls
linux-vdso.so.1 =>  (0x00007ffdcf3ee000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f2f9eeb0000)
libcap.so.2 => /lib64/libcap.so.2 (0x00007f2f9ecab000)
libacl.so.1 => /lib64/libacl.so.1 (0x00007f2f9eaa1000)
libc.so.6 => /lib64/libc.so.6 (0x00007f2f9e6de000)
libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f2f9e47c000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f2f9e277000)
/lib64/ld-linux-x86-64.so.2 (0x000055b2e31e1000)
libattr.so.1 => /lib64/libattr.so.1 (0x00007f2f9e072000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f2f9de56000)
“`
##### centos6自动挂在光盘
“`
[root@centos6 ~]#cd /misc
[root@centos6 misc]#cd cd/
[root@centos6 cd]#df
“`
##### 识别新增硬盘
“`
echo ‘- – -‘ >/sys/class/scsi_host/host2/scan
“`
##### centos7目录的变化
“`
/bin和/usr/bin
/sbin和/usr/sbin
/lib和/usr/lib
/lib64和/usr/lib64
以上目录都是以软链接的形式,而在centos6则表示不同的目录
“`
##### 字符设备与块设备
“`
字符设备是顺序访问,块设备是随机访问,块设备有缓存,而字符设备没有,字符设备如:/dev/zero.
[root@centos7 18:58:03 ~]#ll /dev/zero /dev/sda
brw-rw—-. 1 root disk 8, 0 Mar 30 16:03 /dev/sda
crw-rw-rw-. 1 root root 1, 5 Mar 30 16:03 /dev/zero
“`
##### 定义各类文件或目录的颜色
“`
编辑配置文件vi /etc/DIR_COLORS
“`
##### 命令ls
“`
选项:
-R 目录递归通过
-ld目录和符号链接信息
-1文件分行显示
-S按大到小排序
-t按Mtime排序
-u配合-t选项,显示并按atime从新到旧排序
-U按目录存放顺序显示(即文件创建的顺序)
-X按文件后缀排序
-r倒序排列
例:-t
root@centos7 09:47:44 data]#vi file2
[root@centos7 09:49:31 data]#ls -t
file2   file41  file46  file18  file24  file29  file34  file10  file15  file9  file6
例:[root@centos7 19:36:48 data]#ls -1
file1
file10
file2
file3
file4
file5
file6
file7
例:按文件大小倒序排列
[root@centos7 19:41:08 /]#ls -Slr
total 20
dr-xr-xr-x.  13 root root    0 Mar 30 16:02 sys
dr-xr-xr-x. 198 root root    0 Mar 30 16:02 proc
-rw-r–r–.   1 root root    0 Mar 30 04:40 a
drwxr-xr-x.   2 root root    6 Nov  5  2016 srv
drwxr-xr-x.   2 root root    6 Nov  5  2016 mnt
drwxr-xr-x.   2 root root    6 Nov  5  2016 media
lrwxrwxrwx.   1 root root    7 Mar 28 02:04 lib -> usr/lib
lrwxrwxrwx.   1 root root    7 Mar 28 02:04 bin -> usr/bin
lrwxrwxrwx.   1 root root    8 Mar 28 02:04 sbin -> usr/sbin
lrwxrwxrwx.   1 root root    9 Mar 28 02:04 lib64 -> usr/lib64
例:按ctime排序
[root@centos7 19:53:58 data]#chown hehe. file3
[root@centos7 19:54:11 data]#chown hehe. file5
[root@centos7 19:54:16 data]#chown hehe. file6
[root@centos7 19:54:20 data]#ls -clt
total 0
-rw-r–r–. 1 hehe hehe  0 Mar 30 19:54 file6
-rw-r–r–. 1 hehe hehe  0 Mar 30 19:54 file5
-rw-r–r–. 1 hehe hehe  0 Mar 30 19:54 file3
-rw-r–r–. 1 root root  0 Mar 30 19:36 file1
-rw-r–r–. 1 root root  0 Mar 30 19:36 file10
-rw-r–r–. 1 root root  0 Mar 30 19:36 file2
-rw-r–r–. 1 root root  0 Mar 30 19:36 file4
例:查看链接文件/bin加“/”与不加的区别
[root@centos7 09:38:24 data]#ll /bin/
-rwxr-xr-x. 1 root root       1118 Dec 29  2013 zenheiset
-rwxr-xr-x. 1 root root     103608 Aug  2  2017 zenity
-rwxr-xr-x. 1 root root        123 Aug  2  2017 zfgrep
-rwxr-xr-x. 1 root root       2144 Aug  2  2017 zforce
-rwxr-xr-x. 1 root root       5916 Aug  2  2017 zgrep
-rwxr-xr-x. 1 root root     215840 Nov  6  2016 zip
-rwxr-xr-x. 1 root root     100456 Nov  6  2016 zipcloak
-rwxr-xr-x. 1 root root      48451 Jun 10  2014 zipdetails
-rwxr-xr-x. 1 root root       2953 Oct 11  2008 zipgrep
-rwxr-xr-x. 2 root root     185488 Nov  6  2016 zipinfo
-rwxr-xr-x. 1 root root      95984 Nov  6  2016 zipnote
-rwxr-xr-x. 1 root root     100096 Nov  6  2016 zipsplit
-rwxr-xr-x. 1 root root       2041 Aug  2  2017 zless
-rwxr-xr-x. 1 root root       2859 Aug  2  2017 zmore
-rwxr-xr-x. 1 root root       5343 Aug  2  2017 znew
lrwxrwxrwx. 1 root root          6 Mar 28 02:05 zsoelim -> soelim
[root@centos7 09:38:24 data]#ll /bin
lrwxrwxrwx. 1 root root 7 Mar 28 02:04 /bin -> usr/bin
例:
[root@centos6 data]#ll -d /etc      只显示目录本身
drwxr-xr-x. 118 root root 12288 Apr  1 09:34 /etc
[root@centos6 data]#ll -d /etc/*     显示目录下文件和目录
drwxr-xr-x.  3 root root   4096 Mar 27 17:19 /etc/abrt
drwxr-xr-x.  4 root root   4096 Mar 27 17:29 /etc/acpi
-rw-r–r–.  1 root root     47 Mar 31 21:05 /etc/adjtime
-rw-r–r–.  1 root root   1512 Jan 12  2010 /etc/aliases
[root@centos6 data]#ll -d /etc/*/    显示目录下目录
drwxr-xr-x.  3 root root 4096 Mar 27 17:19 /etc/abrt/
drwxr-xr-x.  4 root root 4096 Mar 27 17:29 /etc/acpi/
drwxr-xr-x.  2 root root 4096 Mar 27 17:26 /etc/alsa/
drwxr-xr-x.  2 root root 4096 Mar 27 17:30 /etc/alternatives/
“`
##### 命令shutdown
“`
选项:
-r
centos6执行shutdown -r 后面要加时间
centos7则可以直接执行
“`
##### 文件通配符
“`
匹配零个或多个字符
? 匹配任何单个字符
~ 当前用户家目录
~mage 用户mage家目录
~+ 当前工作目录
~- 前一个工作目录
[0-9] 匹配数字范围
[a-z]:字母
[A-Z]:字母
[wang] 匹配列表中的任何的一个字符
[^wang] 匹配列表中的所有字符以外的字符
例:通配符?
[root@centos7 20:01:30 data]#ls ?
你                                 ###汉字也算一个字符
“`
“`
例:通配符[]
[root@centos7 20:29:40 data]#touch file{a..z} file{A..Z}
[root@centos7 20:30:16 data]#ls file[a-d]
filea  fileA  fileb  fileB  filec  fileC  filed
###字母排序以小大小大排序
“`
##### 文件通配符
“`
预定义的字符类:man 7 glob
[:digit:]:任意数字,相当于0-9
[:lower:]:任意小写字母
[:upper:]: 任意大写字母
[:alpha:]: 任意大小写字母
[:alnum:]:任意数字或字母
[:blank:]:水平空白字符
[:space:]:水平或垂直空白字符
[:punct:]:标点符号
[:print:]:可打印字符
[:cntrl:]:控制(非打印)字符
[:graph:]:图形字符
[:xdigit:]:十六进制字符
“`
“`
例:只显示隐藏文件,等价于l.
[root@centos7 20:39:01 ~]#ls -d .*
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  .cache  .config  .cshrc  .dbus  .esd_auth  .ICEauthority  .lesshst  .local  .serverauth.3629  .tcshrc  .Xauthority
“`
“`
例:显示该目录及子目录下隐藏文件
[root@centos7 20:45:36 ~]#ls -R .[^.]*
.bash_history  .bash_logout  .bash_profile  .bashrc  .cshrc  .esd_auth  .ICEauthority  .lesshst  .serverauth.3629  .tcshrc  .Xauthority
.cache:
abrt   event-sound-cache.tdb.centos7.x86_64-redhat-linux-gnu  gnome-control-center  gnome-software  imsettings  thumbnails  yelp
dconf  evolution                                              gnome-shell           gstreamer-1.0   media-art   tracker
.cache/abrt:
applet_dirlist  lastnotification
.cache/dconf:
user
.cache/evolution:
addressbook  calendar  mail  memos  sources  tasks
注:目录中含..这个目录,表示上级目录,因此第二个为字符为非.即[^.]
“`
“`
例:
[root@centos7 20:54:28 ~]#ls -ad .[^.]*
.aa  .bash_history  .bash_logout  .bash_profile  .bashrc  .cache  .config  .cshrc  .dbus  .esd_auth  .ICEauthority  .lesshst  .local  .serverauth.3629  .tcshrc  .Xauthority
“`
##### 命令touch
“`
touch [OPTION]… FILE…
-a 仅改变 atime和ctime
-m 仅改变 mtime和ctime
-t  [[CC]YY]MMDDhhmm[.ss]
指定atime和mtime的时间戳
-c 如果文件不存在,则不予创建
注:touch创建新文件时,若文件名已存在,不会发生覆盖,但改变时间戳,包括atime,ctime,mtime
[root@centos6 data]#touch -a file2
[root@centos6 data]#stat file2
  File: `file2′
  Size: 0         Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d Inode: 12          Links: 1
Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-03-31 15:16:18.576995276 +0800
Modify: 2018-03-31 15:12:56.272000245 +0800
Change: 2018-03-31 15:16:18.576995276 +0800
[root@centos6 data]#touch -m file2
[root@centos6 data]#stat file2
  File: `file2′
  Size: 0         Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d Inode: 12          Links: 1
Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-03-31 15:16:18.576995276 +0800
Modify: 2018-03-31 15:17:18.587996113 +0800
Change: 2018-03-31 15:17:18.587996113 +0800
“`
##### 命令cp
“`
常用选项:
-i:覆盖前提示 –n:不覆盖,注意两者顺序
-r, -R: 递归复制目录及内部的所有内容
-a: 归档,相当于-dR –preserv=all
-d:–no-dereference –preserv=links 不复制原文件,只复制链接名
–preserv[=ATTR_LIST]
mode: 权限
ownership: 属主属组
timestamp:
links
xattr
context
all
-p: 等同–preserv=mode,ownership,timestamp
-v: –verbose
-f: –force
-u:–update 只复制源比目标更新文件或目标不存在的文件
–backup=numbered 目标存在,覆盖前先备份加数字后缀
“`
“`
-f强制覆盖(先删除原文件,再创建新文件,所有者所属主均发生变化)
“`
“`
[hehe@centos7 21:25:16 ~]$ll
total 4
-rw-r–r–. 1 root root 23 Mar 30 21:24 issue
[hehe@centos7 21:25:23 ~]$cp -f /etc/motd issue
[hehe@centos7 21:25:51 ~]$ll
total 4
-rw-r–r–. 1 hehe hehe 7 Mar 30 21:25 issue
“`
“`
-d保留软链接
例:
[root@centos7 21:34:36 data]#ll /etc/grub2.cfg
lrwxrwxrwx. 1 root root 22 Mar 28 02:10 /etc/grub2.cfg -> ../boot/grub2/grub.cfg
[root@centos7 21:35:09 data]#cp -d /etc/grub2.cfg /data
[root@centos7 21:35:43 data]#ll
total 0
lrwxrwxrwx. 1 root root 22 Mar 30 21:35 grub2.cfg -> ../boot/grub2/grub.cfg
“`
“`
-a存档,保留所有属性,常用于备份,可和-v(显示过程)一起使用,但容易造成终端死机
–backup=numbered
例:
[root@centos7 21:47:39 ~]#cp –backup=numbered f1 /data/f1
cp: overwrite ‘/data/f1’? y
[root@centos7 21:47:55 data]#ls
f1  f1.~1~  f1.~2~
可定义成别名,,防止误覆盖
“`
“`
复制特殊文件如:/dev/sda,r如果不加-a,会复制里面的数据,而不是文件名称
cp -a /dev/sda ./
“`
“`
练习
1、定义别名命令baketc,每天将/etc/目录下所有文件,备份到
/data独立的子目录下,并要求子目录格式为 backupYYYY-mm-dd,
备份过程可见
练习
[root@centos7 22:00:09 data]#cp -av /etc /data/backup`date +%F`
[root@centos7 22:00:04 data]#ls
backup2018-03-30
[root@centos7 15:48:45 data]#alias backetc=’cp -av /etc /data/backup`date +%F`’
“`
“`
##### 命令rename
例:
[root@centos7 22:09:13 data]#touch {1..30}.conf
[root@centos7 22:09:28 data]#ls
10.conf  13.conf  16.conf  19.conf  21.conf  24.conf  27.conf  2.conf   4.conf  7.conf
11.conf  14.conf  17.conf  1.conf   22.conf  25.conf  28.conf  30.conf  5.conf  8.conf
12.conf  15.conf  18.conf  20.conf  23.conf  26.conf  29.conf  3.conf   6.conf  9.conf
[root@centos7 22:09:28 data]#rename “conf” “conf.bak” *.conf
[root@centos7 22:11:21 data]#ls
10.conf.bak  14.conf.bak  18.conf.bak  21.conf.bak  25.conf.bak  29.conf.bak  4.conf.bak  8.conf.bak
11.conf.bak  15.conf.bak  19.conf.bak  22.conf.bak  26.conf.bak  2.conf.bak   5.conf.bak  9.conf.bak
12.conf.bak  16.conf.bak  1.conf.bak   23.conf.bak  27.conf.bak  30.conf.bak  6.conf.bak
13.conf.bak  17.conf.bak  20.conf.bak  24.conf.bak  28.conf.bak  3.conf.bak   7.conf.bak
“`
##### 命令rm防止误删除
“`
方法:定义别名
[root@centos7 08:28:26 ~]#alias rm=’mv -t /data’
并且写入配置文件.bashrc
“`
##### 磁盘空间被占满
“`
在boot目录下生成一个大文件
[root@centos7 08:51:45 boot]#dd if=/dev/zero of=/boot/bigfile bs=1M count=800
[root@centos7 08:53:47 boot]#df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        30G   33M   30G   1% /data
/dev/sda1      1014M  958M   57M  95% /boot
此时/boot空间即将被占满,假若有用户正在访问bigfile这个文件,用rm删除文件,文件虽然被删除,但并不能马上释放空间。
解决方法一:
lsof |grep deleted  查看系统中被删除,但还没释放空间的文件。
[root@centos7 09:12:18 boot]#lsof |grep deleted
single    1691 2277           root   15r      REG                8,2     32768   68247442 /root/.local/share/gvfs-metadata/root-056956aa.log (deleted)
vi        2935                root    3r      REG                8,1 838860800         79 /boot/bigfile (deleted)
[root@centos7 09:12:48 boot]#killall vi
[root@centos7 09:15:23 boot]#df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           609M     0  609M   0% /sys/fs/cgroup
/dev/sda3        30G   33M   30G   1% /data
/dev/sda1      1014M  158M  857M  16% /boot
“`
“`
解决方法二:
[root@centos7 09:17:41 boot]#>bigfile
[root@centos7 09:17:55 boot]#df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        30G   33M   30G   1% /data
/dev/sda1      1014M  158M  857M  16% /boot
tmpfs           122M   24K  122M   1% /run/user/0
/dev/sr0        8.1G  8.1G     0 100% /run/media/root/CentOS 7 x86_64
[root@centos7 09:18:14 boot]#\rm -rf bigfile
“`
##### 命令tree
“`
-L 显示目录的层级       例:tree -L 2 /etc/
-d 只显示目录不显示文件
-P pattern 只显示由指定pattern匹配到的路径
“`
##### 元数据与数据
“`
inode(index node)表中包含文件系统所有文件列表
一个节点 (索引节点)是在一个表项,包含有关文件的信息( 元数据 ),包
括:
文件类型,权限,UID,GID
链接数(指向这个文件名路径名称个数)
该文件的大小和不同的时间戳
指向磁盘上文件的数据块指针
有关文件的其他数据
目录的元数据:包括目录自身权限,目录类型,UID,GID等信息
目录真实数据:存放的是文件i节点编号,通过节点编号找到节点表,通过节点表找到磁盘中文件数据,因此删除目录中的文件并不是删除文件数据,只是删除文件节点编号,数据并未被马上删除。
彻底删除磁盘中的文件使用如下方法:
[root@centos7 10:36:51 data]#ll
total 4
-rw-r–r–. 1 root root 2059 Mar 31 10:36 passwd
[root@centos7 10:36:54 data]#shred -zv -n 8 passwd
shred: passwd: pass 1/9 (random)…
shred: passwd: pass 2/9 (555555)…
shred: passwd: pass 3/9 (000000)…
shred: passwd: pass 4/9 (random)…
shred: passwd: pass 5/9 (ffffff)…
shred: passwd: pass 6/9 (random)…
shred: passwd: pass 7/9 (aaaaaa)…
shred: passwd: pass 8/9 (random)…
shred: passwd: pass 9/9 (000000)…
[root@centos7 10:37:18 data]#ll
total 4
-rw-r–r–. 1 root root 4096 Mar 31 10:37 passwd
-z表示最后一次用0覆盖,-n表示覆盖的次数
“`
“`
模拟磁盘inode被用完,但磁盘空间并没有满(/boot/test)
[root@centos7 10:07:10 testdir]#df -i
Filesystem       Inodes  IUsed    IFree IUse% Mounted on
/dev/sda3      15728640     10 15728630    1% /data
/dev/sda1        524288    327   523961    1% /boot
echo file{1..524288} |xargs touch    xargs解决参数过多问题
touch: cannot touch ‘file524287’: No space left on device
touch: cannot touch ‘file524288’: No space left on device
[root@centos7 10:14:14 test]#df -h
/dev/sda1      1014M  433M  582M  43% /boot
[root@centos7 10:13:13 test]#df -i
Filesystem       Inodes  IUsed    IFree IUse% Mounted on
/dev/sda1        524288 524288        0  100% /boot
“`
“`
结果磁盘空间还剩余,但inode被用光,提示磁盘空间不足,删除大量文件
echo file{1..524288} |xargs rm
“`
##### 软硬链接
“`
链接数含义:同一个文件有几个名,硬链接链接一次链接数增加一次,而软链接不增加。
硬链接机制:一个节点编号,多个文件名
软硬链接本质区别:
硬链接:同一个文件,多个名字
软链接:一个文件对应快捷方式,不同文件
硬链接特点:
1.链接文件彼此独立平等,删除一个文件只是删除一个名字,还可通过其他文件名访问
2.不可以跨分区链接(硬链接原理是一个节点,如果跨分区了,可能出现多个节点)
3.不能对目录创建
4.硬链接链接次数会增加
5.删除原始文件,新链接文件不受影响
6.硬链接大小由文件由原文件大小决定
软链接特点:
1.软链接依赖原始文件
2.可以跨分区
3.可对目录创建
4.链接数不增加
5.删除原始文件,链接失效
6.软链接的大小由原始文件的路径决定
注:创建软链接路径,相对路径不能使相对当前目录路径,而是相对软链接的路径
例:
[root@centos7 11:13:54 data]#ln -s ../data/fi /root/link2
[root@centos7 11:14:54 data]#ll /root/link2
lrwxrwxrwx. 1 root root 10 Mar 31 11:14 /root/link2 -> ../data/fi
“`
##### 命令readlink(查看链接对应的原始文件)
“`
例:
[root@centos7 11:30:25 ~]#readlink link2
../data/fi
“`
##### 利用软链接实现软件部署
“`
假设nginx有两个版本,nginx1,nginx2,可实现两个版本相互切换
[root@centos7 11:45:14 data]#ln -s nginx1 nginx
[root@centos7 11:45:37 data]#ll
total 0
lrwxrwxrwx. 1 root root 6 Mar 31 11:45 nginx -> nginx1
drwxr-xr-x. 2 root root 6 Mar 31 11:45 nginx1
drwxr-xr-x. 2 root root 6 Mar 31 11:45 nginx2
[root@centos7 11:46:06 data]#rm nginx
[root@centos7 11:46:55 data]#ln -s nginx2 nginx
[root@centos7 11:47:00 data]#ll
total 0
lrwxrwxrwx. 1 root root  6 Mar 31 11:47 nginx -> nginx2
drwxr-xr-x. 2 root root 20 Mar 31 11:46 nginx1
drwxr-xr-x. 2 root root  6 Mar 31 11:45 nginx2
“`
##### 命令file
“`
文件可以包含多种类型的数据
检查文件的类型,然后确定适当的打开命令或应用程序使用
file [options] <filename>…
常用选项:
-b 列出文件辨识结果时,不显示文件名称
-f filelist 列出文件filelist中文件名的文件类型
-F 使用指定分隔符号替换输出文件名后默认的”:”分隔符
-L 查看对应软链接对应文件的文件类型
“`
##### I/O重定
“`
错误输出和正确输出分别输入到对应文件中
“`
“`
[root@centos7 12:37:16 data]#ls /erro /data >true 2>erro
[root@centos7 13:41:55 data]#cat true
/data:
erro
link
links
nginx
[root@centos7 13:42:15 data]#cat erro
ls: cannot access /erro: No such file or directory
“`
“`
正确输出和错误输出都重定向到一个文件中
方法一:
[root@centos7 13:42:24 data]#ls /erro /data >f1 2>&1
[root@centos7 13:46:49 data]#cat f1
ls: cannot access /erro: No such file or directory
/data:
erro
f1
link
links
nginx
“`
“`
方法二:
[root@centos7 13:46:58 data]#ls /erro /data &>f1
[root@centos7 13:53:15 data]#cat f1
ls: cannot access /erro: No such file or directory
/data:
erro
f1
link
links
nginx
“`
“`
禁止覆盖,强制覆盖,允许覆盖
[root@centos7 13:53:24 data]#hostname >f1
[root@centos7 13:56:47 data]#set -C
[root@centos7 13:56:56 data]#hostname >f1
-bash: f1: cannot overwrite existing file
[root@centos7 13:57:07 data]#ls >| f1
[root@centos7 13:57:58 data]#set +C f1
“`
“`
以下四个重定向,哪个与其它三个不同
[root@centos7 13:58:53 data]#ls /erro /data >log1 2>&1
[root@centos7 14:05:51 data]#ls /erro /data 2>&1 >log2
ls: cannot access /erro: No such file or directory
[root@centos7 14:07:39 data]#ls /erro /data &>log3
[root@centos7 14:08:12 data]#ls /erro /data 2>log4 >&2
其中第二个执行结果错误输出未重定向到文件中
“`
“`
cat <f1 >f1    相当于清空f1
cat <f1 >f2   相当复制f1到f2
cat <f1 >>f2   无限追加f1内容到f2
“`

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/94319

(0)
上一篇 2018-04-01 17:44
下一篇 2018-04-01 18:10

相关推荐