Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

retrieve directory name from a $file variable

Posted on 2008-06-24
4
Medium Priority
?
302 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 85

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 2000 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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
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 …

926 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