1、生成任意大小文件 dd
[root@stone ~]# dd if=/dev/zero of=f1_dd bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.004317 seconds, 243 MB/s

2、文本文件的交集和差集 comm
[root@stone ~]# cat num1
apple
orange
pear
banana
peach
strawberry
watermelon
mango
[root@stone ~]# cat num2
orange
banana
grape
cherry
[root@stone ~]# sort num1 -o num1
[root@stone ~]# cat num1
apple
banana
mango
orange
peach
pear
strawberry
watermelon
[root@stone ~]# sort num2 -o num2
[root@stone ~]# cat num2
banana
cherry
grape
orange
[root@stone ~]# comm num1 num2
apple
banana
cherry
grape
mango
orange
peach
pear
strawberry
watermelon

#comm输出的第一列表示只在num1中出现的行,第二列表示只在num2中出现的行,第三列表示在num1和num2中都出现的行(交集),各列以制表符(\t)作为分隔符。
[root@stone ~]# comm num1 num2 -1 -2
banana
orange

#-1,-2表示去掉第一列和第二列,显示两个文件的交集
[root@stone ~]# comm num1 num2 -3
apple
cherry
grape
mango
peach
pear
strawberry
watermelon

#-3表示去掉第三列,表示对两个文件求差
[root@stone ~]# comm num1 num2 -3 | sed 's/^\t//'
apple
cherry
grape
mango
peach
pear
strawberry
watermelon

#使用sed删除了第二列行首的制表符
[root@stone ~]# comm num1 num2 -2 -3
apple
mango
peach
pear
strawberry
watermelon

#显示num1的差集
3、查找并删除重复文件
使用到awk命令,学习了awk后再来看
4、创建长路径目录
[root@stone ~]# mkdir -p china/chongqing/beibei
[root@stone ~]# tree china
china
`-- chongqing
`-- beibei
2 directories, 0 files

5、文件权限,所有者和粘滞位
[root@stone file]# ll
total 24
-rw-r--r-- 1 root root   15 May 13 11:57 f1
-rw-r--r-- 1 root root    4 May 13 11:58 f2
drwxr-xr-x 2 root  root  4096 May 14 17:26 file1
[root@stone file]# chown stone.stone f1
#改变所有者
[root@stone file]# chmod +t file1
[root@stone file]# chmod 1755 file1/
#设置粘滞位
[root@stone file]# ll
total 24
-rw-r--r-- 1 stone stone   15 May 13 11:57 f1
-rw-r--r-- 1 root  root     4 May 13 11:58 f2
drwxr-xr-t 2 root  root  4096 May 14 17:26 file1

[root@stone file]# ll -R
.:
total 24
-rw-r--r-- 1 stone stone   15 May 13 11:57 f1
-rw-r--r-- 1 root  root     4 May 13 11:58 f2
drwxr-xr-t 2 root  root  4096 May 14 17:27 file1
./file1:
total 4
-rw-r--r-- 1 root root 0 May 14 17:27 f11
[root@stone file]# chmod 777 file1 -R
#递归更改权限
[root@stone file]# ll -R
.:
total 24
-rw-r--r-- 1 stone stone   15 May 13 11:57 f1
-rw-r--r-- 1 root  root     4 May 13 11:58 f2
drwxrwxrwx 2 root  root  4096 May 14 17:27 file1
./file1:
total 4
-rwxrwxrwx 1 root root 0 May 14 17:27 f11

[root@stone file]# chown stone.stone file1 -R
#递归更改所有者
[root@stone file]# ll -R
.:
total 24
-rw-r--r-- 1 stone stone   15 May 13 11:57 f1
-rw-r--r-- 1 root  root     4 May 13 11:58 f2
drwxrwxrwx 2 stone stone 4096 May 14 17:27 file1
./file1:
total 4
-rwxrwxrwx 1 stone stone 0 May 14 17:27 f11

6、创建不可修改文件 chattr
[root@stone file]# chattr +i f
#增加不可修改属性
[root@stone file]# ll
total 24
-rw-r--r-- 1 stone stone   15 May 13 11:57 f1

[root@stone file]# rm f1
rm: remove write-protected regular file `f1'? y
rm: cannot remove `f1': Operation not permitted

