[linux]一、星级命令find的详细用法、read、数组定义以及引用方法、seq的详细用法

目录

1、seq 生成数列

1.1、seq  n  

1.2、seq  n m

1.3、seq  n  k m  

1.4、seq -w

1.5、seq -s

1.5.1、bc

1.5.2、for和seq结合

1.6、数组定义以及引用方法

1.6.1、数组的各种用法

1.6.2、用数组编写一个抽奖的代码 

1.7、用shell产生5到9之间的数 

2、./ 相当于bash和source

3、which + 命令或者程序名

4、whereis +命令

5、locate

5.1、which和whereis、locate的差异

6、find⭐⭐⭐

6.1、find + 范围 +  -name

6.2、find + 范围 + -iname 按文件名查找,不区分大小写

6.3、find + 范围 + -size 按照文件大小查找

6.4、-name 和 -size组合使用

6.5、多条件组合

6.5.1、-and;-a   并且

6.5.2、-or ;-o  或者

6.5.3、-not  逻辑非;取反

6.5.4、改变优先级

6.6、find + 范围 + -type + 文件类型

6.7、find + 范围 + -mtime 

6.7.1、-mmin   以分钟为单位

6.8、find + 范围 + -user + 用户

6.9、find + 范围 + 查找条件 + 动作(-exec;-ok)⭐⭐⭐

6.9.1、动作(-exec)

6.9.2、动作(-ok)

6.10、-maxdepth 

6.11、-newr

7、read

8、小练习


1、seq 生成数列

1.1、seq  n  

生成 1到n的差为1的等差数列

[root@fttsaxf rough_book]# seq 3
1
2
3

1.2、seq  n m

 生成n到m的差为1的等差数列

[root@fttsaxf rough_book]# seq 3 5
3
4
5

1.3、seq  n  k m  

生成n到m的差为k的等差数列,k可以为自然数

[root@fttsaxf rough_book]# seq 1 3 10
1
4
7
10

1.4、seq -w

用0来填充,达到宽度一样

[root@fttsaxf rough_book]# seq  -w 1 10
01
02
03
04
05
06
07
08
09
10

1.5、seq -s

[root@fttsaxf rough_book]# seq  -s " " 10  # "",中是用来连接这个序列的字符
1 2 3 4 5 6 7 8 9 10

1.5.1、bc

是linux里的计算器

利用seq和bc计算1到100之和

[root@fttsaxf rough_book]# seq -s "+" 100
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+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100
[root@fttsaxf rough_book]# seq -s "+" 100|bc
5050

bc的正常使用

[root@fttsaxf rough_book]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
9+8
17

1.5.2、for和seq结合

[root@fttsaxf rough_book]# cat for.sh 
#!/bin/bash

for i in $(seq 5)
do
	echo "第$i次循环"
done

for i in "I   Love   You"
do
	echo "字符内容分别为:$i"
done
# for循环在读取字符串的时候,以空白(空格和tab键)作为分隔符
for i in $(echo "I   Love   You")
do
	echo "字符内容分别为:$i"
done

[root@fttsaxf rough_book]# bash for.sh 
第1次循环
第2次循环
第3次循环
第4次循环
第5次循环
字符内容分别为:I   Love   You
字符内容分别为:I
字符内容分别为:Love
字符内容分别为:You

1.6、数组定义以及引用方法

1.6.1、数组的各种用法


图片解释:图中ip为变量 ,且shell在中,当数组删除一个数之后,他的下标不会自动重新排序。图中的"@"可以换成"*"

[root@fttsaxf rough_book]# star=(beidou beijixiong xiannv)
[root@fttsaxf rough_book]# echo ${star[0]}
beidou

1.6.2、用数组编写一个抽奖的代码 

[root@fttsaxf lucky_draw]# cat singer.sh 
#!/bin/bash

# 将name1.txt文件立的内容读取出来后赋值给数组singers
singers=($(cat /root/linux/script/lucky_draw/name1.txt))

