Shell-2-命令之乐

时间:2022-04-22
本文章向大家介绍Shell-2-命令之乐,主要内容包括1.cat、2.录制并回放终端回话(script/scriptreplay)、3.文件查找与文件列表(find)、4.玩转xargs、5.用tr进行转换、6.检验与核实、7.加密工具与散列、8.排序、唯一与重复、9.分割文件和数据、10.根据扩展名切分文件名、11.批量重命名和移动、12.交互输入自动化、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

1.cat

(1)基本用法

[root@cai tmp]# cat 1.txt  2.txt 
this is a test1
this is a test 2

(2)cat -s file(删除额外空白行)

[root@cai tmp]# cat 3.txt 
a





b



c

d
[root@cai tmp]# cat -s 3.txt 
a

b

c

d

(3)cat -n lines.txt (显示行号)

[root@cai tmp]# cat -s -n 3.txt 
     1    a
     2    
     3    b
     4    
     5    c
     6    
     7    d

2.录制并回放终端回话(script/scriptreplay)

(1)[root@cai tmp]# script -t 2>timing.log -a output.session(录制)
Script started, file is output.session
(2)[root@cai tmp]# scriptreplay timing.log output.session (回放)

3.文件查找与文件列表(find)

(1)find base_path(顺着目录,向下查找)

[root@cai tmp]# find /tmp/
/tmp/
/tmp/.ICE-unix
/tmp/output.session
/tmp/Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>
/tmp/1.txt
/tmp/2.txt
/tmp/3.txt
/tmp/test.txt

(2)find . -iname "*.txt" -print(忽略要查找内容大小写)

[root@cai tmp]# find . ( -name "*.txt" -o -name "*.log" )
./1.txt
./2.txt
./3.txt
./test.txt

(3)find / -path "*/cairui/*"

[root@cai tmp]# find / -path "*/cairui/*"
/home/cairui/tools
/home/cairui/tools/nginx-1.12.0.tar.gz
/home/cairui/tools/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
/home/cairui/tools/nginx-1.12.0
/home/cairui/tools/nginx-1.12.0/Makefile
/home/cairui/tools/nginx-1.12.0/man
/home/cairui/tools/nginx-1.12.0/man/nginx.8

(4)否定参数(!)

[root@cai tmp]# ls
1.txt  Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>  test3.txt
2.txt  output.session                                      timing.log
[root@cai tmp]# find . ! -name "*.txt"(打印出不是以.txt结尾的文件)
.
./.ICE-unix
./timing.log
./output.session
./Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>

(5)基于目录深度的搜索(maxdepth)

find . -maxdepth 1 -name “f*” -print

[root@cai tmp]# find / -maxdepth 3 -path "*/cairui/*"
/home/cairui/tools
/home/cairui/.bash_profile
/home/cairui/.bash_logout
/home/cairui/.bashrc

(6)根据文件类型搜索

[root@cai tmp]# find . -type d -print(只列出所有的目录)
.
./.ICE-unix

[root@cai tmp]# find . -type f (只列出文件)
./timing.log
./output.session
./test3.txt
./1.txt
./2.txt

文件类型

参数类型

普通文件

f

符号链接

l

目录

d

字符设备

c

块设备

b

套接字

s

FIFO

p

(7)根据文件时间进行搜索

访问时间(-atime):用户最近一次访问文件的时间。

修改时间(-mtime):文件内容最后一次被修改的时间。

变化时间(-ctime):文件元数据(例如权限和所有权)最后一次改变的时间。

[root@cai tmp]# find . -type f -atime -7 (打印出最近7天内访问过的所有文件)
./timing.log
./output.session
./test3.txt
./1.txt
./2.txt

[root@cai tmp]# find . -type f -atime 7 (正好在7天前那天访问的文件)
[root@cai tmp]# find . -type f -atime +7 (访问时间超过7天的所有文件)

上面是按照天计算的,同样有按分钟计算的

-amin(访问时间)

-mmin(修改时间)

-cmin(变化时间)

(8)基于文件大小的搜索

find . -type f -size +2k(大于2K的文件)

find . -type f -size -2k(小于2k的文件)

find . -type f -size 2k(等于2k的文件)

除了k,还有其他的

b---块(512字节)

c---字节

w---字(2字节)

k---1024字节

M---1024k字节

G---1024M字节

(9)删除匹配的文件(-delete)

