alias
别名
别名的作用: 系统为了安全给重要的命令加了 -i参数 起了别名(小名)
张三–>小名 铁蛋
李四–>小名 狗胜
王xx–>小名 二柱子
rm -----> rm -i
cp -----> cp -i
mv -----> mv -i
查看系统的别名
[root@ahui ~]#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'
查看定义的临时别名
[root@ahui ~]#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='echo command bny........'
alias vieth0='vim /etc/sysconfig/network-scripts/ifcfg-eth0'
临时取消别名
第一种方法: unalias
[root@ahui ~]#unalias rm
[root@ahui ~]#rm 2.txt
第二种方法: \
[root@ahui ~]#\rm 1.txt
自定义别名
命令行定义临时生效 重启失效
alias 别名(自定义)=‘可执行命令’
永久定义别名
命令写入/etc/profile
[root@ahui ~]#tail -1 /etc/profile
alias vieth0='vim /etc/sysconfig/network-scripts/ifcfg-eth0'
案例1: rm
[root@ahui ~]#alias rm='echo command bny........'
[root@ahui ~]#ll
总用量 4
drwxr-xr-x 2 root root 19 5月 10 12:09 test
-rw-r--r-- 1 root root 32 5月 10 12:15 test.txt
[root@ahui ~]#rm test.txt
command bny........ test.txt
案例2: 类似变量
[root@ahui ~]#alias vieth0='vim /etc/sysconfig/network-scripts/ifcfg-eth0'
[root@ahui ~]#vieth0
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=10.0.0.200
PREFIX=24
GATEWAY=10.0.0.2
DNS1=223.5.5.5
文件详细属性
查看文件的属性 ls
ls
-l 长格式显示文件详细信息
-h 人类可读
-t 按照时间排序
-r 逆序排序
[root@ahui ~]#ls -l
总用量 4
drwxr-xr-x 2 root root 19 5月 10 12:09 test
-rw-r--r-- 1 root root 32 5月 10 12:15 test.txt
注意: ls查看的文件或者目录默认按照名字的一个字母进行正序排序
按照字母的逆序进行排序:
[root@ahui ~]#ls -rl /etc/
总用量 1192
drwxr-xr-x. 2 root root 65 11月 29 21:17 yum.repos.d
-rw-r--r--. 1 root root 970 4月 13 2018 yum.conf
drwxr-xr-x. 6 root root 100 8月 28 2020 yum
或者使用别名
[root@ahui ~]#ll -r /etc/
总用量 1192
drwxr-xr-x. 2 root root 65 11月 29 21:17 yum.repos.d
-rw-r--r--. 1 root root 970 4月 13 2018 yum.conf
按照时间进行排序 默认最新的文件在最上面
[root@ahui ~]#ll -t /etc/
总用量 1192
-rw-r--r-- 1 root root 0 5月 11 09:36 ahui.txt
-rw-r--r-- 1 root root 0 5月 11 08:47 huhu.txt
按照时间进行逆序排序 最新的文件在最下面出现
[root@ahui ~]#ll -rt /etc/
-rw-r--r-- 1 root root 0 5月 11 08:47 huhu.txt
-rw-r--r-- 1 root root 0 5月 11 09:36 ahui.txt
如何识别Linux中的文件或者目录(1)
1.使用颜色区分
2.使用文件的后缀区分 windows的使用方式 在Linux中一切皆文件 后缀自定义的给我们自己看
3.查看文件的属性区分
4.通过命令进行区分
文件属性每列的含义
16819633 - rw-r--r-- . 1 root root 158 6月 7 2013 /etc/hosts
-------- - --------- - - ---- ---- --- ------------ -----------
1 2 3 4 5 6 7 8 9 10
第一列: 文件的inode号码
特点:
类似身份证信息 通过身份证信息找到对应的人
类似藏宝图 通过藏宝图找到对应的宝藏
inode号码中存储了具体存储文件内容的指针指向
inode号码中存储了文件的详细信息
指向了具体的硬件存储位置(block)
文件名称存储在上级的目录中
第二列: - 文件类型
什么是文件类型:
- Windows: exe 安装 运行程序
.txt 普通文本文档
.doc word文档
.pdf
.avi
.mp4 - Linux: .txt 普通文本文档 一切皆文件
.sh 可执行脚本
.rpm 安装包
.jpb 图片
.mp4 视频
.py python脚本文件
Linux文件系统类型分类:
第一种文件类型: -
三种表示方法:
-
普通文件 hosts passwd ahui.txt
-
系统命令 touch vim pwd ls
-
数据文件 压缩文件 rpm包 视频类型
第二种文件类型: d 目录
第三种文件类型: l 软链接文件 小写的L 称为符号链接 softlink
[root@ahui ~]#ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 8月 28 2020 /etc/rc.local -> rc.d/rc.local
其他文件类型: c 字符串设备文件
[root@ahui ~]#ll /dev/urandom
crw-rw-rw- 1 root root 1, 9 12月 29 16:38 /dev/urandom
[root@ahui ~]#ll /dev/null
crw-rw-rw- 1 root root 1, 3 12月 29 16:38 /dev/null
[root@ahui ~]#ll /dev/zero
crw-rw-rw- 1 root root 1, 5 12月 29 16:38 /dev/zero
b 块设备 /dev/sda /dev/cdrom block
p 管道文件
s 网络套接字
扩展: /dev/null
作用: 类似黑洞 可以把不想要的执行结果输出到黑洞中
执行结果: 执行命令显示到屏幕上的内容
[root@ahui ~]#ping -c1 www.baidu.com >/dev/null
[root@ahui ~]#echo $?
0
[root@ahui ~]#ping -c1 www.baiduaaaaa.com >/dev/null
ping: www.baiduaaaaa.com: 未知的名称或服务
$? 上一条命令的执行结果 0为成功 非0 失败
[root@ahui ~]#echo $?
2
特殊符号:> >>
使用> 标准正确输出重定向 先清空后写入 相等于 1> 默认省略了1
只接收正确的执行结果
使用>>标准正确追加输出重定向 1>>
[root@ahui ~]#echo hehe
hehe
[root@ahui ~]#echo hehe > 1.txt
[root@ahui ~]#cat 1.txt
hehe
[root@ahui ~]#ech hehe
-bash: ech: 未找到命令
[root@ahui ~]#
[root@ahui ~]#ech hehe > 1.txt
-bash: ech: 未找到命令
[root@ahui ~]#cat 1.txt
[root@ahui ~]#ls test.txt
test.txt
[root@ahui ~]#ls test.txt > 1.txt
[root@ahui ~]#cat 1.txt
test.txt
[root@ahui ~]#ls test.txttttt > 1.txt
ls: 无法访问test.txttttt: 没有那个文件或目录
[root@ahui ~]#cat 1.txt
[root@ahui ~]#
[root@ahui ~]#echo hehe 1>1.txt
[root@ahui ~]#cat 1.txt
hehe
[root@ahui ~]#ls 111111 1> 1.txt
ls: 无法访问111111: 没有那个文件或目录
[root@ahui ~]#cat 1.txt
2> 标准错误输出重定向
只接收错误的执行结果 正确的不要
2>> 标准错误追加输出重定向
[root@ahui ~]#echo hehe
hehe
[root@ahui ~]#echo hehe 2> 2.txt
hehe
[root@ahui ~]#cat 2.txt
接收执行结果为错误的
[root@ahui ~]#ech hehe
-bash: ech: 未找到命令
[root@ahui ~]#ech hehe 2> 2.txt
[root@ahui ~]#cat 2.txt
-bash: ech: 未找到命令
正确和错误的结果输出到test.txt中 >>test.txt 2>>test.txt
第一种写法:
[root@ahui ~]#echo hehe >test.txt
[root@ahui ~]#cat test.txt
hehe
[root@ahui ~]#ech hehe >>test.txt 2>>test.txt
[root@ahui ~]#
[root@ahui ~]#cat test.txt
hehe
-bash: ech: 未找到命令
第二种写法: >>test.txt 2>&1 接收正确的结果到test.txt 错误的结果和1相同
[root@ahui ~]#echo hehe >>test.txt 2>&1
[root@ahui ~]#ech hehe >>test.txt 2>&1
[root@ahui ~]#cat test.txt
hehe
hehe
-bash: ech: 未找到命令
第三种写法: &>test.txt 表示正确和错误的都输入到test.txt 文件中
[root@ahui ~]#echo hehe &>test.txt
[root@ahui ~]#echo hehe &>>test.txt
[root@ahui ~]#
[root@ahui ~]#eco hehe &>>test.txt
[root@ahui ~]#
[root@ahui ~]#cat test.txt
hehe
hehe
-bash: eco: 未找到命令
案例: 将执行结果输入到空
[root@ahui ~]#ping -W1 -c1 www.baiduaaaa.com &>/dev/null
[root@ahui ~]#echo $?
2
[root@ahui ~]#ll /dev/sr0
brw-rw---- 1 root cdrom 11, 0 5月 11 10:40 /dev/sr0
[root@ahui ~]#ll /dev/sda
brw-rw---- 1 root disk 8, 0 12月 29 16:38 /dev/sda
文件相关的命令
which
查看命令的所在路径
[root@ahui ~]#which mkdir
/usr/bin/mkdir
[root@ahui ~]#which touch
/usr/bin/touch
[root@ahui ~]#which ntpdate
/usr/sbin/ntpdate
whereis
查看命令的全路径和命令的帮助文件所在位置 了解
[root@ahui ~]#whereis mkdir
mkdir: /usr/bin/mkdir /usr/share/man/man1/mkdir.1.gz
file
查看文件类型
[root@ahui ~]#file test.txt
test.txt: UTF-8 Unicode text
[root@ahui ~]#ll
总用量 4
-rw-r--r-- 1 root root 38 5月 11 10:33 test.txt
[root@ahui ~]#mkdir ahui
[root@ahui ~]#ll
总用量 4
drwxr-xr-x 2 root root 6 5月 11 10:50 ahui
-rw-r--r-- 1 root root 38 5月 11 10:33 test.txt
[root@ahui ~]#file ahui
ahui: directory
[root@ahui ~]#file /etc/rc.local symbolic: 符号链接 软链接
/etc/rc.local: symbolic link to `rc.d/rc.local'
find
查找系统中某个文件或者多个文件目录
根据文件类型查找文件目录软链接
find语法结构:
find 路径 文件类型 特点
find / -type f
-type: 文件类型:
f普通文件
d文件夹
b块设备
c字符设备
l 软链接文件
案例1: 查找当前目录中所有的普通文件(默认会显示隐藏文件)
[root@ahui ~]#find ./ -type f
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./.bash_history
./.mysql_history
./.rnd
./.lesshst
./.viminfo
./test.txt
带路径查找
[root@ahui ~]#touch ahui/{1..4}.txt
[root@ahui ~]#ll ahui/
总用量 0
-rw-r--r-- 1 root root 0 5月 11 10:57 1.txt
-rw-r--r-- 1 root root 0 5月 11 10:57 2.txt
-rw-r--r-- 1 root root 0 5月 11 10:57 3.txt
-rw-r--r-- 1 root root 0 5月 11 10:57 4.txt
查找ahui目录下所有的普通文件
[root@ahui ~]#find ahui/ -type f
ahui/1.txt
ahui/2.txt
ahui/3.txt
ahui/4.txt
案例2: 查找当前目录下所有的目录
[root@ahui ~]#find ./ -type d
./
./ahui
案例3: 按照名字来查找文件
[root@ahui ~]#ll ahui/
总用量 0
-rw-r--r-- 1 root root 0 5月 11 10:57 1.txt
-rw-r--r-- 1 root root 0 5月 11 10:57 2.txt
-rw-r--r-- 1 root root 0 5月 11 10:57 3.txt
-rw-r--r-- 1 root root 0 5月 11 10:57 4.txt
[root@ahui ~]#find ahui/ -name "4.txt"
ahui/4.txt
案例4: 按照名称模糊查找
[root@ahui ~]#find ahui/ -name "*.txt"
ahui/1.txt
ahui/2.txt
ahui/3.txt
ahui/4.txt
[root@ahui ~]#touch ahui/{5..10}.TXT
[root@ahui ~]#ll ahui/
总用量 0
-rw-r--r-- 1 root root 0 5月 11 11:01 10.TXT
-rw-r--r-- 1 root root 0 5月 11 10:57 1.txt
-rw-r--r-- 1 root root 0 5月 11 10:57 2.txt
-rw-r--r-- 1 root root 0 5月 11 10:57 3.txt
-rw-r--r-- 1 root root 0 5月 11 10:57 4.txt
-rw-r--r-- 1 root root 0 5月 11 11:01 5.TXT
-rw-r--r-- 1 root root 0 5月 11 11:01 6.TXT
-rw-r--r-- 1 root root 0 5月 11 11:01 7.TXT
-rw-r--r-- 1 root root 0 5月 11 11:01 8.TXT
-rw-r--r-- 1 root root 0 5月 11 11:01 9.TXT
查找ahui目录下1.开头的文件 结尾任意
[root@ahui ~]#find ahui/ -name "1.*"
ahui/1.txt
案例5: 查找所有的普通文件
不区分大小写 iname
[root@ahui ~]#find ahui/ -name "*.txt"
ahui/1.txt
ahui/2.txt
ahui/3.txt
ahui/4.txt
[root@ahui ~]#find ahui/ -name "*.TXT"
ahui/5.TXT
ahui/6.TXT
ahui/7.TXT
ahui/8.TXT
ahui/9.TXT
ahui/10.TXT
[root@ahui ~]#find ahui/ -iname "*.TXT"
ahui/1.txt
ahui/2.txt
ahui/3.txt
ahui/4.txt
ahui/5.TXT
ahui/6.TXT
ahui/7.TXT
ahui/8.TXT
ahui/9.TXT
ahui/10.TXT
企业案例: 在/data下查找code.txt 文件
find /data -name "code.txt"
find /data -name "code.*"
案例6: 按照深度等级查找使用 -maxdepth 默认递归查找
[root@ahui ~]# find ahui/ -type f
ahui/1.txt
ahui/2.txt
ahui/3.txt
ahui/4.txt
ahui/5.TXT
ahui/6.TXT
ahui/7.TXT
ahui/8.TXT
ahui/9.TXT
ahui/10.TXT
ahui/test/ahui-1.txt
ahui/test/ahui-2.txt
ahui/test/ahui-3.txt
#-maxdepth 1 在一级目录中查找文件
[root@ahui ~]#find ahui/ -maxdepth 1 -type f
ahui/1.txt
ahui/2.txt
ahui/3.txt
ahui/4.txt
ahui/5.TXT
ahui/6.TXT
ahui/7.TXT
ahui/8.TXT
ahui/9.TXT
ahui/10.TXT
案例7: 查找普通文件并且名称为 test.txt 不是目录 不是软链接 不是字节设备
[root@ahui ~]#find ahui/ -type f -name "test.txt"
ahui/test/test.txt
[root@ahui ~]#find ahui/ -type f -name "*.txt"
ahui/1.txt
ahui/2.txt
ahui/3.txt
ahui/4.txt
ahui/test/ahui-1.txt
ahui/test/ahui-2.txt
ahui/test/ahui-3.txt
ahui/test/test.txt
案例8: 按照文件的大小查找
-size -k -M -G
+1M 大于1M +1G
-2M 小于2M -1G
1M 等于1M 1G
合并文件:
[root@ahui ~]#cat /etc/services /etc/services /etc/services > all.txt
[root@ahui ~]#ll -h
总用量 2.0M
-rw-r--r-- 1 root root 2.0M 5月 11 11:19 all.txt
查找当前目录下大于1M的文件
[root@ahui ~]#find ./ -size +1M
./all.txt
查找ahui目录下小于2M的文件
[root@ahui ~]#find ahui/ -size -2M
ahui/
ahui/1.txt
ahui/2.txt
ahui/3.txt
查找ahui目录下大于2M并且小于10M的文件
[root@ahui ~]#ll -h ahui/
总用量 20M
-rw-r--r-- 1 root root 0 5月 11 11:01 10.TXT
-rw-r--r-- 1 root root 0 5月 11 10:57 1.txt
-rw-r--r-- 1 root root 5.8M 5月 11 11:24 2.txt
-rw-r--r-- 1 root root 12M 5月 11 11:24 3.txt
-rw-r--r-- 1 root root 0 5月 11 10:57 4.txt
-rw-r--r-- 1 root root 0 5月 11 11:01 5.TXT
-rw-r--r-- 1 root root 0 5月 11 11:01 6.TXT
-rw-r--r-- 1 root root 0 5月 11 11:01 7.TXT
-rw-r--r-- 1 root root 0 5月 11 11:01 8.TXT
-rw-r--r-- 1 root root 0 5月 11 11:01 9.TXT
-rw-r--r-- 1 root root 2.0M 5月 11 11:19 all.txt
drwxr-xr-x 2 root root 82 5月 11 11:15 test
[root@ahui ~]#find ahui/ -size +2M -size -10M
ahui/2.txt
大小等于2M并且小于10M
[root@ahui ~]#find ahui/ -size 2M -size -10M
ahui/all.txt
-size参数默认就是并且关系 find把and 省略了
[root@ahui ~]#find ahui/ -size 2M -and -size -10M
ahui/all.txt
查找ahui目录下大于2M 或者名称为1.txt的文件
[root@ahui ~]#find ahui/ -size +2M -or -name "1.txt"
ahui/1.txt
ahui/2.txt
ahui/3.txt
find进阶:
find查找的文件交给其他命令来调用
如:cp mv rm ls -l
案例1: 查找ahui目录下的1.txt 交给cat命令查看 xargs
[root@ahui ~]#find ahui/ -name "1.txt"|xargs cat
hehe
案例2: 查找ahui目录下的1.txt 交给rm命令删除
注意: 别名在xargs后面失效 所有rm删除不需要加参数-f
[root@ahui ~]#find ahui/ -iname "7.txt"|xargs rm
[root@ahui ~]#ll ahui/7.TXT
ls: 无法访问ahui/7.TXT: 没有那个文件或目录
案例3: 查找ahui目录下的1.txt 交给ls -l查看属性 (ll别名 失效)
[root@ahui ~]#find ahui/ -name "1.txt"|xargs ls -l
-rw-r--r-- 1 root root 5 5月 11 11:39 ahui/1.txt
案例4: 在ahui目录下查找2.txt文件 并且复制到/opt目录下
[root@ahui ~]#find ahui/ -name "1.txt"|xargs -i cp {} /opt/
[root@ahui ~]#ll /opt/
总用量 4
-rw-r--r-- 1 root root 5 5月 11 11:47 1.txt
案例5: 在ahui目录下查找2.txt文件 移动到/opt下
xargs的-i参数将管道符前的输出填到{}位置
[root@ahui ~]#find ahui/ -name "2.txt"
ahui/2.txt
[root@ahui ~]#find ahui/ -name "2.txt"|xargs -i mv {} /opt/
[root@ahui ~]#ll ahui/2.txt
ls: 无法访问ahui/2.txt: 没有那个文件或目录
[root@ahui ~]#ll /opt/
总用量 5896
-rw-r--r-- 1 root root 5 5月 11 11:47 1.txt
-rw-r--r-- 1 root root 6032637 5月 11 11:24 2.txt
案例6: 使用exec方式进行cp mv rm
exec 拷贝
[root@ahui ~]#find ahui/ -name "4.txt" -exec cp {} /opt/ \;
[root@ahui ~]#ll /opt/
总用量 5896
-rw-r--r-- 1 root root 5 5月 11 11:47 1.txt
-rw-r--r-- 1 root root 6032637 5月 11 11:24 2.txt
-rw-r--r-- 1 root root 0 5月 11 11:51 4.txt
exec删除
[root@ahui ~]#find ahui/ -name "4.txt" -exec rm {} \;
[root@ahui ~]#ll ahui/4.txt
ls: 无法访问ahui/4.txt: 没有那个文件或目录
exec移动
[root@ahui ~]#find ahui/ -name "6.TXT" -exec mv {} /opt/ \;
[root@ahui ~]#ll ahui/6.TXT
ls: 无法访问ahui/6.TXT: 没有那个文件或目录
[root@ahui ~]#ll /opt/
总用量 5896
-rw-r--r-- 1 root root 5 5月 11 11:47 1.txt
-rw-r--r-- 1 root root 6032637 5月 11 11:24 2.txt
-rw-r--r-- 1 root root 0 5月 11 11:51 4.txt
-rw-r--r-- 1 root root 0 5月 11 11:01 6.TXT
exec查看
[root@ahui ~]#find ahui/ -type f -exec ls -l {} \;
-rw-r--r-- 1 root root 5 5月 11 11:39 ahui/1.txt
-rw-r--r-- 1 root root 12065274 5月 11 11:24 ahui/3.txt
;分号在命令行中有特殊的含义 命令的分隔符
[root@ahui ~]#ls;pwd;cd
ahui test.txt
/root
案例7: 将查询到的结果交给cp mv rm ls -l 使用反引号
直接将想要作为输出的命令用反引号``括住按输入格式放在被输入的命令中
反引号查看
[root@ahui ~]#ll `find ahui/ -name "8.TXT"`
-rw-r--r-- 1 root root 0 5月 11 11:01 ahui/8.TXT
[root@ahui ~]#ls -l `find ahui/ -name "8.TXT"`
-rw-r--r-- 1 root root 0 5月 11 11:01 ahui/8.TXT
反引号删除
[root@ahui ~]#rm -f `find ahui/ -name "8.TXT"`
反引号复制
[root@ahui ~]#cp `find ahui/ -name "3.txt"` /opt/
[root@ahui ~]#ll /opt/
总用量 17680
-rw-r--r-- 1 root root 5 5月 11 11:47 1.txt
-rw-r--r-- 1 root root 6032637 5月 11 11:24 2.txt
-rw-r--r-- 1 root root 12065274 5月 11 12:00 3.txt
反引号移动
[root@ahui ~]#mv `find ahui/ -name "all.txt"` .
[root@ahui ~]#ll
总用量 1968
-rw-r--r-- 1 root root 2010879 5月 11 11:19 all.txt
find小结:
-
find按照文件类型类型:
find ahui/ -type f
find ahui/ -type d -
find按照名称查找:
find ahui/ -name “4.txt” -
find模糊查找:
find ahui/ -name “*.txt” -
find查找不区分大小写:
find ahui/ -iname “*.txt” -
find按照深度等级查找:
find ahui/ -maxdepth 1 -type f -
find并且查找:
[root@ahui ~]#find ahui/ -type f -name “test.txt” -
find按照大小查找:
find ./ -size +1M
find ./ -size -2M
find ./ -size 1M
find ./ -size +1M -size -10M
find ./ -size +1M -or -name “1.txt” -
find结果输出到命令中
- : xargs
find ./ -name “1.txt”|xargs -i cp {} /opt
find ./ -name “1.txt”|xargs rm
find ./ -name “1.txt”|xargs ls -l - : exec
find ./ -name “1.txt” -exec cp {} /opt ;
find ./ -name “1.txt” -exec rm {} ;
find ./ -name “1.txt” -exec ls -l {} ; - : ``反引号
ls -l `find ./ -name "1.txt"`
cp `find ./ -name "1.txt"` /opt
rm -f `find ./ -name "1.txt"`
小结:
-
alias 别名
unalias rm -
文件属性 (1)
inode 文件的指针指向和属性信息
文件类型:
f普通文件
d文件夹
b块设备
c字符设备
l 软链接文件 -
文件相关命令
which 查看命令的所在路径
whereis 查看命令的全路径和命令的帮助文件所在位置
file 查看文件类型
/dev/null 黑洞 -
特殊符号:> >>
2> >
1>/dev/null 2>/dev/null
1>/dev/null 2>&1
echo hehe &>/dev/null -
find 查找
- 类型查找
- 名称查找
- 不区分大小写
- 深度等级查找
- 并且关系
- 或者关系
- 按照大小查找
- 查找到结果交给其他命令处理
- -xargs 、-exec 、``反引号