shell script which will delete every thing under a directory except a single sub dire


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script which will delete every thing under a directory except a single sub dire
# 1  
Old 10-16-2008
shell script which will delete every thing under a directory except a single sub dire

write a shell script which will delete every thing under a directory except a single sub directory
# 2  
Old 10-16-2008
How about this?

Code:
#!/bin/bash

for FILE in `ls`
do
     rm {};
done;

But even these can be used:

Code:
find ./ -maxdepth 1 -name "*" -exec rm {} \;

and

Code:
 rm *

Note that rm does not remove sub directories. If someone finds a mistake, please let me know.
# 3  
Old 10-16-2008
hi ,

question is bit ambiguous

For Ex : In your current directory u have file a.txt , b.txt , c.txt and directories a1 , b1 , c1 .

you want to remove everything except a1 directory

If this is the case ...

find . ! -name a1 -exec rm -i {} \;



Regards,
Penchal
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

2. Shell Programming and Scripting

Shell script to delete specify directory

I have the following directories under "0.0.0.0" /scratch/builds/snapshotBuilds/0.0.0.0 drwxr-xr-x+ tested-571 drwxr-xr-x+ tested-576 drwxr-xr-x+ tested-597 drwxr-xr-x+ 600 drwxr-xr-x+ 601 drwxr-xr-x+ 602 drwxr-xr-x+ 603 drwxr-xr-x+ tested-604 drwxr-xr-x+ tested-605... (13 Replies)
Discussion started by: ibad_urs
13 Replies

3. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

4. Solaris

Strange thing with solaris ls on nfs shared directory

I shared from linux server a dir with nfs3,solaris mount ok,and can tar files,but if i do ls or cp.. on mnt i have mount the nfs share root@solaris: mnt $ touch 2 root@solaris: mnt $ ls -lh ls: can't read ACL on .: Permission denied root@solaris: mnt $ ls 1.tar 2 root@solaris: mnt $ cp... (4 Replies)
Discussion started by: Linusolaradm1
4 Replies

5. Shell Programming and Scripting

delete only particular file in directory shell script

Hi, what function do we use to delete only particular file that we want in directory shell script for example I want only to delete test.txt in directory how to do it ? in sh Thank (1 Reply)
Discussion started by: guidely
1 Replies

6. Shell Programming and Scripting

script to delete contents in a directory by date

Hi , I am trying to make a cron job to delete the contents of a directory in a linux environment.The contents created before 2 days should get deleted by this job.Here is what i have written : /usr/bin/find / -name *.log -ctime +2 -exec /bin/rm -f {} \; Is it correct....If not so,please... (9 Replies)
Discussion started by: d8011
9 Replies

7. Shell Programming and Scripting

Script to delete all something.txt~ file from a directory

There are some files in a directory like a.tx~ , b.txt~,c.txt~. I want to delete all these files inside that directory and sub directory. How can i do this? #!/bin/bash cd thatdirectory ...... rm -rf *~ ...... (7 Replies)
Discussion started by: cola
7 Replies

8. Shell Programming and Scripting

Linux delete script in sub-directory

Under TEMP folder, it have many sub-folder, example"0015,0016,etc" I need to discovery those file which are 2 days ago in this sub-folder , and list out to investigate, at the end delete all file in those sub folder, only keep the emptu directory. Thanks (4 Replies)
Discussion started by: android
4 Replies

9. Homework & Coursework Questions

Single command to delete a directory

Use and complete the template provided. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a command that will delete the john directory, and all of its contents. The problem I can't firgure out is how to create a absoute path to... (5 Replies)
Discussion started by: jorogon0099
5 Replies
Login or Register to Ask a Question