bash1---基本0

  最近在看《高级BASH脚本编程》,边学习脚本编程,边学习Linux的命令。

  另外还解决了笔记本电脑上关于Ubuntu无法找到wifi适配器的问题。具体看我另外一篇博客。

  另外推荐一个学习linux命令的网站

  今天主要想讲一下如何备份最后一天所有修改的文件

#!/bin/bash

# 在一个"tarball"中(经过tar和gzip处理过的文件)
#+ 备份最后24小时当前目录下d所有修改的文件.

BACKUPFILE=backup-$(date +%m-%d-%Y)
# 在备份文件中嵌入时间.
# Thanks, Joshua Tschida, for the idea.
archive=${1:-$BACKUPFILE}      #这个地方讲的是变量1=${1:-$变量2},如果命令行没有指定参数(式子里面的$1),变量1=变量2;如果有参数变量1=$1
# 如果在命令行中没有指定备份文件的文件名,
#+ 那么将默认使用"backup-MM-DD-YYYY.tar.gz".
tar cvf - `find . -mtime -1 -type f -print` > $archive.tar
gzip $archive.tar
echo "Directory $PWD   backed up in archive file "$archive.tar.gz"."

# Stephane Chazelas指出上边代码,
#+ 如果在发现太多的文件的时候, 或者是如果文件
#+ 名包括空格的时候, 将执行失败.

# Stephane Chazelas建议使用下边的两种代码之一:
# -----------------------------------------------------------------
# find . -mtime -1 -type f -print0 | xargs -0 tar rvf "$archive.tar"
# 使用gnu版本的"find".
# find . -mtime -1 -type f -exec tar rvf "$archive.tar" '{}' ;
# 对于其他风格的UNIX便于移植, 但是比较慢.
# -----------------------------------------------------------------

exit 0

  上面是书本中给出的例子

这里需要讲解的有两个命令,一个是tar,另外一个是find

tar——想要看tar 的相关信息可以输入命令info tar,如果英语不好(像我这样的学渣),可以看上面推荐网站的第十九章归档与备份

下面,我还是做一点简短的介绍:

tar - an archiving utility(一个归档工具):tape archiving file

 Archiving is the process of gathering up many files and bundling them together into a single large file. Archiving is often done as a part of system backups. It is also used when old data is moved from a system to some type of long-term storage.

tar -cvf 打包名字.tar 打包目录(tar --create --file=打包名字 For a more detailed listing, we can add the v (verbose) option:v=打包目录的内容)

tar -rvf 打包名字.tar (r是追加,使用-exec 行为,来 唤醒带有追加模式(r)的 tar 命令,把匹配的文件添加到归档文件 playground.tar 里面。)

eg:

find . -mtime -1 -type f | tar rvf "$archive.tar" -T -

   this convention of using “-” to represent standard input/output is used by a number of other programs, too.“-T -”表示"--files-from=-"(这个-就是标准输出s)

其实这个例子主要讲的就是“-”用于重定向stdin或stdout。即“-”可以被用来将stdout通过管道传递到其他命令中,可以放在结尾,也可以放在 tar中间

 find . -mtime -1 -type f -exec tar rvf "$archive.tar" '{}' '+'  #这个方法是同样是找出当前目录下1天内的普通文件进行打包,吧他们追加如$archive.tar中。

注:

  1. -c:建立一个压缩文件的参数指令(create 的意思);    
  2. -x:解开一个压缩文件的参数指令!    
  3. -t:查看 tarfile 里面的文件!    
  4. -r:向压缩归档文件末尾追加文件    
  5. -u:更新原压缩包中的文件     

另外一个是find命令.这是一个查找文件的命令,详细更多命令可以查看这个网站
包括locate( Find files by name),find(Search for files in a directory hierarchy)
We will also look at a command that is often used with file search commands to process the resulting list of files:
xargs – Build and execute command lines from standard input
stat – Display file or file system status
wc-print newline ,word,and byte counts for each file
因为find 找到的文件是一行一行的表示,配合wc -l可以统计文件的个数
find 路径 -type [bcdfl]



File TypeDescription
b Block special device file
c Character special device file
d Directory
f Regular file
l Symbolic link
[me@linuxbox ~]$ find ~ -type f -name "*.JPG" -size +1M | wc -l
寻找文件大小大于1M的jpg图片,并输出个数。用双引号引起来通配符,为了防止shell展开路径名
另外:  find dir -type [bcdfl] -name "keyword"
     find dir -type [bcdfl] -name keyword -exec ls '{}' ';'
     find dir -type [bcdfl] -name keyword -print | xargs ls -l
  后面两个是等价的,其中'{}'代表查找出来的文件,';'代表结束符, 用''做转义;结束


另外就是 上文中用到的find的测试条件:
测试条件·                    描述
-cmin n 匹配内容或属性最后修改时间正好在 n 分钟之前的文件或目录。 指定少于 n 分钟之前,使用 -n,指定多于 n 分钟之前,使用 +n。
-cnewer file 匹配内容或属性最后修改时间晚于 file 的文件或目录。
-ctime n 匹配内容和属性最后修改时间在 n*24小时之前的文件和目录。
-empty 匹配空文件和目录。
-group name 匹配属于一个组的文件或目录。组可以用组名或组 ID 来表示。
-iname pattern 就像-name 测试条件,但是不区分大小写。
-inum n 匹配 inode 号是 n的文件。这对于找到某个特殊 inode 的所有硬链接很有帮助。
-mmin n 匹配内容被修改于 n 分钟之前的文件或目录。
-mtime n 匹配的文件或目录的内容被修改于 n*24小时之前。
-name pattern 用指定的通配符模式匹配的文件和目录。
-newer file 匹配内容晚于指定的文件的文件和目录。这在编写执行备份的 shell 脚本的时候很有帮。 每次你制作一个备份,更新文件(比如说日志),然后使用 find 命令来判断哪些文件自从上一次更新之后被更改了。
-nouser 匹配不属于一个有效用户的文件和目录。这可以用来查找 属于被删除的帐户的文件或监测攻击行为。
-nogroup 匹配不属于一个有效的组的文件和目录。
-perm mode 匹配权限已经设置为指定的 mode的文件或目录。mode 可以用 八进制或符号表示法。
-samefile name 类似于-inum 测试条件。匹配和文件 name 享有同样 inode 号的文件。
-size n 匹配大小为 n 的文件
-type c 匹配文件类型是 c 的文件。
-user name 匹配属于某个用户的文件或目录。这个用户可以通过用户名或用户 ID 来表示。
原文地址:https://www.cnblogs.com/SsoZhNO-1/p/9222062.html