Results 1 to 5 of 5

Thread: trim left so the string only show the right info

  1. #1
    Join Date
    Aug 2005
    Location
    Sweden
    Beans
    373
    Distro
    Ubuntu

    trim left so the string only show the right info

    hi
    I have a variable that have a loong address to some folders.

    Example

    /var/www/html/site/directory/lib/folder_1

    I like to remove /var/www/html/site/directory/ so I only get lib/folder_1


    I have search but not find anything easy to understand.
    Some say I can use regex but I have problem to understand how it works (is that any good online generator for that?)
    Cazz

  2. #2
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    17,104
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: trim left so the string only show the right info

    I would use cdpath. It is a built-in variable for bash.
    You could use symbolic links too.
    Or you could use more wildcards to get there. If there is only 1 directory, then use /*/ where possible.
    cd /v*/w*/h*/s*/dir*/lib/f*

    Lots of choices.

    https://github.com/jlevy/the-art-of-command-line

  3. #3
    Join Date
    Jan 2017
    Beans
    86

    Re: trim left so the string only show the right info

    As TheFu says, "lots of choices":
    Code:
    $ var="/var/www/html/site/directory/lib/folder_1"
    $ echo "${var#*/*/*/*/*/*/}"
    lib/folder_1
    $ echo $var | cut -d'/' -f7-
    lib/folder_1

  4. #4
    Join Date
    Aug 2005
    Location
    Sweden
    Beans
    373
    Distro
    Ubuntu

    Re: trim left so the string only show the right info

    Wow it was fantastic, did never know I can do that. Thanks alot it works for me
    Cazz

  5. #5
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    17,104
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: trim left so the string only show the right info

    You can pattern match on any part of the path/file name. The pattern used just needs to be unique.
    cd /*r/w*/*l/*/*y/*i*/*1

    But for long paths, I use symbolic links from my HOME or cdpath. cdpath is powerful, but it is best used for unusual directories, not directories like "lib". Play with it and you'll quickly learn why.

    I think this stuff is covered in beginning Linux courses ... and books. It is in my classes, but I don't know about others.
    Last edited by TheFu; 3 Weeks Ago at 01:24 AM.

Bookmarks

Posting Permissions

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