Shell programming Exercise-Copy the specified file (keep the directory structure of the file)

Source: Internet
Author: User

1. Purpose
Copy all files larger than 1M (customizable) in the/root directory to the/tmp/bakdir (customizable) directory, and you need to keep the directory structure.
For example, the/root/reed.file file size is 12M and will be copied to the/tmp/bakdir/root/path location.
2. Difficulty
It needs to be copied along with the path where the file resides.
3. Knowledge points
3.1 operation of files and directories (dirname command)
3.2 File Lookup operation (Find command)
3.3SHELL Programming Basics
4. Thinking and concrete realization
4.1 Ideas
1) Find the specified file first;
2) According to the found file, output its path;
3) Create the path and copy the file.
4.2 Effects
Original catalog file:

[[email protected] BakDir]# find /root -size +1M |xargs ls -lh -rw-r--r-- 1 root root 4.7M Jan 17  2017 /root/ansible-packages/ansible-2.2.1.0-1.el6.noarch.rpm-rw-r--r-- 1 root root 1.5M Jul  3  2011 /root/ansible-packages/python-babel-0.9.4-5.1.el6.noarch.rpm-rw-r--r-- 1 root root 7.5M Feb  3  2017 /root/ansible-packages.tar.gz-rw------- 1 root root 6.3M Mar 14  2017 /root/.cache/pip/http/6/b/5/7/b/6b57b626d79b36c0d0749210d43211130a56666ce3959ca91a518127-rw-r--r-- 1 root root  13M Feb  6 13:54 /root/dir1/dir2/file2-rw-r--r-- 1 root root  15M Feb  6 13:55 /root/dir1/dir3/file3-rw-r--r-- 1 root root  23M Feb  6 13:54 /root/dir1/file1-1-rwxr-xr-x 1 root root 3.7M Nov 18 03:31 /root/nginx-1.12.2/objs/nginx-rw-r--r-- 1 root root 2.0M Nov 18 02:53 /root/pcre-8.40.tar.gz-rw-r--r-- 1 root root 1.2M Mar  8  2017 /root/PI.DAT

After the copied path file:

  [[email protected] bakdir]# find/tmp/bakdir-size +1m |xargs ls-lh-rw-r--r--1 root root 4.7M Feb 12 12: 19/tmp/bakdir/root/ansible-packages/ansible-2.2.1.0-1.el6.noarch.rpm-rw-r--r--1 root root 1.5M Feb 12:19/tmp/ bakdir/root/ansible-packages/python-babel-0.9.4-5.1.el6.noarch.rpm-rw-r--r--1 root root 7.5M Feb 12:19/tmp/bakdir /ROOT/ANSIBLE-PACKAGES.TAR.GZ-RW-------1 root root 6.3M Feb 12:19/tmp/bakdir/root/.cache/pip/http/6/b/5/7/b/ 6b57b626d79b36c0d0749210d43211130a56666ce3959ca91a518127-rw-r--r--1 root root 13M Feb 12:19/tmp/bakdir/root/dir1/ dir2/file2-rw-r--r--1 root root 15M Feb 12:19/tmp/bakdir/root/dir1/dir3/file3-rw-r--r--1 root root 23M Feb 12 12:1 9/tmp/bakdir/root/dir1/file1-1-rwxr-xr-x 1 root root 3.7M Feb 12:19/tmp/bakdir/root/nginx-1.12.2/objs/ nginx-rw-r--r--1 root root 2.0M Feb 12:19/tmp/bakdir/root/pcre-8.40.tar.gz-rw-r--r--1 root root 1.2M Feb 12:19/t Mp/bakdir/root/pi. DAT  

4.3 Concrete implementations

[[email protected] ~]# cat CpFiles.sh #!/bin/bash#Func:cp files include directory#Author:reed. /etc/profileSourcePath="/root"DestPath="/tmp/BakDir"#mkdir backup directory[ ! -d $DestPath ] && mkdir -p $DestPathFunc_CpFiles(){        for FileList in $(find $SourcePath -size +1M);do                #mkdir                 [ ! -d ${DestPath}$(dirname $FileList) ] && mkdir -p ${DestPath}$(dirname $FileList)                #copy                cp -rf $FileList ${DestPath}$(dirname $FileList)         done}Func_CpFiles

Shell programming Exercise-Copy the specified file (keep the directory structure of the file)

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.