LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-09-2012, 11:17 AM   #1
rbalaa
Member
 
Registered: Mar 2010
Posts: 61

Rep: Reputation: 0
rm all except current dir ?


Hi All,

I need to run a script from the same directory that this script deletes files from. But I don't want it to touch the folder that the script is in ? Is there anyway to do this ? (note I have to use rm)

MainFolder (containd all folders and files)
FolderA (contains my files including the script)
FolderB, FolderC ... (Could be any fodlers that I want to delete.

Thanks.
 
Old 01-09-2012, 11:33 AM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
So you have a "MainFolder" that has many files and directories, all of which you want to delete including any number of subdirectories and files contained therein, except for anything in FolderA (and obviously the MainFolder itself)?

You could try something like this:
Code:
for i in `find /path/to/MainFolder/* | grep -v "FolderA"`; do rm -rf $i; done
Let me add the disclaimer that any script where you forcefully and without confirmation delete files and folders should be run only after careful examination of the list of files/folders (from the "find" command) you are going to delete. And it's always a good idea to have everything backed up elsewhere in case something goes awry.

Last edited by thesnow; 01-09-2012 at 11:34 AM.
 
Old 01-09-2012, 02:07 PM   #3
rbalaa
Member
 
Registered: Mar 2010
Posts: 61

Original Poster
Rep: Reputation: 0
Thank you. that solved it for me.
 
  


Reply



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] What can I use to resize my current '/' dir? george-lappies Slackware 17 05-22-2011 04:23 PM
Current dir in C rigel_kent Programming 7 07-25-2006 06:18 PM
su - and stay in current dir lord-fu Linux - Newbie 2 11-16-2005 08:08 AM
Prompt w/ current dir and user Jair Slackware 2 03-15-2004 06:16 PM
How xterm get known that current dir changed? pvv Programming 3 03-15-2004 08:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:47 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
Open Source Consulting | Domain Registration