find . type f -name “*.txt” -delete

(10)利用find执行命令或动作(-exec)

find .type f -user root -exec chown slynux {} ;

find . type f -name “*.c” -exec cat {} ;>all_c_files.txt

4.玩转xargs

 擅长将标准输入数据转换成命令行参数。

(1)将多行输入转换成单行输出

[root@cai tmp]# cat ex.txt 
123456
789
00
[root@cai tmp]# cat ex.txt |xargs
123456 789 00

(2)将单行输入转换为多行输出

[root@cai tmp]# cat ex.txt |xargs -n 2
123456 789
00

(3) 

[root@cai tmp]# cat ex.txt 
splitXsplitXsplitX


[root@cai tmp]# cat ex.txt |xargs -d X(用自己的分隔符来分割参数)
split split split 

5.用tr进行转换

tr [option] test1 test2
(1)[root@cai tmp]# echo "HELLO WORLD" |tr 'A-Z' ‘a-z’

(2)[root@cai tmp]# echo 12345|tr '0-9' '9876543210'(加密)
87654
[root@cai tmp]# echo 87654|tr '9876543210' '0-9'(解密)
12345

(3)删除字符(tr -d)
[root@cai tmp]# echo "hello 122 world 55"|tr -d '0-9'
hello  world 

(4)用tr压缩字符(tr -s)
[root@localhost ~]# echo "cairui    is    so  handsome " | tr -s ' '
cairui is so handsome


[root@localhost tmp]# cat test.txt 
1
2
3
4
5
[root@localhost tmp]# cat test.txt | echo $[ $(tr 'n' '+' ) 0 ]
15

6.检验与核实

(1)

[root@localhost tmp]# md5sum test.txt 
a7b1ac3a2b072f71a8e0d463bf4eb822  test.txt

如上所示,md5sum是一个32个字符的十六进制串

将输出的检验和重定向到一个文件,然后用md5文件核实数据的完整性

(2)

[root@localhost tmp]# md5sum -c test_sum.md5 
test.txt: 确定(检验是否匹配)                              

7.加密工具与散列

 (1)

crypt
crypt <input_file >output_file
ENTER passphrase
(2)gpg
[root@localhost tmp]# gpg -c test.txt(加密,保证在传输过程中无法读取)

[root@localhost tmp]# gpg test.txt(解密)

8.排序、唯一与重复

(1)
[root@localhost tmp]# sort 1.txt 2.txt > sorted.txt
[root@localhost tmp]# cat sorted.txt 
123
33444
566
dfzcx
dq
kdfi
[root@localhost tmp]# cat 1.txt 
123
33444
566
[root@localhost tmp]# cat 2.txt 
kdfi
dfzcx
dq

 (2)按照数字顺序进行排序

[root@localhost tmp]# sort -n 1.txt 
111
123
566
777
33444

(3)按照逆序进行排序

[root@localhost tmp]# sort -r 1.txt 
777
566
33444
123
111

(4)按照月份进行排序(依照一月、二月、三月、、、、)

sort -M months.txt

(5)按照两个已排序过的文件

sort -m sorted1 sorted2

(6)找出已排序文件中不重复的行

sort file.txt file2.txt |uniq

(7)检查文件是否已经排序过

#!/bin/bash
#function:paixu
sort -C filename;
if [ $? eq 0 ]; then 
echo sorted;
else
echo unsorted;
fi

9.分割文件和数据

split -b 10k data.file
ls
data.file xaa xab xac.......

10.根据扩展名切分文件名

#!/bin/bash
#function:根据扩展名切分文件名
file_jpg="sample.jpg"
name=${file_jpg%.*}  (name=${file_jpg#*.})
echo file name is:$name
[root@localhost shell]# sh sample.sh 
file name is:sample

11.批量重命名和移动

#!/bin/bash
#用途:重命名.jpg和.png文件

count=1;
for img in ‘find . -iname ‘*.png’ -o -iname ‘*.jpg’ -type f -maxdepth 1’
do
  new=image-$count.${img##*.}
  echo “renaming $img to $new”
  mv “$img” “$new”
  let count++
done

12.交互输入自动化

#!/bin/bash
read -p "enter number:" no;
read -p "enter name:" name
echo you have entered $no,$name
[root@localhost shell]# echo -e "1nhellon" | ./interactive.sh
you have entered 1,hello