《Linux Shell脚本攻略》 笔记 第二章:常用命令

简介: 《Linux Shell脚本攻略》 笔记

第二章:常用命令

1、cat

     cat -s //多个空白行压缩成一个 
     cat *.txt | tr -s '\n'   //移除空白行
     cat -n //加行号

2、find
沿着文件层次结构向下遍历,匹配符合条件的文件,并执行相应的操作。
eg:

find ./ ! -name "*.txt" -print
[root@localhost program_test]# find ./  -type f -name "*.swp" -delete

3、xargs 将标准输入数据转换成命令行参数。

[root@localhost program_test]# cat out.txt | xargs  //将单行转化为多行

[root@localhost program_test]# cat out.txt | xargs -n 3 //将单行转化为多行,每行的个数为3.

//统计.c和.cpp的文件的代码行数.  xargs -0 将'\0'作为定界符.
[root@localhost program_test]# find . -type f -name "*.c*" -print0 | xargs -0 wc -l
  10 ./main/cos_value.c
  10 ./main/sin_value.c
   5 ./main/haha.c
  15 ./main/main.c
   8 ./hello.cpp
   8 ./sin.c
  32 ./review2.cpp
  24 ./review5.cpp
   7 ./hello.c
119 total

4.tr命令(translate的简写) 可对来自标准输入的字符进行替换、删除及压缩。

即:将一组字符变为另一组字符。

1)替换

echo “HELLO” | tr [A-Z] [a-z]

2)删除

[root@localhost program_test]# echo "hello 123 world 456" | tr -d '0-9'
hello  world

3)压缩字符

[root@localhost program_test]# echo "GNU is     not UNIX" | tr -s ' '
GNU is not UNIX
//综合举例 
[root@localhost program_test]# cat sum.txt
1
2
3
4
5
[root@localhost program_test]# cat sum.txt | echo $[ $(tr '\n' '+') 0 ]
15

5、md5校验

[root@localhost program_test]# md5sum out.txt > out.txt.md5
[root@localhost program_test]# cat out.txt.md5
fd46d559bf0c90170fef3da3c3de4c67  out.txt
//eg:
[root@localhost program_test]# find ./ 
-type f -name "*.txt" -print0 | xargs -0 md5sum >> curr_dir.md5
46e17910faf509df48dbfba9729ef018  ./banana.txt
c1dbbf63209a5580c052dc557510e7fb  ./11.txt
a80ddf02fa3a86c14066204e4bf2dbb9  ./multiline.txt
[root@localhost program_test]# md5sum -c curr_dir.md5
./banana.txt: OK
./11.txt: OK
./multiline.txt: OK

6、sort排序

//sort 按第2列排序
[root@localhost program_test]# sort -k 2 sort.txt
4 bad 5000
3  linux 50
1   mac 2000
2  winxp 100
//sort 逆序排序
[root@localhost program_test]# sort -r sort.txt
4 bad 5000
3  linux 50
2  winxp 100
1   mac 2000

//综合举例:统计字符串中每个字符出现的次数

[root@localhost program_test]# ./total_cnts.sh
AHEBHAAA
4A1B1E2H
[root@localhost program_test]# cat total_cnts.sh
INPUT="AHEBHAAA"
output=$(echo $INPUT | sed 's/[^.]/&\n/g' | sed '/^$/d' | sort | uniq -c | tr -d ' \n')
echo $INPUT
echo $output

拆分如下:

[root@localhost program_test]# input="ahebhaaa"
[root@localhost program_test]# echo $input | sed 's/[^.]/&\n/g'
a
h
e
b
h
a
a
a

 sed '/^$/d'  //删除空行
uniq -c         //统计各行文本出现的次数.
tr -d '\n  '     //删除换行符以及空格字符

7、临时文件命名

[root@localhost program_test]# temp_file="/tmp/var.$$"
[root@localhost program_test]# echo $temp_file
/tmp/var.16565

作者:铭毅天下

转载请标明出处,原文地址:http://blog.csdn.net/laoyang360/article/details/42364751

相关文章
|
1天前
|
Linux Shell
10-10|linux命令查询 关键字在文本中出现的行数
10-10|linux命令查询 关键字在文本中出现的行数
|
1天前
|
存储 Linux Shell
常用vim命令和vim基本使用及Linux用户的管理,用户和组相关文件
这篇文章介绍了Vim编辑器的基本使用、常用命令和模式,以及Linux系统中用户和组的管理方法,包括用户和组相关文件如/etc/passwd、/etc/shadow和/etc/group的说明。
常用vim命令和vim基本使用及Linux用户的管理,用户和组相关文件
|
2天前
|
Linux Docker 容器
linux之docker命令
linux之docker命令
|
3天前
|
人工智能 监控 Shell
常用的 55 个 Linux Shell 脚本(包括基础案例、文件操作、实用工具、图形化、sed、gawk)
这篇文章提供了55个常用的Linux Shell脚本实例,涵盖基础案例、文件操作、实用工具、图形化界面及sed、gawk的使用。
14 2
|
3天前
|
Ubuntu Java Linux
LINUX常用命令总结_持续更新
本文汇总了多种Linux命令的使用方法,包括系统信息查询、文件与目录管理、打包与压缩、软件包管理、文件系统分析、备份操作及网络配置等。例如,使用`df -h`检查文件系统使用情况,`uname -a`查看内核版本,`ps -ef|grep java`查看Java服务,`kill -9 pid`结束进程,以及使用`tar`、`zip`、`unzip`进行文件压缩与解压。此外,还介绍了系统关机、重启、登录注销、文件权限设置、磁盘空间管理、RPM与DEB包安装更新、文本处理、文件系统初始化与修复、SWAP文件系统管理、光盘刻录与挂载、网络接口配置等实用命令。
|
7天前
|
监控 Linux
Linux系统中du命令与df命令的区别与用法
总的来说,`du` 和 `df` 在磁盘管理中互补使用,能够提供全面的磁盘空间使用信息,帮助用户和管理员有效地监控和管理系统资源。
18 3
|
1天前
|
Linux Docker 容器
9. 同步执行Linux多条命令
9. 同步执行Linux多条命令
|
1天前
|
Ubuntu Linux Shell
10-21|在linux我如果修改了时间,什么命令可以恢复正确啊
10-21|在linux我如果修改了时间,什么命令可以恢复正确啊
|
1天前
|
Linux Perl
6-20|linux sed命令
6-20|linux sed命令
|
3天前
|
Shell Linux Python
python执行linux系统命令的几种方法(python3经典编程案例)
文章介绍了多种使用Python执行Linux系统命令的方法,包括使用os模块的不同函数以及subprocess模块来调用shell命令并处理其输出。
10 0