LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   rm all except current dir ? (https://www.linuxquestions.org/questions/linux-software-2/rm-all-except-current-dir-922887/)

rbalaa 01-09-2012 11:17 AM

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.

thesnow 01-09-2012 11:33 AM

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.

rbalaa 01-09-2012 02:07 PM

Thank you. that solved it for me.


All times are GMT -5. The time now is 12:03 AM.