[root@stone file]# chattr -i f1
#移除不可修改属性
[root@stone file]# rm f1
rm: remove regular file `f1'? y

[root@stone file]# 

7、批量生成空白文件及更改时间戳 touch
[root@stone file]# touch f{1..9}
#批量生成文件
[root@stone file]# ls
f1  f2  f3  f4  f5  f6  f7  f8  f9 

[root@stone file]# stat f1
File: `f1'
Size: 0               Blocks: 8          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 2783591     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-05-15 08:48:42.000000000 +0800
Modify: 2013-05-15 08:48:48.000000000 +0800
Change: 2013-05-15 08:48:48.000000000 +0800
[root@stone file]# touch -a f1
#更改f1的访问时间为当前时间
[root@stone file]# stat f1
File: `f1'
Size: 0               Blocks: 8          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 2783591     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-05-15 08:49:16.000000000 +0800
Modify: 2013-05-15 08:48:48.000000000 +0800
Change: 2013-05-15 08:49:16.000000000 +0800
[root@stone file]# touch -m f1
#更改f1的修改时间为当前时间
[root@stone file]# stat f1
File: `f1'
Size: 0               Blocks: 8          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 2783591     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-05-15 08:49:16.000000000 +0800
Modify: 2013-05-15 08:49:43.000000000 +0800
Change: 2013-05-15 08:49:43.000000000 +0800

[root@stone file]# touch -t "201305011230" f1
#更改f1的访问时间和修改时间为指定时间,-t后面的时间格式固定
[root@stone file]# stat f1
File: `f1'
Size: 0               Blocks: 8          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 2783591     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-05-01 12:30:00.000000000 +0800
Modify: 2013-05-01 12:30:00.000000000 +0800
Change: 2013-05-15 08:51:34.000000000 +0800

[root@stone file]# touch -d "2012/12/20" f1
#更改f1 的访问时间和修改时间为指定时间,-d后面的时间格式不任何标准日期格式
[root@stone file]# stat f1
File: `f1'
Size: 0               Blocks: 8          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 2783591     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-12-20 00:00:00.000000000 +0800
Modify: 2012-12-20 00:00:00.000000000 +0800
Change: 2013-05-15 08:54:39.000000000 +0800

8、符号链接
[root@stone ~]# ln -s f1 f1_s
#创建符号链接
[root@stone ~]# ll f1 f1_s
-rw-r--r-- 1 root root 0 May 15 09:01 f1
lrwxrwxrwx 1 root root 2 May 15 09:01 f1_s -> f1
[root@stone ~]# find . -type l
./f1_s
#找出当前目录下所有的符号链接文件
[root@stone ~]# readlink f1_s
f1

#找出符号链接文件的源文件
9、文件类型 file
[root@stone ~]# file num
num: ASCII text
#列出文件名称和类型
[root@stone ~]# file -b num
ASCII text

#只列出文件类型
统计目录下文件类型脚本:
[root@stone ~]# vim bin/filestat.sh 
1 #!/bin/bash
2 path=$1
3 fileline=`find $path|wc -l`
4 echo "the total file number is: $fileline"
5 filedir=`find $path -type d | wc -l`
6 echo "the total dir number is: $filedir"
7 echo "the other file number is: $[fileline-filedir]"
8 rm -rf /tmp/filestat
9 touch /tmp/filestat
10 for filename in `find $path`
11 do
12   filetype=`file -b $filename`
13   echo $filetype >> /tmp/filestat
14 done
15 cat /tmp/filestat | sort | uniq -c | sort -rn

10、生成ISO文件
[root@stone ~]# cat /dev/cdrom > p_w_picpath.iso
[root@stone ~]# dd if=/dev/cdrom of=p_w_picpath.iso
11、文件比较及补丁 diff patch
[root@stone ~]# cat num1
apple
banana
mango
orange
peach
pear
strawberry
watermelon
[root@stone ~]# cat num2
banana
cherry
grape
orange
[root@stone ~]# diff num1 num2
1d0
< apple
3c2,3
< mango
---
> cherry
> grape
5,8d4
< peach
< pear
< strawberry
< watermelon
#比较两个文件的差异
[root@stone ~]# diff -c num1 num2
*** num1        2013-05-15 14:31:14.000000000 +0800
--- num2        2013-05-14 15:58:28.000000000 +0800
***************
*** 1,8 ****
- apple
banana
! mango
orange
- peach
- pear
- strawberry
- watermelon
--- 1,4 ----
banana
! cherry
! grape
orange

