《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

相关文章
|
3天前
|
监控 Linux 应用服务中间件
探索Linux中的`ps`命令:进程监控与分析的利器
探索Linux中的`ps`命令:进程监控与分析的利器
|
1天前
|
Linux
Linux的top命令是什么,如何使用
【6月更文挑战第30天】Linux的top命令是什么,如何使用
6 1
|
1天前
|
Linux 数据处理
探索Linux下的readelf命令:深入了解ELF文件
`readelf`是Linux下分析ELF文件的命令行工具,用于查看文件头、节区、符号表等信息。支持可执行文件、共享库等多种类型。常用选项有`-h`(文件头)、`-l`(程序头)、`-S`(节区)、`-s`(符号表)、`-r`(重定位)和`-d`(动态节区)。结合其他工具如`objdump`,能深入理解二进制文件,助力开发和调试。
|
2天前
|
Shell
蓝易云 - 简单shell脚本的编写教程
以上就是编写一个基本Shell脚本的步骤。当然,Shell脚本可以做的远不止这些,你可以使用变量,控制结构(如if语句和循环),以及各种Shell命令和功能来编写更复杂的脚本。
11 1
|
3天前
|
IDE Linux 数据处理
探索Linux中的`pydoc`命令:Python文档生成器的力量
`pydoc`是Linux上Python的文档生成和查看工具,尤其对数据科学家有价值。它从docstring生成模块、函数和类的文档,提供快速API参考。主要特点包括易用性、支持标准库和第三方库、跨平台。命令行示例:`pydoc pandas` 查看库文档,`pydoc numpy.array` 查看类详情,`pydoc -k 关键字` 进行搜索。使用时注意正确安装Python,编写清晰的docstring,并结合IDE以提升效率。
|
3天前
|
存储 算法 安全
深入理解Linux命令pwscore:密码质量的守护者
**pwscore命令详解:Linux密码强度评估工具** pwscore是Linux下的密码强度检查工具,分析密码长度、字符类型及避免常见模式来评分。它提供简单语法、可定制选项和高效评估。例如,`pwscore -l 12 -m alnum`评估至少含12个字符和字母数字的密码。应用时,定期评估用户密码,制定强密码策略,避免常见单词和模式,使用密码管理器,并保护输出信息安全,以增强系统安全性。
|
3天前
|
Web App开发 运维 监控
深入探索Linux命令pwdx:揭秘进程工作目录的秘密
`pwdx`命令在Linux中用于显示指定进程的工作目录,基于`/proc`文件系统获取实时信息。简单易用,如`pwdx 1234`显示PID为1234的进程目录。结合`ps`和`pgrep`等命令可扩展使用,如查看所有进程或特定进程(如Firefox)的目录。使用时注意权限、进程ID的有效性和与其他命令的配合。查阅`man pwdx`获取更多帮助。
|
1天前
|
Linux 数据处理
探索Linux下的readlink命令:解析符号链接的利器
`readlink`命令在Linux中用于揭示符号链接的指向,显示它们所链接的实际文件或目录的路径。它可以显示简洁的绝对路径(-f),处理循环链接(-e),或不加换行符输出(-n)。例如,查看`link.txt`指向:`readlink link.txt`;获取绝对路径:`readlink -f link.txt`。使用时要注意链接是否存在、权限问题和可能的循环链接。
|
1天前
|
Linux
常用的Linux系统命令及其使用技巧
常用的Linux系统命令及其使用技巧
|
2天前
|
Shell 调度
Shell脚本中的if条件判断语句
Shell脚本中的if条件判断语句