Find the answer to your Linux question:
Results 1 to 2 of 2
I am very new to bash and linux, and having a bear of a time extracting strings from filespecs. Most solutions give me the pwd, or the basename, but I ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    2

    Bash: Getting all parts of a filespec

    I am very new to bash and linux, and having a bear of a time extracting strings from filespecs. Most solutions give me the pwd, or the basename, but I am really ALSO need to get the directory path BEFORE the file or folder.

    Can someone please give me a simple bash routine or set of functions that will (complete with the correct way to call the function, and some sample of output expected):

    Take a partial OR fully qualified filespec passed to my script:
    eg. folderName OR fileName (could be either)
    eg /home/bin/folderName OR /home/bin/fileName.html

    and give me these key parts of the spec

    1) basename= (eg "folderName/" OR "fileName")
    2) file extension = (eg html, if applicable)
    3) preceding filePath= /home/bin/sub1/ (even if script called from current dir)


    Judging from the dozens of partial solutions aroud the web, I think this would help many. THanks in advance!

  2. #2
    Just Joined!
    Join Date
    May 2007
    Posts
    2

    Bash filespec 2

    No sooner did I post, than I came across this. I think it's the answer

    bash String Manipulations Issue 18

    EXCERPT FROM ARTICLE
    Given:
    foo=/tmp/my.dir/filename.tar.gz

    We can use these expressions:

    path = ${foo%/*}
    To get: /tmp/my.dir (like dirname)
    file = ${foo##*/}
    To get: filename.tar.gz (like basename)
    base = ${file%%.*}
    To get: filename
    ext = ${file#*.}
    To get: tar.gz

Posting Permissions

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