Find the answer to your Linux question:
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. ...
  1. #1
    Just 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.,

    Code:
    $pwd
    $/Users/users/umut/mystar1
    How can I extract only the name of "mystar1" and write it to a file?

    Thanks a lot

    Umut

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    here is the code that extracts the filename and prints it to stdout
    Code:
    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
    not necessarily the easiest/best way, but it works

  3. #3
    Just Joined!
    Join Date
    Aug 2005
    Posts
    65
    basename $(pwd) >file

  4. #4
    Just Joined!
    Join Date
    Nov 2009
    Posts
    8
    They are both working great, thanks a lot.

    Umut

  5. #5
    drl
    drl is online now
    Linux Engineer drl's Avatar
    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:
    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
    producing:
    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
    One drawback is that one needs to learn the syntax, and initially it can be confusing.

    See section "Parameter Expansion" in man bash ... cheers, drl
    Welcome - 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 )

  6. #6
    Just Joined!
    Join Date
    Aug 2005
    Posts
    65
    thx drl
    it was really so nice to know that

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...