Solved

retrieve directory name from a $file variable

Posted on 2008-06-24
4
284 Views
Last Modified: 2013-12-26
Hi

I started this script, in which I would like to get the directory name where $file is located, and put it in a variable. I only want the name of the directory, not the path...

Can someone help ?

Cheers

Terence
#!/bin/sh
thumbdir=/Users/terencepires/Documents/Skydog/Images/for_web/thumb
 
for file in $(find . -type f -iname "*.jpg" -o -iname "*.tif" -o -iname "*.tiff" -o -name "*.gif"); do 
 
#imageMagick format and size conversion 
convert $file -quality 10 -resize 25% $thumbdir$file
done

Open in new window

0
Comment
Question by:terencepires
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 21854483
directory=`dirname $file`
0
 
LVL 1

Author Comment

by:terencepires
ID: 21854563
ok but it gives this :

./__Freddy_Lynxx
./__Flamin_Groovies
./__Eddie_and_Ze_Hot_Rods

I need to remove the ./ how can i do ?
0
 
LVL 1

Author Comment

by:terencepires
ID: 21854880
ok I did it like this : file=`echo $file | cut -c3-70`

thanks anyway

Terence
0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 21860488
Two better solutions are to do

find *

instead of

find .

or

file=`echo $file | sed "s/^..//"`

instead of

file=`echo $file | cut -c3-70`

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The error "There was an error performing the update" occurred on a Mac OS X client workstation running  Symantec AntiVirus for Mac (http://www.symantec.com/business/products/purchasing.jsp?pcid=pcat_security&pvid=825_1) - the Enterprise product vers…
Syslogd is a utility that traps and logs messages sent by running processes. It is configured with the syslog.conf file, which consists of lines containing a pair of fields: "the selector field which specifies the types of messages and priorities to…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question