Thursday, September 23, 2010

delete subdirecotry and files from a directory but not the directory

sometimes, people need delete only subdirectory and files in a direcotry but not the directory.

for example, we have following directory structure

tti - aa.log
- tt1 - ab.log
tti and tt1 are directories; aa.log and ab.log are files.

if all but tti are required to be deleted.
issue following command:
find tti/* -exec rm -rf {} \;


ATTEN:
find tti -exex tm -rf {} \; will not work because it delet tti too.

Following is the output test in solaris 10

:->find tti -exec ls -ltr {} \;
total 0
-rw-r--r-- 1 oracle dba 0 Sep 23 11:36 aa.log
drwxr-xr-x 2 oracle dba 96 Sep 23 11:37 tt1
-rw-r--r-- 1 oracle dba 0 Sep 23 11:36 tti/aa.log
total 0
-rw-r--r-- 1 oracle dba 0 Sep 23 11:37 ab.log
-rw-r--r-- 1 oracle dba 0 Sep 23 11:37 tti/tt1/ab.log

:->find tti -exec rm -rf {} \;
:->cd tti
bash: cd: tti: No such file or directory
:->

When tti/* is used


:->find tti/* -exec ls -ltr {} \;
-rw-r--r-- 1 oracle dba 0 Sep 23 11:39 tti/aa.log
total 0
-rw-r--r-- 1 oracle dba 0 Sep 23 11:39 ab.log
-rw-r--r-- 1 oracle dba 0 Sep 23 11:39 tti/tt1/ab.log
:->find tti/* -exec rm -rf {} \;
:->cd tti
:->ls
:->pwd
~/tti

No comments:

Post a Comment