Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 988
  • Last Modified:

how to exclude a directory from rm -rf on linux

Hi have a util which generates set of files and directories on a location...but the problem is it create those in windows format (with spaces and special char).

So I execute rm, soon after I execute this util to remove directories

export DIR_NAME="/oracle/app/my_dir"
rm -fr $DIR_NAME/*\/

Help I request is, I need to exclude one single directories from this rm

for example: Assume the util creates 20 directories. I would like to exclude one directory from this 20 which I know which one to exclude.

please note...I cant speceify the target location to create these directories/files when I run the uti. its kind of hard coded in the code.

So how to exclude a directory from this rm

thanks in advance.
0
enthuguy
Asked:
enthuguy
  • 4
  • 2
  • 2
  • +1
2 Solutions
 
Gerwin Jansen, EE MVETopic Advisor Commented:
If you know the directory that you want to exclude, remove it from the list using sed for example, like this:

echo rm -rf `echo ${DIR_NAME}/*\/ | sed "s/<dir to remove>//"`

How does the directory look that you don't want to remove?
0
 
enthuguyAuthor Commented:
Thanks for your quick response....will try this

the directory which I would like exclude from deleting (keep it) consists sub directories and files. but the good thing is without special chars. (just a standard unix directory)

But the (empty) directories which util creates might have special chars (spaces, brackets ) etc.

sorry just clarifying :the directory will be excluded from rm correct? sorry I"m kind of new to shell :)

echo rm -rf `echo ${DIR_NAME}/*\/ | sed "s/<dir to exclude>//"

Thanks again
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
The sample I gave you will just echo the directories and remove the dir to exclude, for testing, you can try this as well:

echo rm -rf `echo ${DIR_NAME}/*\/ | sed "s|/oracle/app/my_dir/somedir ||"`

see if it removes the dir "/oracle/app/my_dir" if  you compare output from above command with this command:

echo rm -rf `echo ${DIR_NAME}/*\/`

Note that if your dire to exclude contains subdirectories then the search pattern may have to be adapted a bit. So if  you can, provide a sample :)
0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
enthuguyAuthor Commented:
echo rm -rf `echo ${DIR_NAME}/*\/ | sed "s/Util//"`
outputs.txt
0
 
enthuguyAuthor Commented:
please check the attached file
0
 
serialbandCommented:
After looking at the output file, you could also just do the following within the directory.

rm -rf [a-z]*

Open in new window

or if you want to visually inspect the list first.
ls [a-z]*
rm -rf `ls [a-z]*`

Open in new window


Another way, if your directory structure is more complex, would be to use find.
find . ! -name "Util" -type -d -prune -o -exec echo rm -r {} \;

Open in new window


Take out the "echo" to run it.
0
 
simon3270Commented:
An alternative approach is temporarily to make the desired directory undeletable:

export DIR_NAME="/oracle/app/my_dir"
chmod 0 $DIR_NAME/dir_to_save
rm -fr $DIR_NAME/*\/
chmod 755 $DIR_NAME/dir_to_save

Open in new window

(use 775, 777, 700, or whatever matches the original permissions of the directory)
0
 
enthuguyAuthor Commented:
Thanks serialband, will try that and let you know.

hi simon, thats what I did. but I moved the direcotory to differet path and cp -r to restore it back after rm
0
 
simon3270Commented:
If you "mv" the directory to a different area on  the same filesystem, then you just need to "mv" it back (no "cp -r" required).
export DIR_NAME="/oracle/app/my_dir"
mv $DIR_NAME/dir_to_save /oracle/app/
rm -fr $DIR_NAME/*\/
mv /oracle/app/dir_to_save $DIR_NAME/

Open in new window

(assuming that there isn't already a directory called /oracle/app/dir_to_save!)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now