Shell traversal directory processing specific directory script code _linux shell

Source: Internet
Author: User

Requirements: Delete files, directory structure: ip/year/month, everyone has a copy of this directory now need to delete all files before 2012, 2012 after the deletion of a specific month.
Originally wanted to use find, the results found that these file time (A,M,C) records are not originally generated.
No way to have to think of a soil method to traverse, who has a good method, welcome to inform.

Copy Code code as follows:

#!/bin/bash
cd/home/wanggy/rmfile/
Ls-l | grep ^d | awk ' {print $NF} ' >/home/wanggy/tmp
Cat/home/wanggy/tmp |while Read Dir1
Todo
CD $dir 1
echo "First level directory $dir 1"
Ls-l | grep ^d | awk ' {print $NF} ' >/HOME/WANGGY/TMP1
CAT/HOME/WANGGY/TMP1 |while Read Dir2
Todo
echo "Level two directory $dir 2"
CD $dir 2
#对大于2012年的目录再判断, less than is deleted directly.
If [$dir 2-gt];then
Ls-l | grep ^d | awk ' {print $NF} ' >/HOME/WANGGY/TMP2
CAT/HOME/WANGGY/TMP2 |while Read Dir3
Todo
#9月份之前的删除 "
If [$dir 3-lt];then
RM-RF $dir 3
Fi
Done
Else
echo "Delete directory before 2012: $dir 2"
RM-RF $dir 2
Fi
#返回上一级
Cd..
Done
#返回上一级
Cd..
Done

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.