[root@stone ~]# diff -u num1 num2
--- num1        2013-05-14 15:58:11.000000000 +0800
+++ num2        2013-05-14 15:58:28.000000000 +0800
@@ -1,8 +1,4 @@
-apple
banana
-mango
+cherry
+grape
orange
-peach
-pear
-strawberry
-watermelon
#-u表示使用一体化格式比较两个文件的差异
[root@stone ~]# diff -u num1 num2 > num.patch
#生成补丁文件
[root@stone ~]# cat num.patch 
--- num1        2013-05-14 15:58:11.000000000 +0800
+++ num2        2013-05-14 15:58:28.000000000 +0800
@@ -1,8 +1,4 @@
-apple
banana
-mango
+cherry
+grape
orange
-peach
-pear
-strawberry
-watermelon
[root@stone ~]# patch -p1 num1 < num.patch 
missing header for unified diff at line 3 of patch
patching file num1
#将补丁文件应于与num1,使num1与num2一样
[root@stone ~]# cat num1
banana
cherry
grape
orange
[root@stone ~]# patch -p1 num1 <num.patch 
missing header for unified diff at line 3 of patch
patching file num1
Reversed (or previously applied) patch detected!  Assume -R? [n] y
#再次将补丁文件应用于num1,是num1恢复
[root@stone ~]# cat num1
apple
banana
mango
orange
peach
pear
strawberry
watermelon

[root@stone ~]# ls file
f1  f2  f3  f4  f5  f6  f7  f8  f9  file1
[root@stone ~]# ls file1
config.txt  f1  f1_h  f2  f2_s  hello

[root@stone ~]# diff -aur file file1
Only in file1: config.txt
diff -aur file/f1 file1/f1
--- file/f1     2012-12-20 00:00:00.000000000 +0800
+++ file1/f1    2013-02-25 17:33:01.000000000 +0800
@@ -0,0 +1,4 @@
+aaa
+a      a
+aaa
+, ,
Only in file1: f1_h
diff -aur file/f2 file1/f2
--- file/f2     2013-05-14 17:44:25.000000000 +0800
+++ file1/f2    2013-02-22 17:09:57.000000000 +0800
@@ -0,0 +1 @@
+aaa
Only in file1: f2_s
Only in file: f3
Only in file: f4
Only in file: f5
Only in file: f6
Only in file: f7
Only in file: f8
Only in file: f9
Only in file: file1
Only in file1: hello

#-a表示将所有文件视为文本文件
#-u表示一体化输出
#-r表示递归比较目录下的所有文件
12、打印文件前后行 head tail
[root@stone ~]# seq 5
1
2
3
4
5

#生成序列
[root@stone ~]# seq 5 | head -2
1
2
#输出前两行
[root@stone ~]# seq 5 | head -n 2
1
2
#输出前两行
[root@stone ~]# seq 5 | head -n +2
1
2
#输出前两行
[root@stone ~]# seq 5 | head -n -2
1
2
3
#输出除了最后两行之外的所有行
[root@stone ~]# seq 5 | tail -2
4
5
#输出后两行
[root@stone ~]# seq 5 | tail -n 2
4
5
#输出后两行
[root@stone ~]# seq 5 | tail -n -2
4
5
#输出后两行
[root@stone ~]# seq 5 | tail -n +2
2
3
4
5

#输出从第2行到最后
[root@stone ~]# tail -f /var/log/messages
May 15 14:03:39 stone smbd[20833]:   read_data: read failure for 4 bytes to client 172.16.3.56. Error = Connection reset by peer 
May 15 14:05:23 stone avahi-daemon[2892]: Invalid query packet.
May 15 14:06:04 stone last message repeated 9 times
May 15 16:44:09 stone smbd[21346]: [2013/05/15 16:44:09, 0] lib/util_sock.c:read_data(540) 
May 15 16:44:09 stone smbd[21346]:   read_data: read failure for 4 bytes to client 172.16.3.56. Error = Connection reset by peer 
May 16 03:00:19 stone avahi-daemon[2892]: Invalid query packet.
May 16 03:01:00 stone last message repeated 7 times
May 16 03:18:55 stone avahi-daemon[2892]: Invalid query packet.
May 16 06:59:46 stone last message repeated 8 times
May 16 08:12:40 stone last message repeated 8 times

#-f选项可以监视文件内容的变化,常用于实时查看日志
13、列出目录
[root@stone ~]# ll -d
drwxr-xr-x 30 root root 4096 May 15 14:31 .
#列出当前父目录
[root@stone ~]# ll -d */
drwxr-xr-x 5 1000 1000  4096 Mar  9 04:17 awstats-7.1.1/
drwxr-xr-x 2 root root  4096 May 15 11:06 bin/
drwxr-xr-x 3 root root  4096 May 14 17:03 china/
drwxr-xr-x 2 root root  4096 Feb  8 11:58 Desktop/
drwxr-xr-x 3 root root 28672 May 14 17:44 file/
drwxr-xr-x 2 root root  4096 May  8 17:17 file1/
drwxr-xr-x 3 root root  4096 Mar 25 11:09 logfile/
drwxr-xr-x 2 root root  4096 Feb 25 16:56 lvm0/
drwxr-xr-x 2 root root  4096 Mar  5 17:39 scripts/
drwxr-xr-x 2 root root  4096 May  8 15:50 sysconfig/

