LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash to find and disect current working Directory (https://www.linuxquestions.org/questions/linux-general-1/bash-to-find-and-disect-current-working-directory-496284/)

carl0ski 10-28-2006 01:27 AM

Bash to find and disect current working Directory
 
hi

PWD command shows the full path relative to root
however i hoping to find just the last segment
/home/carl0ski/.kde/share/apps/amarok

amarok

pwd |cut -d/ -f5

achieves but i need something that works on varying directory depths.


If you ineterested heres the reason
Amarok creates a .directory file to make the album cover the folder image, linking to the amarok cache in you home directory

however under varying users and computers i prefer the image to be stored in the actual album directory.
i use the folder name (Album name) as the Image name rather that the random characters amarok uses in the cache.
Code:

for cd in *
do cd "$cd"
pwd
image=`cat .directory |grep Icon| cut -d= -f2`

#name of the album based on last path name
album=`pwd |cut -d/ -f5`

#copy source image to the ablum folder and rename it to  album.png
cp "$image" ./"$album".png

#replace Icon=/home/carl0ski/.kde/share/apps/amarok/ with
#the one contained in the albums directory
##Backing up original
mv .directory .directory.backup
echo [Desktop Entry] >.directory
echo Icon=./${album}.png>>.directory

#back to root fo next Directory
cd ../
pwd
done


uselpa 10-28-2006 03:41 AM

try
Code:

basename $(pwd)

carl0ski 10-28-2006 04:27 AM

Quote:

Originally Posted by uselpa
try
Code:

basename $(pwd)

cheers

filler to 10 character response

out of interest, is their an opposite command to basename?

uselpa 10-28-2006 04:28 AM

Something like `dirname` ?

carl0ski 10-28-2006 04:36 AM

Quote:

Originally Posted by uselpa
Something like `dirname` ?

yep that's the one :)


All times are GMT -5. The time now is 12:23 PM.