#统计singers数组里有多少个元素
total=${#singers[*]}  # 控制for循环的次数

#有多少人,就循环多少次
for i in $(seq $total)
do
	lucky_num=$((RANDOM % ${#singers[*]}))  # 随机抽取一个数字,做下标使用
	echo "有请著名歌手:${singers[$lucky_num]},献歌一首"
	# 删除抽取的歌手
	unset singers[$lucky_num]
	# 重新生成新的数组,防止下标不连续,因为产生随机数,希望有连续的数字,不然会导致抽不到人
	singers=($(echo ${singers[*]}))
	# 输出还有那些歌手没有演唱
	echo "还有${#singers[*]}位歌手没有演唱,请在后台做好准备"
	echo "没有演唱的歌手具体名单如下,${singers[*]}"
	echo "###############五分钟过后#############"
	read -p "按任意键有请下一位歌手上台演唱"
	echo "热烈欢迎歌手上台演唱"
	echo;echo
done

 运行的部分结果如下:

1.7、用shell产生5到9之间的数 

9-5=4--》产生0~4的数,然后加5

[root@fttsaxf script]# echo $((RANDOM % 5 +5 ))

2、./ 相当于bash和source

文件报权限不够就是没有执行权限

3、which + 命令或者程序名

用途:查找可执行文件并显示所在的位置——搜索范围由PATH环境变量指定

可执行文件:

        1.c语言编译出来的二进制文件   本身就自带可执行权限
        2.python文件,授予可执行权限   chmod +x sc.py
        3.shell脚本文件,授予可执行权限  chmod +x sc.sh

===============================

4、whereis +命令

whereis mkdir
mkdir: /usr/bin/mkdir /usr/share/man/man1/mkdir.1.gz
          所在路径           mkdir的man手册

=================================

可执行文件:
    1、c语言编译出来的二进制文件,本身就自带可执行权限
    2、python文件,授予可执行权限   chmod  +x  sc.py
    3、shell脚本文件,授予可执行权限   chomd +x sc.sh

编译安装
    tomcat——》jave
    nginx——》静态页面
    MySQL——》数据库软件

/usr    是linux存放系统资源的目录   unix  system  resource  
==========================================

5、locate

locate能查找命令和文件(locate命令是在mlocate.db这个文件里查找这些命令和文件的,所以查询速度比find更快一些),但是查找新文件的时候,需要进行更新操作(updatedb)

[root@fttsaxf linux-workbook]# locate cali143 
[root@fttsaxf linux-workbook]# updatedb
[root@fttsaxf linux-workbook]# locate cali143 
/root/linux-workbook/cali143

5.1、which和whereis、locate的差异

1.which和whereis是查找linux命令的工具,只能到PATH变量定义的路径下查找,只能找到命令,精确查找
2.locate  是从整个linux系统里找的,能查到命令和文件,但是缺点是查找新文件,有时候找不到,需要更新数据库(命令为:updatedb)。模糊查找,到mlocate.db里查找

6、find⭐⭐⭐

6.1、find + 范围 +  -name

[root@fttsaxf ~]# find / -name hello.c  # 在根目录(/)下精确查找
/sanchuang/hello.c
[root@fttsaxf ~]# find / -name hello
/sanchuang/hello
[root@fttsaxf ~]# find / -name "hello*"  # 这是在根目录下边模糊查找以hello开头的文件
/boot/grub2/i386-pc/hello.mod
/usr/lib/grub/i386-pc/hello.mod
/usr/lib/golang/doc/play/hello.go
/sanchuang/hello.c
/sanchuang/hello
/go/hellogo.go
/go/hellogo
# 根目录这里可以使用绝对路径,也可以使用相对路径

要用""把要查找的东西引用起来,如果没有引用起来且当前目录下有hello,则会报错

6.2、find + 范围 + -iname 按文件名查找,不区分大小写

[root@fttsaxf lianxi]# ls
backup.sh  emmm  EMMM  flshs  h  hh  lianxi  python-test  script
[root@fttsaxf lianxi]# find . -name "emmm"
./emmm
[root@fttsaxf lianxi]# find . -iname "emmm"
./emmm
./EMMM

6.3、find + 范围 + -size 按照文件大小查找

[root@fttsaxf lianxi]# find / -size 10M  #这是查找文件等于10M的文件
find: ‘/proc/2316/task/2316/fd/6’: 没有那个文件或目录
find: ‘/proc/2316/task/2316/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/2316/fd/5’: 没有那个文件或目录
find: ‘/proc/2316/fdinfo/5’: 没有那个文件或目录
/usr/lib/firmware/qcom/sdm845/adsp.mbn

#上面错误信息:这样是因为这是一些进程产生的文件是放在(proc)中,有些时候找到这里的时候,这个进程就没有了,所以会出现一些问题
# +10M是查找大于10M的文件,-10M是查找小于10M的文件

[root@fttsaxf lianxi]# find / -size 10M 2>/dev/null
/usr/lib/firmware/qcom/sdm845/adsp.mbn

# 2> 是错误的输出重定向  这个会覆盖原内容  >>2错误的追加输出重定向  这个不会覆盖原内容
[root@fttsaxf lianxi]# jklasgka
-bash: jklasgka: 未找到命令
[root@fttsaxf lianxi]# jklasgka 2>1.txt  # 把错误的内容写到1.txt中
[root@fttsaxf lianxi]# cat 1.txt 
-bash: jklasgka: 未找到命令

6.4、-name 和 -size组合使用

# -name 和 -size 的位置可以交换
[root@fttsaxf dev]# find /boot -name "*linuz*"  -size +3M
/boot/vmlinuz-3.10.0-1160.el7.x86_64
/boot/vmlinuz-0-rescue-e0bd1fb5180d428188b15b589e4e6244

6.5、多条件组合

默认情况下-a的优先级比-o的优先级高

6.5.1、-and;-a   并且

[root@fttsaxf dev]# find /boot -name "*linuz*"  -and -size +3M
/boot/vmlinuz-3.10.0-1160.el7.x86_64
/boot/vmlinuz-0-rescue-e0bd1fb5180d428188b15b589e4e6244

6.5.2、-or ;-o  或者

[root@fttsaxf dev]# find /boot -name "*linuz*"  -or -size +3M
/boot/.vmlinuz-3.10.0-1160.el7.x86_64.hmac
/boot/System.map-3.10.0-1160.el7.x86_64
/boot/vmlinuz-3.10.0-1160.el7.x86_64
/boot/initramfs-0-rescue-e0bd1fb5180d428188b15b589e4e6244.img
/boot/vmlinuz-0-rescue-e0bd1fb5180d428188b15b589e4e6244
/boot/initramfs-3.10.0-1160.el7.x86_64.img
/boot/initramfs-3.10.0-1160.el7.x86_64kdump.img

6.5.3、-not  逻辑非;取反

在命令中可用"!"表示、该运算符表示查找不满足所给条件的文件

# !只对后边的命令有用
[root@fttsaxf ~]# find /home -user fdd ! -type f
/home/fdd
/home/fdd/f
/home/fdd/feng
/home/fdd/dai
/home/fdd/hua

6.5.4、改变优先级

# ()为改变优先级,但是find里边使用()会报错,所以这里使用\,\为转义
[root@fttsaxf ~]# find . -user root -type f \( -size +1M -o -name "vmlinuz" \)
./coreutils-9.0.tar.xz
./coreutils-9.0/Makefile.in
./coreutils-9.0/configure
./.cache/pip/http/c/2/0/f/9/c20f9521a82f9cef0884e8b05175075ef3bae0dde15ae558dab0c22b
./.cache/pip/http/3/2/1/8/4/3218494306922593478bc48392c18b15782baf0d73f19d4c0af1cca4

6.6、find + 范围 + -type + 文件类型

文件类型:

        1、f  类型为文件  file
        2、d 类型为文件夹(目录) directory
        3、l 类型为链接文件  link
        4、b 类型为块设备文件  block
        5、c 类型为字符设备文件  character
        6、p 类型为管道文件  pipe
        7、s 类型为socket文件

[root@fttsaxf lianxi]# find . -type d
.
./.nongda
./script
./script/lucky_draw
./script/delete_create_file.sh
./script/for_while.sh
./lianxi
./python-test

6.7、find + 范围 + -mtime 

 时间有3个时间:

6.7.1、-mmin   以分钟为单位

[root@fttsaxf lianxi]# touch xixi
[root@fttsaxf lianxi]# find . -mmin -1 -type f # 查找当前目录下一分钟前创建的文件
.
./xixi

6.8、find + 范围 + -user + 用户

可以查询到该用户创建的文件

6.9、find + 范围 + 查找条件 + 动作(-exec;-ok)⭐⭐⭐

6.9.1、动作(-exec)

将find查找的结果交给-exec后面的命令执行

查找出文件名叫做helllo.c的文件,复制到/backup目录下

[root@fttsaxf ~]# mkdir -p /backup
[root@fttsaxf ~]# cd /backup/
[root@fttsaxf backup]# ls
[root@fttsaxf backup]# find / -name hello.c -type f -exec cp {} /backup \;

解析命令:

        find / -name hello.c -type f 到根目录下查找名字是hello.c的文件
        -exec  需要执行后边的命令  execute 执行
        cp  {}  /backup  执行cp命令 
        {}  代表前面find找到的内容,相当于一个容器,存放前面find找到的内容,没有这个{}会报错
        \;   是find命令的结束符号

查找出/root/ lianxi目录下的sc.txt文件,然后删除

[root@fttsaxf lianxi]# touch sc.txt
[root@fttsaxf lianxi]# ls
1.txt  backup.sh  emmm  EMMM  flshs  h  hh  lianxi  python-test  script  sc.txt  xixi
[root@fttsaxf lianxi]# find /root/lianxi/ -name sc.txt -exec rm -rf {} \;
[root@fttsaxf lianxi]# ls
1.txt  backup.sh  emmm  EMMM  flshs  h  hh  lianxi  python-test  script  xixi

查找出当前文件下以test开头的文件,把它们命名成test***backup

[root@fttsaxf rough_book]# ls
find  test1  test2
[root@fttsaxf rough_book]# find . -name "test*" -exec mv {} {}.backup \;
[root@fttsaxf rough_book]# ls
find  test1.backup  test2.backup

把上面这些文件都复制到别的文件下(使用xargs命令)

[root@fttsaxf rough_book]# ls find
gbk.txt  hhh  passwd  utf-8.txt
# xargs  配合管道符号一起使用,将前一条命令的输出,作为后一条命令的参数传入
# -i,前边一条命令的n个输出,逐个传入后边的命令去执行
[root@fttsaxf rough_book]# find . -name "test*" |xargs -i cp {} find
[root@fttsaxf rough_book]# ls find
gbk.txt  hhh  passwd  test1.backup  test2.backup  utf-8.txt

6.9.2、动作(-ok)

[root@fttsaxf lianxi]# touch sc.txt
[root@fttsaxf lianxi]# find /root/lianxi/ -name sc.txt -ok rm -rf {} \;
< rm ... /root/lianxi/sc.txt > ? y
[root@fttsaxf lianxi]# ls
1.txt  backup.sh  emmm  EMMM  flshs  h  hh  lianxi  python-test  script  xixi

6.10、-maxdepth 和 -mindepth

查找文件的时候,目录的深度

        1.代表当前
        2.代表下一级目录
        3.下一级的下一级目录,以此类推

# -maxdepth 只能放在这个位置,不能放在后边
[root@fttsaxf lianxi]# find . -maxdepth 1 -name hello
./hello
[root@fttsaxf lianxi]# find . -maxdepth 2 -name hello
./aa/hello
./hello
[root@fttsaxf lianxi]# find . -maxdepth 3 -name hello
./aa/bb/hello
./aa/hello
./hello
[root@fttsaxf lianxi]# find . -maxdepth 4 -name hello
./aa/bb/cc/hello
./aa/bb/hello
./aa/hello
./hello

6.11、-newr

按比某个文件时间更新的查找

[root@fttsaxf script]# ll
总用量 0
drwxr-xr-x. 2 root root  89 12月 15 12:40 delete_create_file.sh
drwxr-xr-x. 2 root root  36 12月 15 12:43 for_while.sh
drwxr-xr-x. 2 root root 106 12月 15 12:44 lucky_draw
[root@fttsaxf script]# find . -newer for_while.sh/
./lucky_draw

7、read

[root@fttsaxf ~]# read -p "请输入呆猪的名字:" name 
请输入呆猪的名字:fdd
[root@fttsaxf ~]# echo $name
fdd

read 是shell编程里接受用户输入的命令--》相当于python里的input函数
-p "请输入用户名":是提示用户的字符串
name 变量名,用来存储用户输入的内容

8、小练习

#1.在根目录下新建一个目录叫find
[root@fttsaxf /]# mkdir find

# 2.进入find目录,复制/etc/passwd  到当前目录
[root@fttsaxf find]# cp /etc/passwd .

# 3.复制/boot目录到当前目录下
[root@fttsaxf find]# cp /boot/ . -r

# 4.新建一个目录叫hunantv
[root@fttsaxf find]# mkdir hunantv

# 5.新建一个空文件叫daydayup
[root@fttsaxf find]# touch daydayup

# 6.查找mkdir和touch命令的路径
[root@fttsaxf find]# which mkdir
[root@fttsaxf find]# which touch

# 7.使用locate命令查找出mount文件的路径
[root@fttsaxf find]# locate mount

# 8.使用find命令查找出/find目录下大小大于1M且名字里包含vmlinuz的文件
[root@fttsaxf find]# find /find -size +1M -name "*vmlinuz*" -type f

# 9.使用find命令查找出/find目录下小于4M的文件
[root@fttsaxf find]# find /find -size -4M -type f

# 10.查找出你linux系统里包含rhel*.iso的文件并且此文件大小大于2G
[root@fttsaxf find]# find / -size +2G -type f -iname "rhel*.isoc"

# 11.查找hello.c的文件复制到/find目录下
[root@fttsaxf find]# find / -iname "hello.c" -tyep f -exec cp {} /find \; 

# 12.查找出daydayup的文件然后删除它
[root@fttsaxf find]# find -name daydayup -type f -exec rm -rf {} \;

# 13.将最近3小时内/lian目录下文件大小大于10k的文件,移动到/back目录下(这个back目录要存在)
[root@fttsaxf back]# find /lianxi -tyep f -mmin -180 +10K -exec mv {} /back \;

# 14.新建用户califeng、cali123、然后复制/boot/vmlinuz开头的文件到/home目录下,再复制
# /var/log/messages到/home目录下改名为cali789,然后查找/home目录下用户是root,文件类型
# 是f,这2个条件必须满足,然后再满足大小大于2k或者文件名包含cali的文件中的一个条件,查找出来
# ,复制到/lianxi目录下
[root@fttsaxf back]# useradd califeng
[root@fttsaxf back]# useradd cali123
[root@fttsaxf home]# cp /var/log/messages /home/cali789
[root@fttsaxf home]# find /home -user root -type f \( -size +2k -o -name "*cali*" \) -exec cp {} /lianxi \;
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FanMY_71

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值