Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8564839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:17:18+00:00 2026-06-11T17:17:18+00:00

I have so much files and folders. They have a syntax like this path

  • 0

I have so much files and folders. They have a syntax like this path

/opt/logs/qnap/[hosta,hostb]/2012/03/12

All log file on the Qnap data storage and it is in the another location. So when I want to delete files & folders which are older than 180 days I cannot use find command cause of the slowness. So I wrote a script like this .

SixMonthAgo=$(date --date='190 day ago' "+%Y/%m/%d/%H")  = 2011/06/12/12
Hosts="hosta hostb"
maxDay=181
qnapFolder="/opt/logs/qnap"

for host in $(echo "${Hosts}"); do
        maxDayAgo=$(date --date="${maxDay} day ago" "+%Y/%m/%d")
        countCharacters=$(echo ${maxDayAgo} | wc -c)
        if [ ${countCharacters} -ge 10 ]; then
                rm -rf ${qnapFolder}/${host}/${maxDayAgo} #output of this just like 2012/03/12
        fi
done

But sometimes i got an error. This soluiton is not working correctly what I want. I want to delete all files and folders before 180 days. How can I do this ? That must delete before all files and directories before 2012/03/23 for example .

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:17:19+00:00Added an answer on June 11, 2026 at 5:17 pm

    The find command is slow? Are you certain? The only drawback is that it fails to remove the directory structure.

    find /opt/logs/qnap -type f -mtime +180 -delete

    If the directories were indeed created at the same time then maybe removing -type f could work for you.

    find /opt/logs/qnap -mtime +180 -delete

    find is very fast mind you … but if you really want to do it with a shell script …

    CUTOFF=$(date --date='190 day ago' "+%s)
    HOSTS="hosta hostb"
    DIRECTORY="/opt/logs/qnap"
    
    for HOST in $(ls -1 $DIRECTORY); do
        if [ -d $DIRECTORY$HOST ]; then
            for DATE in $(ls -1 $DIRECTORY$HOST); do
                if [ -d $DIRECTORY$HOST$DATE ]; then
                    if [ "$CUTOFF" < "$(stat --format="%Y" $DIRECTORY$HOST$DATE)" ]; then
                        echo "Deleting $DIRECTORY$HOST$DATE"
                        rm -rf $DIRECTORY$HOST$DATE;
                    fi
                fi
            done
        fi
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have spent way too much time looking for the files owner icon to
I have a file with the structure like so: http://gamedev.pastebin.com/8iESYTVY but it's much bigger
This post on SO answers most of the questions I have (much thanks to
I have a folder called notes, naturally they will be categorized into folders, and
Much related to this question , we have a scenario on my team where
I don't have much experience working with resultsets, but as ResultSet is an interface,
I don't have much experience with Serial I/O, but have recently been tasked with
I'm currently extending a Site and do not have much experience with symfony. Currently
What does x :: xs' mean? I dont have much functional experience but IIRC
Need a list of Pcap.Net members or classes? Their website doesn't have much documentation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.