LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-11-2013, 04:16 PM   #1
phinzola1
LQ Newbie
 
Registered: Dec 2013
Posts: 12

Rep: Reputation: Disabled
deleting multiple directories and their subdirectories and then set crontab to delate


Hi Guys

The following is what I want to do but I dont know how to do it.
1.Delete multiple directories and their subdirectories.I want all files that ends with BSCXXX eg APSTBSC1,MTCBSC23 etx
2.Then I would like to use crontab to delete these folders after every 15 mins
3.Folders are in this path /opt/var/test (thus where APSTBSC1 etc are.No worry about subdirectories)

Thanks in advance

phinzola
 
Old 12-11-2013, 04:25 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You can start from a find command:
Code:
find /opt/var/test -maxdepth 1 -type d -regex '.*BSC[0-9]+$' -exec echo rm -rf {} +
the echo command is for safety: keep it until you verify that the results of the search are exactly what you're looking for. Regarding the crontab part please read
Code:
man 5 crontab
 
1 members found this post helpful.
Old 12-11-2013, 04:30 PM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,051

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
Quote:
Originally Posted by phinzola1 View Post
Hi Guys

The following is what I want to do but I dont know how to do it.
1.Delete multiple directories and their subdirectories.I want all files that ends with BSCXXX eg APSTBSC1,MTCBSC23 etx
2.Then I would like to use crontab to delete these folders after every 15 mins
3.Folders are in this path /opt/var/test (thus where APSTBSC1 etc are.No worry about subdirectories)

Thanks in advance

phinzola
this would go in the users crontab:
Code:
15,30,45 * * * *     echo /bin/rm -rf /opt/var/test/*BSC* > /opt/var/test/delete.lst
this is kinda' destructive. remove the echo and redirection when you are sure it selects the rite files.

Last edited by schneidz; 12-11-2013 at 04:43 PM.
 
1 members found this post helpful.
Old 12-11-2013, 04:31 PM   #4
phinzola1
LQ Newbie
 
Registered: Dec 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
ok thanks
So if the such is fine, then I remove the echo when implementing the command?

---------- Post added 12-11-13 at 04:32 PM ----------

Sorry I wanted to say the search instead of such
 
Old 12-11-2013, 04:35 PM   #5
phinzola1
LQ Newbie
 
Registered: Dec 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
Ok but I wouldn't use the redirect because the idea behind is to create disk space and we dont want to lad these files in these directories
 
Old 12-11-2013, 04:44 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
In both cases the echo command is just for testing purposes. Since the rm command is potentially dangerous you may want to test it without actually execute. The redirection suggested by schneidz in the crontab entry is for testing as well, so that you can check the result of the cron job. It won't be necessary when you actually remove the directories, since the rm -rf command does its job without sending any message to standard output, nor to standard error.
 
Old 12-12-2013, 03:28 PM   #7
phinzola1
LQ Newbie
 
Registered: Dec 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
Hello there

I did what you said I must do.It did not delete.the command can run without any error but its not deleting anything
 
Old 12-12-2013, 03:37 PM   #8
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,051

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
the echo is there to prevent you from shooting yourself in the foot.
 
Old 12-12-2013, 03:53 PM   #9
phinzola1
LQ Newbie
 
Registered: Dec 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
yes but the command is not deleting.

I am doing it manually and the folders are so many and I want to go for Xmas vacation and am afraid the system will crush because of disk space shortage
 
Old 12-12-2013, 03:55 PM   #10
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,051

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
are you saying that you are trying to delete the files manually (without useing cron) but you are not able to ?

i think that would be because the user you are logged in as doesnt have the authority to remove those files.

what is the command you are using / what is the error you are getting / what distribution are you using ?

Last edited by schneidz; 12-12-2013 at 04:08 PM.
 
Old 12-12-2013, 04:04 PM   #11
phinzola1
LQ Newbie
 
Registered: Dec 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
1.No if am in /opt/var/test , if i type rm -rf APBSC1 I will delete
2.But if I use the crontab 15,30,45 * * * * /bin/rm -rf /opt/var/test/*BSC* nothing is happening.Remember its you who gave me this cron settings
3.The user I am using have rights to delete.

4.What I want is to set cron to delete just like if i type rm -rf APBSC1 I will delete (so that it will delete at set time)
 
Old 12-12-2013, 04:10 PM   #12
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,051

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
what distro are you using. what does it return when you type
Code:
which rm
 
Old 12-12-2013, 04:14 PM   #13
phinzola1
LQ Newbie
 
Registered: Dec 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
let me connect to server nw.i am at home will revert back just nw
 
Old 12-12-2013, 04:20 PM   #14
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,051

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
fyi: this works for me:
Code:
[schneidz@hyper stuff]$ uname -a -m -p
Linux hyper 2.6.43.8-1.fc15.x86_64 #1 SMP Mon Jun 4 20:33:44 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
[schneidz@hyper stuff]$ crontab -l
15,30,45 * * * *     echo /bin/rm -rf /opt/var/test/*BSC* > /home/schneidz/cron.test
[schneidz@hyper stuff]$ ll /home/schneidz/cron.test
-rw-rw-r--. 1 schneidz schneidz 32 Dec 12 16:15 /home/schneidz/cron.test
can you please run this command and copy the result in your next response:
Code:
ll /opt/var/test/*BSC*

Last edited by schneidz; 12-12-2013 at 04:24 PM.
 
Old 12-12-2013, 04:24 PM   #15
phinzola1
LQ Newbie
 
Registered: Dec 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
which rm gives me /bin/rm

No I dont want to redirect.I just want to delete the directories and its subdirectories completely.
I can see u use echo.I thought you said i must not use echo
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH Help - Set multiple directories in variable cli Linux - Newbie 13 06-08-2013 12:50 AM
[SOLVED] Command for deleting file type from a directory and its subdirectories? Cinematography Linux - General 3 06-21-2012 06:58 PM
Find directories that have two subdirectories in them-- PLEASE help pierceogden Programming 6 04-06-2012 09:47 PM
Deleting remote directories on multiple hosts with a bash script zirtik Programming 6 04-16-2009 10:52 AM
copy directories and subdirectories Red Squirrel Linux - Newbie 2 03-06-2004 10:18 PM


All times are GMT -5. The time now is 07:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration