Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2007
    Location
    Utah, USA
    Posts
    4

    Question Bash Script for a Cron Job > Deleting files in a directory.


    Warning, I am still a Newbie

    My web server is running Linux (don't know what flavor) and I have set up a Cron job to delete all files in my "files" directory.

    This is what I have right now:
    Code:
    rm /home/user/upload/files/*.*
    However if there are no files, I keep getting "No files found in "files/" directory.
    So I am looking for a bash command that does this:

    (pseudo code)
    Code:
    if(FilesExist)
    {
      if(Files != ".htaccess")
      {
        rm /home/user/upload/files/*.* EXCEPT .htaccess
      }
    }
    Simple enough right?
    This is where it gets tricky.

    My web server (long story) only allows me to do this on ONE LINE and I cannot have multiple lines.

    Does anyone know how to do this?

    Second Thought...
    Is there a way that I could create a Bash script that would run the above code and I could just run the script in my Cron job instead?

    I am very new at writing bash scripts and dont really know how to write them, so as much help you can provide, the better.

    Thank you in advance!!!

  2. #2
    Hi,

    The rm comand don't removes files if their names are started with a dot.
    To suppress the error messages you can redirect them to /dev/null.

    Code:
    rm /home/user/upload/files/* 2>/dev/null
    Here you can read how to run a command as a cron job:

    crontab - Wikipedia, the free encyclopedia

    Because the PATH variable is not always set in cronjobs you must use the absolute path:
    Code:
    /bin/rm /home/user/upload/files/* 2>/dev/null

    Regards

  3. #3
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101
    Hint: user -not option with find command and -maxdepth 1 option too...for deleting files at the current dir level only. if you use -not option, then give -name "*<thefilename>*" or the correct pattern of the file that you don't wana delete...consists of.
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101
    Using this also, .* hidden file will not get removed...but if you don't use -not option and use -o with -name for including ".*" files also then may be by using xargs ..we can remove the files...Franklin...what say?



    Quote Originally Posted by Sangal-Arun View Post
    Hint: user -not option with find command and -maxdepth 1 option too...for deleting files at the current dir level only. if you use -not option, then give -name "*<thefilename>*" or the correct pattern of the file that you don't wana delete...consists of.
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  6. #5
    Just Joined!
    Join Date
    Aug 2007
    Location
    Utah, USA
    Posts
    4

    Lightbulb This is what I ended up coming up with...

    Does it look sound to you or do you see any potential problems?
    I will most likely take into consideration what you have also noted.

    (i have the cron job execute this like every 15 minutes) [lots of traffic].

    Code:
    #!/bin/sh
    
    DIR="/home/username/public_html/upload/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

  7. #6
    Just Joined!
    Join Date
    Aug 2007
    Location
    Utah, USA
    Posts
    4

    Exclamation However

    The said method above does not address the .htaccess issue.

  8. #7
    To remove dot files you can do something like this:

    Code:
    rm .[a-zA-Z0-9]*
    Regards

  9. #8
    Just Joined!
    Join Date
    Aug 2007
    Location
    Utah, USA
    Posts
    4
    Quote Originally Posted by Franklin52 View Post
    To remove dot files you can do something like this:

    Code:
    rm .[a-zA-Z0-9]*
    Regards
    What about files with Underscores and Hyphens?
    I'm thinking something like...
    Code:
    rm .[\._a-zA-Z0-9\-]*

  10. #9
    Quote Originally Posted by climbjm View Post
    What about files with Underscores and Hyphens?
    I'm thinking something like...
    Code:
    rm .[\._a-zA-Z0-9\-]*
    Try this:

    Code:
    rm *[_-]*
    Regards

  11. #10
    -->
    rm -f .??* is much easier!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •