Shell-2-命令之乐
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 .txt
a b c d
[root@cai tmp]# cat -s .txt
a b c d
(3)cat -n lines.txt (显示行号)
[root@cai tmp]# cat -s -n .txt
a b c d
2.录制并回放终端回话(script/scriptreplay)
()[root@cai tmp]# script -t >timing.log -a output.session(录制)
Script started, file is output.session
()[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--6765EDAD7CBA)>
/tmp/.txt
/tmp/.txt
/tmp/.txt
/tmp/test.txt
(2)find . -iname "*.txt" -print(忽略要查找内容大小写)
[root@cai tmp]# find . \( -name "*.txt" -o -name "*.log" \)
./.txt
./.txt
./.txt
./test.txt
(3)find / -path "*/cairui/*"
[root@cai tmp]# find / -path "*/cairui/*"
/home/cairui/tools
/home/cairui/tools/nginx-1.12..tar.gz
/home/cairui/tools/mysql-5.7.-linux-glibc2.-x86_64.tar.gz
/home/cairui/tools/nginx-1.12.
/home/cairui/tools/nginx-1.12./Makefile
/home/cairui/tools/nginx-1.12./man
/home/cairui/tools/nginx-1.12./man/nginx.
(4)否定参数(!)
[root@cai tmp]# ls
.txt Aegis-<Guid(5A2C30A2-A87D-490A--6765EDAD7CBA)> test3.txt
.txt output.session timing.log
[root@cai tmp]# find . ! -name "*.txt"(打印出不是以.txt结尾的文件)
.
./.ICE-unix
./timing.log
./output.session
./Aegis-<Guid(5A2C30A2-A87D-490A--6765EDAD7CBA)>
(5)基于目录深度的搜索(maxdepth)
find . -maxdepth -name “f*” -print [root@cai tmp]# find / -maxdepth -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
./.txt
./.txt
|
文件类型 |
参数类型 |
|
普通文件 |
f |
|
符号链接 |
l |
|
目录 |
d |
|
字符设备 |
c |
|
块设备 |
b |
|
套接字 |
s |
|
FIFO |
p |
(7)根据文件时间进行搜索
访问时间(-atime):用户最近一次访问文件的时间。
修改时间(-mtime):文件内容最后一次被修改的时间。
变化时间(-ctime):文件元数据(例如权限和所有权)最后一次改变的时间。
[root@cai tmp]# find . -type f -atime - (打印出最近7天内访问过的所有文件)
./timing.log
./output.session
./test3.txt
./.txt
./.txt [root@cai tmp]# find . -type f -atime (正好在7天前那天访问的文件)
[root@cai tmp]# find . -type f -atime + (访问时间超过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 [root@cai tmp]# cat ex.txt |xargs
(2)将单行输入转换为多行输出
[root@cai tmp]# cat ex.txt |xargs -n
(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
()[root@cai tmp]# echo "HELLO WORLD" |tr 'A-Z' ‘a-z’ ()[root@cai tmp]# echo |tr '0-9' ''(加密) [root@cai tmp]# echo |tr '' '0-9'(解密) ()删除字符(tr -d)
[root@cai tmp]# echo "hello 122 world 55"|tr -d '0-9'
hello world ()用tr压缩字符(tr -s)
[root@localhost ~]# echo "cairui is so handsome " | tr -s ' '
cairui is so handsome [root@localhost tmp]# cat test.txt [root@localhost tmp]# cat test.txt | echo $[ $(tr '\n' '+' ) ]
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
()gpg
[root@localhost tmp]# gpg -c test.txt(加密,保证在传输过程中无法读取) [root@localhost tmp]# gpg test.txt(解密)
8.排序、唯一与重复
()
[root@localhost tmp]# sort .txt .txt > sorted.txt
[root@localhost tmp]# cat sorted.txt dfzcx
dq
kdfi
[root@localhost tmp]# cat .txt [root@localhost tmp]# cat .txt
kdfi
dfzcx
dq
(2)按照数字顺序进行排序
[root@localhost tmp]# sort -n .txt
(3)按照逆序进行排序
[root@localhost tmp]# sort -r .txt
(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 ]; 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=;
for img in ‘find . -iname ‘*.png’ -o -iname ‘*.jpg’ -type f -maxdepth ’
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 "1\nhello\n" | ./interactive.sh
you have entered ,hello
Shell-2-命令之乐的更多相关文章
- (linux shell)第二章--命令之乐(一)
文章来自于我的个人博客:(linux shell)第二章--命令之乐(一) 上一章我们描写叙述了一些linux shell中须要注意的一些语法.接下来我们開始了解linux shell的经常使用 ...
- linux 基础 shell脚本命令
#########shell脚本命令#### 1.diff diff file file1 ####比较两个文件的不同 -c ####显示周围的行 -u ####按照一格式统一输出生成补丁 -r ## ...
- paip.执行shell cmd 命令uapi java php python总结
paip.执行shell cmd 命令uapi java php python总结 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:h ...
- (转)Hbase shell 常用命令(1)
Hbase shell 常用命令(1) link:http://blog.csdn.net/scutshuxue/article/details/6988348 下面我们看看HBase Shell的一 ...
- Shell printf 命令
Shell printf 命令 printf 命令模仿 C 程序库(library)里的 printf() 程序. 标准所定义,因此使用printf的脚本比使用echo移植性好. printf 使用引 ...
- Shell echo命令
Shell echo命令 echo "It is a test" 这里的双引号完全可以省略 .显示变量 read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shel ...
- 进入BIOS SHELL DUMP 命令
LINUX系统 进入SHELL 输入命令 fs1: or fs0: 就进入了U盘目录 然后输入 ACPIRW.efi -d -s dsdt.bat 就会产生结果到U盘 ——————————————— ...
- Linux Shell : Test命令参数解析
格式: test conditions test -n string : string 不为空 test -z string : string 为空 test int1 -eq int2 : int ...
- shell解析命令行的过程以及eval命令
本文说明的是一条linux命令在执行时大致要经过哪些过程?以及这些过程的大致顺序. 1.1 shell解析命令行 shell读取和执行命令时的大致操作过程如下图: 以执行以下命令为例: echo -e ...
- Shell和命令基础
什么是Shell Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口(命令解析器),Shell接收用户输入的命令并把它送入到内核去执行,结构如下图 Shell的功能 Shell最重要的 ...
随机推荐
- java代码分解质因数
总结: 循环...首位 逻辑要清晰 很简单.首先质因数最小的2.因为1不是质数 然后当输入的数刚好为2时,结束.不用下一步了.否则在循环里不停的输出一个数, 当输入的数比2大时,分能被2整除和不能被2 ...
- binlog之三:binlog开启、查看
0.开启二进制日志记录功能: #vim /etc/my.cnf [mysqld] log_bin=mysql-bin b ...
- 深入VR之前 你应该知道VR头显透镜原理
转自:http://www.gamelook.com.cn/2016/03/246817 要理解虚拟现实头显透镜的工作原理,首先要搞懂眼睛是如何看到事物的. 眼睛瞳孔后有晶状体,也就是眼珠子.眼睛的背 ...
- catkin 工作空间
catkin 工作空间:组织和管理功能包的文件夹,以 catkin 工具编译 建立工作空间 sch01ar@ubuntu:~$ mkdir -p ~/catkin_ws/src sch01ar@ubu ...
- 2016.5.30实现透明Panel及控件置顶的方法
想放置一个透明Panel在某控件上端,实现效果是可透过此Panel看见下面控件,但鼠标点击却无任何反应. 1.新建置自定义Panel类 using System; using System.Colle ...
- C#引用类库时出现黄色三角加感叹号的处理
C#引用类库时出现黄色三角加感叹号的处理方法 一个C#项目 在引用中有个引用项上有个黄色三角加感叹号 导致报错 类库的目标框架不一致,修改成一样就可以了. 选中类库右击属性:“目标框架”,修改成与引用 ...
- 问题:sqlserver有没有类似Oracle的LISTAGG;结果: 灵活运用 SQL SERVER FOR XML PATH
灵活运用 SQL SERVER FOR XML PATH FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前 ...
- latex如何进行多行注释
单行注释:当LATEX 处理一个源文件时,如果遇到一个百分号%,LATEX 将忽略% 后的该行内容,换11行符以及下一行前的空白字符.多行注释:\begin{comment}rather stupid ...
- 201671010127 2016—2017—2 Java学习周结
时间真是个最公平东西,只要能够好好地利用它,它可以为我们带来我们我们所想要的东西.学习Java已经有一周了,对于Java基础知识的认识也更进一步,对Java的兴趣也愈来愈浓.实现一个Java程序,主要 ...
- 机器人自主移动的秘密:SLAM与路径规划有什么关系?(三)
博客转载自:https://www.leiphone.com/news/201612/lvDXqY82OGNqEiyl.html 雷锋网(公众号:雷锋网)按:本文作者SLAMTEC(思岚科技公号sla ...