Backs up all modified files in the current directory within the last 24 hours in a "tarball" (Files that have been processed by tar and gzip).
The program code is as follows:
#!/bin/bashBACKUPFILE=backup-$(date +%y-%m-%d)# 在备份文件中嵌入时间。archive=${1:-$BACKUPFILE}# 如果在命令行中没有指定备份文件的文件名,那么将默认使用"backup-YYYY-MM-DD"文件名。tar cvf - `find . -mtime -1 -type f -print` > $archive.targzip $archive.tarecho "Directory $PWD backed up in archive file \"$archive.tar.gz\"."exit 0
Attention
If too many files are found, or if the file name includes a space, the execution fails.
We recommend using one of the two codes below
# -------------------------------------------------------------------# 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 便于移植,但是比较慢。# -------------------------------------------------------------------
The-EXEC option for Find provides an example of
Locate all. txt files in the current directory and print them as "file: File name":
find . -type f -name "*.txt" -exec printf "File: %s\n" {} \;
In the example above, {} is used in conjunction with the-EXEC option to match all files and then be replaced with the corresponding file name.
Xargs-0 Options
The xargs-0 is used as a delimiter.
Shell instance: Back up all modified files in the last day