Results 1 to 6 of 6
Hi,
I would like to use the current folder name as a title of a text document, but I could not print out the folder name that I am in.
...
- 11-30-2009 #1Just Joined!
- Join Date
- Nov 2009
- Posts
- 8
Extract the folder name that you are in
Hi,
I would like to use the current folder name as a title of a text document, but I could not print out the folder name that I am in.
E.g.,
How can I extract only the name of "mystar1" and write it to a file?Code:$pwd $/Users/users/umut/mystar1
Thanks a lot
Umut
- 11-30-2009 #2
here is the code that extracts the filename and prints it to stdout
not necessarily the easiest/best way, but it worksCode:i=2 while [ 1 ] ; do file1=`echo $PWD | cut -d "/" -s -f${i}` let i=i+1 file2=`echo $PWD | cut -d "/" -s -f${i}` if [ "$file2" = "" ] then echo $file1 break fi done
- 11-30-2009 #3Just Joined!
- Join Date
- Aug 2005
- Posts
- 65
basename $(pwd) >file
- 12-01-2009 #4Just Joined!
- Join Date
- Nov 2009
- Posts
- 8
They are both working great, thanks a lot.
Umut
- 12-01-2009 #5Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
Modern shells allow forms of string extraction expressions. These are useful because they do not require external programs such basename, dirname, etc. For example with bash:
producing:Code:#!/usr/bin/env bash # @(#) s3 Demonstrate emulation of old basename command. echo echo "(Versions displayed with local utility \"version\")" version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) basename set -o nounset echo mypath="/Users/users/umut/mystar1" echo " Current directory is emulated as $mypath" echo echo " Final component extracted with basename:" echo "$( basename $mypath)" echo echo " Final component extracted with string expression:" echo "${mypath##*/}" exit 0
One drawback is that one needs to learn the syntax, and initially it can be confusing.Code:% ./s3 (Versions displayed with local utility "version") OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64 Distribution : Debian GNU/Linux 5.0 GNU bash 3.2.39 basename (GNU coreutils) 6.10 Current directory is emulated as /Users/users/umut/mystar1 Final component extracted with basename: mystar1 Final component extracted with string expression: mystar1
See section "Parameter Expansion" in man bash ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 12-01-2009 #6Just Joined!
- Join Date
- Aug 2005
- Posts
- 65
thx drl
it was really so nice to know that


Reply With Quote