#列出当前目录下所有所有一级子目录
[root@stone ~]# ll -F | grep '/$'
drwxr-xr-x  5  1000  1000      4096 Mar  9 04:17 awstats-7.1.1/
drwxr-xr-x  2 root  root       4096 May 15 11:06 bin/
drwxr-xr-x  3 root  root       4096 May 14 17:03 china/
drwxr-xr-x  2 root  root       4096 Feb  8 11:58 Desktop/
drwxr-xr-x  3 root  root      28672 May 14 17:44 file/
drwxr-xr-x  2 root  root       4096 May  8 17:17 file1/
drwxr-xr-x  3 root  root       4096 Mar 25 11:09 logfile/
drwxr-xr-x  2 root  root       4096 Feb 25 16:56 lvm0/
drwxr-xr-x  2 root  root       4096 Mar  5 17:39 scripts/
drwxr-xr-x  2 root  root       4096 May  8 15:50 sysconfig/
[root@stone ~]# ll | grep '^d'
drwxr-xr-x  5  1000  1000      4096 Mar  9 04:17 awstats-7.1.1
drwxr-xr-x  2 root  root       4096 May 15 11:06 bin
drwxr-xr-x  3 root  root       4096 May 14 17:03 china
drwxr-xr-x  2 root  root       4096 Feb  8 11:58 Desktop
drwxr-xr-x  3 root  root      28672 May 14 17:44 file
drwxr-xr-x  2 root  root       4096 May  8 17:17 file1
drwxr-xr-x  3 root  root       4096 Mar 25 11:09 logfile
drwxr-xr-x  2 root  root       4096 Feb 25 16:56 lvm0
drwxr-xr-x  2 root  root       4096 Mar  5 17:39 scripts
drwxr-xr-x  2 root  root       4096 May  8 15:50 sysconfig

[root@stone ~]# find . -maxdepth 1 -type d
.
./.redhat
./logfile
./.chewing
./.nautilus
./.gnome2_private
./Desktop
./scripts
./.lftp
./awstats-7.1.1
./.elinks
./.gstreamer-0.10
./.gnome2
./file1
./china
./file
./.gnome
./.gconfd
./sysconfig
./lvm0
./.metacity
./.mozilla
./.ssh
./.scim
./.gconf
./.vim
./bin
./.eggcups
./.Trash

14、目录切换 cd pushd popd

特殊目录名

符号

含义

.

当前工作目录

..

父目录

~

用户主目录

-

上个工作目录

[root@stone ~]# pwd
/root
[root@stone ~]# cd file1
[root@stone file1]# pwd
/root/file1
[root@stone file1]# cd ..
[root@stone ~]# pwd
/root
[root@stone ~]# cd file1
[root@stone file1]# pwd
/root/file1
[root@stone file1]# cd -
/root
[root@stone ~]# cd /tmp
[root@stone tmp]# cd ~
[root@stone ~]# pwd
/root

#不建议使用pushd和popd,容易出错
15、统计文件行数,单词数和字符数 wc
[root@stone etc]# seq 5 | wc
5       5      10

[root@stone ~]# cat num2
banana
cherry
grape
orange
[root@stone ~]# wc num2 -L
6 num2

#-L选项打印最长行的长度
16、打印目录树 tree
[root@stone ~]# tree file1
file1
|-- config.txt
|-- f1
|-- f1_h
|-- f2
|-- f2_s -> f2
`-- hello
0 directories, 6 files

[root@stone ~]# tree file1 -P "*.txt"
file1
`-- config.txt
0 directories, 1 file

