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

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 > image.iso
[root@stone ~]# dd if=/dev/cdrom of=image.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选项排除特定模式的文件


































 

你可能感兴趣的:(linux,shell,以文件之名)