Shell instance: Back up all modified files in the last day

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.