LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-21-2009, 12:49 PM   #1
gedi1
LQ Newbie
 
Registered: Apr 2001
Distribution: RedHat 9.0
Posts: 23

Rep: Reputation: 15
Find folders with ONLY one specific file in and delete file and folder - How ?


Redhat Linux Enterprise 3

I have a work related issue.

Many folders within a subdirectory some of which have lots of data in and some of which have only one specific file called produkt.fil inside.
I need a command to find and delete those folders that contain ONLY the file produkt.fil - if other files exist (doesnt matter what they are) then they should be left alone. Note: produkt.fil exists in all of the folders always.

Thanks in advance.

Ged
 
Old 10-21-2009, 01:02 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Have you identified any promising tools for the task? Have you netsearched for similar questions (there are quite a few here on LQ)? What have you tried? What were the results?
 
Old 10-21-2009, 01:12 PM   #3
gedi1
LQ Newbie
 
Registered: Apr 2001
Distribution: RedHat 9.0
Posts: 23

Original Poster
Rep: Reputation: 15
yes I can find threads that show how to find a specific file, but I cant find one that tests if there is only that specific file there i.e. tests if there are any other files there or not. I have searched the web and LQ but cant seem to find it.
 
Old 10-21-2009, 01:22 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
If you got a list of all the files in a directory and that list was "produkt.fil" what would that tell you?

If you got a list of all the files in a directory and that list was not "produkt.fil" what would that tell you?
 
Old 10-21-2009, 09:09 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
As per Catkin, plus note also that

ls |wc -l

only counts non-hidden files
 
Old 10-21-2009, 09:09 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by gedi1 View Post
Redhat Linux Enterprise 3

I have a work related issue.

Many folders within a subdirectory some of which have lots of data in and some of which have only one specific file called produkt.fil inside.
I need a command to find and delete those folders that contain ONLY the file produkt.fil - if other files exist (doesnt matter what they are) then they should be left alone. Note: produkt.fil exists in all of the folders always.

Thanks in advance.

Ged
since its your work. i leave it to do it yourself. here's a sketch of what you can do
Code:
find directories recursively | while read each_directory
do
   var = list each_directory | line_count 
   if var equal 1 and filename == <yourfile_to_find> ; then
     remove each_directory
   fi
done
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
find -exec command to recursively delete files and folders in folders with X name Joan Murt Linux - Newbie 2 07-08-2009 05:35 PM
find a folder containing a specific file amicke Linux - General 3 04-20-2009 11:04 AM
find a folder not containing a specific file itzfritz Linux - Server 2 09-10-2008 12:23 PM
Delete specific lines of a file beginning with a certain letter docaia Programming 4 08-25-2008 12:04 AM
Keep specific file types, delete the rest ? jchambers Programming 5 11-26-2007 07:25 PM


All times are GMT -5. The time now is 03:51 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration