LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-07-2009, 08:19 PM   #1
brian.m
LQ Newbie
 
Registered: Nov 2008
Posts: 11

Rep: Reputation: 0
FIND: Only directories which contain specific files


I am looking for a way to return a listing of directories which contain a specific file type (i.e., *.jpg). I have a directory structure as follows:

Code:
- Folder 1
  - Folder 1a
     1.jpg
     2.jpg
- Folder 2
  - Folder 2a
     3.jpg
     4.jpg
- Folder 3
  - Folder 3a
     index.html
I would like to be able to create symbolic links to directories which only contain jpg files (Folder 3a would be ignored). For example, /Folder 1/Folder 1a/ would end up being linked to /tmp/Folder 1a/.


I image the command would be similar to what is noted below, except this creates a link to directories with other files types in there as well. Again, I only want to create links for folders with contain jpg images.
Code:
find ~/home/username/*/*/*.jpg -type d -exec ln -s {} ~/tmp/ \;
Any ideas? Am I taking the wrong approach to this?
 
Old 05-07-2009, 08:44 PM   #2
rayfordj
Member
 
Registered: Feb 2008
Location: Texas
Distribution: Fedora, RHEL, CentOS
Posts: 488

Rep: Reputation: 78
something like this might work for you but you can encounter errors creating links if it already exists...
Code:
find /home/username/tld/ -type f -iname \*.jpg -exec dirname {} \; | xargs -I{} ln -s {} ~/tmp/
also if you had multiple folders, like in your example except something along the lines of:
Code:
- Folder 1
  - Folder 1a
     1.jpg
     2.jpg
  - Folder 2a
     5.jpg
     6.jpg
- Folder 2
  - Folder 2a
     3.jpg
     4.jpg
- Folder 3
  - Folder 3a
     index.html
  - Folder 1a
     7.jpg
you would have no way of creating multiple links of the same name in the destination directory


i'd probably do something along the lines of calling a script from find's -exec option and work from there to create the link named off of some hash of the path...

Code:
find /home/username/tld/ -type f -iname \*.jpg -exec /home/username/bin/linkit.sh {} \;
linkit.sh
Code:
#!/bin/bash
#
# create links in ~/tmp/ to directories with .jpg files
DSTDIR="/home/username/tmp/"
IN="$1"
DIRNAME=$(dirname ${IN})
SHANAME=$(echo ${DIRNAME} | sha1sum | cut -d" " -f1)
LINKNAME="$(DSTDIR}/${SHANAME}"

if [ ! -d ${DSTDIR} ]; then
  mkdir ${DSTDIR}
fi

if [ ! -h ${LINKNAME} ]
  ln -s ${DIRNAME} ${LINKNAME}
fi
wrote this on the fly, so use at own risk ;-)
also, recommend calling all binaries from absolute path rather than relative (or at least defining a sane PATH variable in the script)
 
Old 05-07-2009, 09:25 PM   #3
brian.m
LQ Newbie
 
Registered: Nov 2008
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by rayfordj View Post
something like this might work for you but you can encounter errors creating links if it already exists...
Code:
find /home/username/tld/ -type f -iname \*.jpg -exec dirname {} \; | xargs -I{} ln -s {} ~/tmp/
This suggestion worked perfectly... thank you so much!
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find files containing specific text? SurfTurtle Linux - Newbie 7 01-05-2008 05:57 AM
How to find those files having specific string??? saeed Red Hat 4 07-19-2006 11:50 AM
du on specific files from find command (use xargs?) fireman949 Linux - Software 2 07-10-2005 01:42 AM
how to find a specific file over several *.tgz files eeyoree Slackware 6 08-09-2004 12:37 AM
Find files, directories that are own by specific user mikeshn Linux - General 2 02-12-2004 03:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:55 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
Open Source Consulting | Domain Registration