LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-22-2011, 03:50 PM   #1
raz230
LQ Newbie
 
Registered: Dec 2011
Posts: 2

Rep: Reputation: Disabled
Exclude a folder from a recursive delete?


I need to periodically delete files and folders from a directory for one of our apps.

rm -R *

won't work because I have one folder, called "app" that I need to leave alone.

I know robocopy in Windows has an exclude option, but I can't find any analogies in "rm". Is there an easy way to do this?
 
Old 12-22-2011, 04:02 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
What about:

Code:
mv foo/bah/app foo/app_temp
rm -rf foo/bah/*
mv foo/app_temp foo/bah/app
Hope this helps,
 
Old 12-22-2011, 04:19 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
You can use the find command.

This works, but I'll be honest with you, I probably can't explain why. As such, you should try it out on an experimental directory (modeled after what you need it for):
Code:
find . -maxdepth 1 \( -path ./app -prune \) -o \( -regex "\./.*" -print \)
If that gives you a list of the top-level files and directories you want to delete, then you can delete them with this:
Code:
find . -maxdepth 1 \( -path ./app -prune \) -o \( -regex "\./.*" -print \) -exec rm -R {} \;
The reason I can't explain why is, my reading of the find command's man page, the -path, -prune, and -o options in this combination are not what I would expect to get this behavior. Then again, I could just be a moron and it's plain as day to everyone else.

Also, to be absolutely clear, the command needs to be run from the directory that contains the files to delete. If you want or need a command that isn't restricted like that, you could try:
Code:
find /path/to/dir -maxdepth 1 \( -path /path/to/dir/app -prune \) -o \( -regex "/path/to/dir/.*" -print \) -exec rm -R {} \;
 
1 members found this post helpful.
Old 12-22-2011, 04:37 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Another method if you're using the bash shell: the GLOBIGNORE environment variable.

For instance:
Code:
user@localhost:~/temp$ ls -ld *
drwxr-xr-x 2 user user 4096 Dec 22 16:31 app
drwxr-xr-x 2 user user 4096 Dec 22 16:31 delete_me
drwxr-xr-x 2 user user 4096 Dec 22 16:31 junk
drwxr-xr-x 2 user user 4096 Dec 22 16:31 waste_of_space
user@localhost:~/temp$ oldIGNORE=$GLOBIGNORE
user@localhost:~/temp$ export GLOBIGNORE=app
user@localhost:~/temp$ ls -ld *
drwxr-xr-x 2 user user 4096 Dec 22 16:31 delete_me
drwxr-xr-x 2 user user 4096 Dec 22 16:31 junk
drwxr-xr-x 2 user user 4096 Dec 22 16:31 waste_of_space
user@localhost:~/temp$ export GLOBIGNORE=$oldIGNORE
user@localhost:~/temp$ ls -ld *
drwxr-xr-x 2 user user 4096 Dec 22 16:31 app
drwxr-xr-x 2 user user 4096 Dec 22 16:31 delete_me
drwxr-xr-x 2 user user 4096 Dec 22 16:31 junk
drwxr-xr-x 2 user user 4096 Dec 22 16:31 waste_of_space
user@localhost:~/temp$
So, the above uses "ls -ld *" as an example whereas you would use "rm -R *" AFTER setting the GLOBIGNORE environment variable
 
1 members found this post helpful.
Old 12-22-2011, 05:17 PM   #5
raz230
LQ Newbie
 
Registered: Dec 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
@Dark_Helmut -
Thank you. I had a feeling it could be done that way, with the piped together commands. I've used that format to bulk remove files of type X older than Y. I'll experiment with that - I found my own way too. Not as elegant - but it works. I also realized that my "app" folder is a symlink so I made this:

#!/bin/bash

for file in /users/Rob/Desktop/test/*
do
if [ -d $file ]; then
echo deleting $file which is a directory
rm -Rf $file
else

if [ -L $file ]; then
echo "Ignoring $file which is a symlink" #this was the item I needed to skip over.
else
echo "deleting $file"
rm -f $file
fi
fi
done
 
Old 12-22-2011, 07:11 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
Dark_Helmet++
Haven't noticed that GLOBIGNORE option before & I've never used it.
Props for a sneaky soln
 
Old 12-23-2011, 01:57 PM   #7
honeybadger
Member
 
Registered: Aug 2007
Location: India
Distribution: Slackware (mainly) and then a lot of others...
Posts: 855

Rep: Reputation: Disabled
Well another suggestion - 'chown -Rv root:root <dir>' and this will prevent the deletion of the folder if you are logged in as a different user. Another thing that can help here is the 'chattr'. I do not know the exact command the man page should help this too will prevent deletion of the directory. Guess, this should help.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Recursive delete specific files from sub-directories. guriinii Linux - Newbie 11 03-07-2011 10:41 AM
[SOLVED] Shell script for recursive delete required. bikbjrt Linux - Newbie 2 07-01-2010 12:25 AM
Scripted folder name changes and recursive disruptive Programming 7 02-28-2008 08:08 PM
ftp recursive delete command? Guest1234 Linux - Software 1 12-01-2007 04:12 AM
TAR: Unpack file, exclude a folder Smeerbalg Linux - Software 1 01-29-2006 07:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:15 PM.

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