#-P选项匹配特定模式的文件
[root@stone ~]# tree file1 -I "*.txt"
file1
|-- f1
|-- f1_h
|-- f2
|-- f2_s -> f2
`-- hello
0 directories, 5 files

#-I选项排除特定模式的文件

转载于:https://blog.51cto.com/stonebox/1341926

《Linux Shell脚本攻略》读书笔记第三章 以文件之名相关推荐

  1. Linux Shell脚本攻略 读书笔记

    内容目录: 嗨,Echo一下 给终端来点颜色 shell的控制结构 算术比较 目录操作 网站下载 tar 归档工具 rsync 备份系统快照 ftp自动传输 磁盘管理 故障排查 使用syslog记录日 ...

  2. linux cat 脚本,Linux Shell 脚本攻略 读书笔记 -- 201.cat的几种用法

    cat可以读取.显示和拼接文件内容 1. 打印单个文件: root@debian:/home/chicol/scripts# cat file.txt This is a line inside fi ...

  3. Linux Shell脚本攻略学习总结:三

    根据扩展名切分文件名 首先,我们先来看两个例子: file_jpg="sample.jgp" name=${file_jpg%.*} echo File name is : $na ...

  4. linux shell 脚本攻略学习10--生成任意大小的文件和文本文件的交集与差集详解

    一.生成任意大小的文件(dd命令): 举例: amosli@amosli-pc:~/learn/example$ dd if=/dev/zero of=test.zip bs=2M count=1; ...

  5. LINUX SHELL脚本攻略笔记[速查]

    LINUX SHELL脚本攻略笔记[速查] Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgr ...

  6. 《Linux Shell脚本攻略》学习笔记-第一章

    1.1 简介 计算机可以从文本文件(称为shell脚本)中读取并执行命令. sehll脚本不仅节省了时间,而且清楚明白地表明了所执行的操作. bash shell变成了UNIX和Linux中既成事实的 ...

  7. linux shell脚本攻略_(python)Linux下shell脚本监控Tomcat的状态并实现自动启动步骤...

    今天为大家带来的内容是:(python)Linux下shell脚本监控Tomcat的状态并实现自动启动步骤 本文内容主要介绍了Linux下shell脚本监控Tomcat的状态并实现自动启动的步骤,文章 ...

  8. linux shell脚本攻略第3版_「技术干货」师傅说不会写shell脚本的网安不是一个好黑客,实战...

    shell脚本? 在说什么是shell脚本之前,先说说什么是shell. shell是外壳的意思,就是操作系统的外壳.我们可以通过shell命令来操作和控制操作系统,比如Linux中的Shell命令就 ...

  9. Linux Shell脚本攻略-调试脚本+函数和参数

    调试脚本 可以利用Bash内建的调试工具或按照易于调试的方式编写脚本 # 1. 使用选项-x,启用shell脚本的跟踪调试功能 $ bash -x script.sh # 打印出所执行的每一行命令以及 ...

最新文章

  1. web标准,我们去向何方?一些想法...
  2. 使用 COM 风格的编程接口
  3. 程序中调用命令行命令,不显示那个黑黑的DOS窗口
  4. centos java jdk_Centos安装JDK(java环境)
  5. 【算法】逆波兰式(后缀表达式)的计算 中缀表达式转后缀表达式(逆波兰式)
  6. stl 优先队列(堆)
  7. 牛客网暑期ACM多校训练营(第九场)H. Prefix Sum(CDQ分治)
  8. 一个奇妙、离奇的算法题
  9. MySql(十二)Sql92和Sql99的区别
  10. 使用 Socket 通信实现 FTP 客户端程序
  11. Markdown 下载安装
  12. 无限循环小数与分数的对应关系证明
  13. 计算机工程师对社会报答什么,报答作文400字(精选10篇)
  14. D语言之路-第1篇 库函数之争
  15. 计算机应用线型类型为虚线方点,cad怎样把线变成虚线或者点划线
  16. Facebook如何安全度过新账号阶段不被封号
  17. [Windows]删除我的电脑WPS网盘等盘符
  18. LeetCode 1079. 活字印刷
  19. 云知识 - OMA-DM 和OTA的关系
  20. LINUX系统程序设计中C/C++编程工具使用

热门文章

  1. [转发]黑苹果修改DSDT彻底解决关机不断电和睡眠问题
  2. win11打开应用被管理员阻止怎么办 window11管理员已阻止你运行此应用的解决方法
  3. 图像转svg,及绘制svg图像
  4. jquery的validate表单验证表单注册插件
  5. Shell:dos新建sh脚本在linux下执行报错“/bin/sh^M”
  6. IoT:BLE4.0教程一 蓝牙协议连接过程与广播分析
  7. 1486. 数组异或操作
  8. 未找到插件 ‘org.springframework.bootspring-boot-maven-plugin‘(已解决 )
  9. 原来awt是可以设置多个frame
  10. vue draggable 火狐拖拽搜索bug解决