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 6701761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:58:22+00:00 2026-05-26T06:58:22+00:00

Need a script to delete all files in a directory then email once complete,

  • 0

Need a script to delete all files in a directory then email once complete, I have the below as a template but unsure if this will work – of course no email component has been added!

#!/bin/sh
DIR="/var/www/public_html/docs/files/"
LIST=`ls -l $DIR | grep -v "total 0"`
FILES=`ls $DIR`
cd $DIR
if [ -z "$LIST" ]
then
  exit
else
  echo "Files Delete:"
  echo $FILES
  rm -f *
fi

Update:

#!/bin/sh
DIR="/home/test/test/docs/test/"
cd $DIR
rm -f *
  • 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-05-26T06:58:23+00:00Added an answer on May 26, 2026 at 6:58 am

    Some notes:

    You use all-caps DIR, LIST and FILES, but all-caps variables in shell scripts are, by convention, environment variables. You should use e.g.

    dir='/var/www/public_html/docs/files/'
    

    instead.

    To find how many files are in a directory use

    find "$dir" -maxdepth 1 -type f | wc -l
    

    You use both LIST and FILES; it seems like you’re tring to find out if there are any files before deleting them. There’s no point to this from a functionality point of view but if you must conditionally echo the list of files it’s better to make the decision this way.

    if [ $(find "$dir" -type f | wc -l) -gt 0 ] ; then
        echo Files Delete:
        find "$dir" -printf '%f '
    fi
    

    Although you should be aware that this output cannot be reliably used to reconstruct the actual file names.

    To actually delete the files you should again use find

    find "$dir" -maxdepth 1 -type f -delete
    

    Putting it all together

    dir='/var/www/public_html/docs/files/'
    
    if [ $(find "$dir" -type f | wc -l) -gt 0 ] ; then
        echo Files Delete:
        find "$dir" -maxdepth 1 -type f  -printf '%f ' -delete
    fi
    

    Here I have combined the “print files” and “delete files” steps into a single invocation of find.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a script that will delete all files from a ftp directory, and
Possible Duplicate: Delete all but the most recent X files in bash I have
I have a folder with a bunch of files. I need to delete all
I am using PHP and I need to script something like below: I have
I have a somewhat large output text file where I need to delete all
I'm trying to create a script to delete all files in a folder and
A python script need to spawn multiple sub-processes via fork(). All of those child
I have a PHP script with infinite loop. I need this script running forever.
i need an sql script that is going to change the datasource of all
I'm looking for a script to loop through a folder and delete all the

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.