LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 08-01-2009, 11:45 AM   #1
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
listing directories containing files of certain type


I'm looking to create a list of directories that contain files of a certain type, such as .mp3 files. I've got it mostly working with this command:
Code:
find . -name "*.mp3" -printf "%h\n"|sort -u
The problem is that it also lists parent directories as well. For example, "./dira/dirb/dirc/song.mp3" would give me

./dira
./dira/dirb
./dira/dirb/dirc

I only want the last entry. Is there any simple way to filter out the unwanted ones?

Note that sometimes the parent directories can contain files as well, so I can't just remove partial duplicates from the list after it's compiled (i.e. there are times when ./dira/dirb would also be a legitimate hit).
 
Old 08-01-2009, 11:55 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
I cannot reproduce this behaviour. It prints only the name of the directories actually containing mp3 files, even if they are two or three levels below the current dir:
Code:
$ find . -name "*.mp3"
./test1.mp3
./dirname/files/songs/test2.mp3
./dirname/files/test3.mp3
$ find . -name "*.mp3" -printf "%h\n"
.
./dirname/files/songs
./dirname/files
Can you provide more details about the actual content of the directories in the search path?
 
Old 08-01-2009, 12:05 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Original Poster
Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Doh! Actually, I've just discovered that it is working the way I want.

After posting I took a closer look at the list I had generated and found that all the "duplicates" it listed actually do have matching files in them. My memory of what was in the upper directories was a bit off and I failed to notice that it was only listing some of the upper directories.

Forget I ever asked anything.
 
  


Reply

Tags
directories, find, list


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
putty display loops on listing directories with many files, through expect script vineet.k Linux - General 5 03-13-2007 09:44 AM
Recursive directories listing cdog Linux - General 11 12-09-2006 07:04 AM
samba - some directories not listing any files tomekp Linux - Networking 6 10-24-2006 08:55 AM
Listing all directories only anjanesh Linux - Newbie 5 06-20-2006 03:23 AM
Display wide listing of files and directories only? northfield Linux - General 3 08-21-2002 06:28 PM


All times are GMT -5. The time now is 02:33 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration