How to delete all the files and folders inside all the directories except some specific directory?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delete all the files and folders inside all the directories except some specific directory?
# 1  
Old 07-27-2015
How to delete all the files and folders inside all the directories except some specific directory?

hi,

i have a requirement to delete all the files from all the directories except some specific directories like archive and log.
for example:
there are following directories such as
A B C D Archive E Log F
which contains some sub directories and files. The requirement is to delete all the files and subfolders except the contents of Archive and Log.
# 2  
Old 07-27-2015
Why would you want to do this?
-- this appears to be a homework-type of problem, and not real-life.
What have you tried so far?
# 3  
Old 07-28-2015
Quote:
Originally Posted by joeyg
Why would you want to do this?
-- this appears to be a homework-type of problem, and not real-life.
What have you tried so far?
Not always. I had almost the same scenario where I had to create a script in my office for the another department guys.
# 4  
Old 07-29-2015
Its not a homework question. I got a small task of clearing all the old files from all the directories except archive. Instead of going to each 200+ directories and deleting the files manually, thought of writing a script. Can someone please help.
# 5  
Old 07-29-2015
Code:
find . -type d \( -name archive -o -name log \) -prune -o -type f -print

If this correctly prints the files to be deleted then replace -print by -delete or -exec rm -f {} +
This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 07-29-2015
Quote:
Originally Posted by MadeInGermany
Code:
find . -type d \( -name archive -o -name log \) -prune -o -type f -print

If this correctly prints the files to be deleted then replace -print by -delete or -exec rm -f {} +
Thanks for the solution. Can you please explain it?
# 7  
Old 07-29-2015
find . -type d
Searches for directories recursively starting from current folder.

\( -name archive -o -name log \) -prune
If directory is named archive or log then remove (prune) it from output of find command.

-o -type f -print
Otherwise print all files from the directories, except those pruned.

As MadeInGermany stated,
Quote:
If this correctly prints the files to be deleted then replace -print by -delete or -exec rm -f {} +
which will delete the files instead of printing them to the standard output.
These 3 Users Gave Thanks to mjf For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to list all folders in a specific directory

The below bash is trying to list the folders in a specific directory. It seems close but adds the path to the filename, which basename could strip off I think, but not sure why it writes the text file created? This list of folders in the directory will be used later, but needs to only be the... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Move specific folders and subfolders in a directory

I am trying to move specific folders and subfolders within a directory using the below. I can see the folders to move and they are at the location, but I am getting an error. Thank you :). mv -v /home/cmccabe/Desktop/NGS/API/6-10-2016{bam/{validation,coverage},bedtools /media/cmccabe/"My... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurrence of a specific word

he following are the files available in my directory RSK_123_20141113_031500.txt RSK_123_20141113_081500.txt RSK_126_20141113_041500.txt RSK_126_20141113_081800.txt RSK_128_20141113_091600.txt Here, "RSK" is file prefix and 123 is a code name and rest is just timestamp of the file when its... (7 Replies)
Discussion started by: kridhick
7 Replies

4. Shell Programming and Scripting

Delete all files with specific extension in directory tree

I'm sure this has been asked many times, but a search didn't turn up a definitive best method for this (if there ever is such a thing). I have been using rsync to back up my main data directory, but I have accumulated a large number of older backups that I don't need. All of the files I don't... (14 Replies)
Discussion started by: LMHmedchem
14 Replies

5. Shell Programming and Scripting

Find specific files only in current directory...not sub directories AIX

Hi, I have to find specific files only in the current directory...not in the sub directories. But when I use Find command ... it searches all the files in the current directory as well as in the subdirectories. I am using AIX-UNIX machine.Please help.. I am using the below command. And i am... (2 Replies)
Discussion started by: aakishore
2 Replies

6. UNIX for Dummies Questions & Answers

[SOLVED] Delete files and folders under given directory

I have a requirement to delete the files and folders under a given directory. my directory structure is like this.. Data | A(Directory) |_PDF(Directory)----pdf files |_XML()Directory --xml files |--files | B(Directory) |_PDF(Directory)----pdf files |_XML()Directory --xml files ... (1 Reply)
Discussion started by: ramse8pc
1 Replies

7. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurence of a specific word

Hello, I have several files in a specific directory. A specific string in one file can occur in another files. If this string is in other files. Then all the files in which this string occured should be deleted and only 1 file should remain with the string. Example. file1 ShortName "Blue... (2 Replies)
Discussion started by: premier_de
2 Replies

8. Shell Programming and Scripting

Copying specific files from remote m/c to specific folders

Hi All, I am trying to rsync some of the latest files from remote m/c to my local linux box. Folder structure in my remote m/c looks like this /pub/Nightly/Package/ROLL/WIN /pub/Nightly/Package/SOLL/sol /pub/Nightly/Package/SOLL/linux Each of the folder contains gzip files which on daily... (0 Replies)
Discussion started by: jhoomsharabi
0 Replies

9. Solaris

How to delete Directory and inside files using Find command

I am using the following Command to delete Directory with contents. But this command is deleting inside files only not directories. is there any change need in my command? find -type f -mtime +3 -exec rm -r {} \; Thanks (3 Replies)
Discussion started by: bmkreddy
3 Replies

10. Shell Programming and Scripting

delete files in specific directory

i have a directory "ABC" with lots of old files and sub directories in it. the issue now is i want to delete away files which are older than 15 days in "ABC" without deleting the files in the sub directories and without deleting the sub directory. i tried using find command but it will drill down... (2 Replies)
Discussion started by: legato
2 Replies
Login or Register to Ask a Question