Script to delete sub directories with parameters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to delete sub directories with parameters
# 1  
Old 06-18-2015
Script to delete sub directories with parameters

Hello,

How would I write a BASH script to remove all sub directories in a root level directory that are older than
10 days, but exclude directories owned by certain users. I would need to run as a cron job once a week.

Thank you
-John
# 2  
Old 06-18-2015
What code do you have now?
An overview of the steps:
Code:
cd to the directory
use find to locate directories with mtime > 10 days, rm -r the sub directory

One caveat - if you have multiple directory levels you may want to rethink your design. BY this I mean,
Code:
dira
   dirb
     dirx
   dirc
     dirm
     dirn

if you clobber dirc because it has not been written to, and dirn is still being modified you have a problem.
# 3  
Old 06-18-2015
Quote:
All the WARNINGS applied. All the liabilities forfeited. WITHOUT ANY WARRANTY; without even the implied warranty of FITNESS FOR A PARTICULAR PURPOSE.
Code:
find /path/to/directory -type d ! \( -user user1 -o -user user2 \) ! -ctime -10 -print

find : find command to recursively locate files, links and directories.

/path/to/directory: location where to start searching from.

-type d: return true if is a directory.

! \( -user user1 -o -user user2 \): returns true if it DOES NOT belong to user1 or user2.

! -ctime -10: returns true if is NOT less than 10 days old.

-print: display the result. It is the default and can be omitted. But here is a place holder for following explanation.

If you like what you get, change the -print for -exec rm -rf {} + which, recursively, will delete those directories that matched.

There is an implied AND between all these expressions, which means: if is a directory AND if it doesn't belong to user1 or user2 AND if is not younger than 10 day.
If you want to exclude more users just add a -o -user <username> inside the ! \( \) The ! -ctime -10 can be changed to -ctime +10 as well

Last edited by Aia; 06-18-2015 at 05:43 PM.. Reason: Adding some note.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need shell script to compare directories and delete files on target server

Hello, I need help in writing the shell script for below mentioned case. There are 2 servers(server A, server B). A cronjob syncs files between these 2 servers. Existing script is copying files from A to B. This is done using the command rsync. However, the files are not deleted... (2 Replies)
Discussion started by: SravaniVedam11
2 Replies

2. Red Hat

How to delete only directories alone?

Hi, I need to delete (remove) only directories. How to achieve this? Plz help (4 Replies)
Discussion started by: karthick nath
4 Replies

3. Shell Programming and Scripting

Script to go Into Directories and Find/Delete files

I have a task, I usually do manually, but with growing responsibilities I tend to forget to do this weekly, I want to write a script that automates this, but I cant seem to work it out in my head, I have the shell of it out, but need help, and you guys have helped me with EVERY problem I have... (5 Replies)
Discussion started by: gkelly1117
5 Replies

4. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

5. Shell Programming and Scripting

Delete Directories

Hello All, My shell script runs everyday to update certain database. Everytime the script runs it creates a directory, with "current date". These directories contain log and data files: HOME_DIR/database_name/20120417/* HOME_DIR/database_name/20120416/*... (6 Replies)
Discussion started by: ad23
6 Replies

6. Shell Programming and Scripting

Script to delete old directories

Hi, I have a requirement like, I need to create the directory with date and time stap (i.e YYYYMMDDHMS) every day end need to delete the old directories which is 12 months old. I have tested with following script cd /export/home/sbeeravo/; find . -type d -mtime +365 -exec rm -rf {} \; ... (2 Replies)
Discussion started by: ShirishaReddy
2 Replies

7. Shell Programming and Scripting

Script to delete old directories

Hi I have 10 directories.I want to delete all the dicrectories except the three recent directories based on timestamp.I am new to BASH can u help me? (1 Reply)
Discussion started by: newuser_25
1 Replies

8. Shell Programming and Scripting

shell script to delete directories...

*Just realized that i posted this in the wrong forum. should have been in Shell, though it is on AIX... Hi. I'm trying to write a script that will delete all directories found, that are not named as a "number" (year)... here is what i mean, let's say i have within /data/exports the... (8 Replies)
Discussion started by: Stephan
8 Replies

9. Shell Programming and Scripting

shell script to delete directories...

Hi. I'm trying to write a script that will delete all directories found, that are not named as a "number" (year)... here is what i mean, let's say i have within /data/exports the following directories: /data/exports/2000 /data/exports/2001 /data/exports/2002 /data/exports/daily/2000... (5 Replies)
Discussion started by: Stephan
5 Replies

10. Shell Programming and Scripting

need some help with a script to delete directories.

Hello, I need a hand with a script im trying to make. I have a directory /usr/db/mail with a bunch of subdirectories such as /usr/db/mail/domain1.com /usr/db/mail/domain2.com etc. etc. all of these directories in turn have sub directories of their own. What I need to do is search all of... (1 Reply)
Discussion started by: centrino
1 Replies
Login or Register to Ask a Question