Using wildcards to match a directory in bash

8,852

Turn on the extglob shell option and then cd /home/!(user*)/asdf

Share:
8,852

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin almost 2 years

    Lets say the folder structure is like so:

    /home/
    --user1/asdf
    --user2/asdf1234
    --user3/asdf325234
    --cool/asdf
    

    How could I change to asdf1234 without specifying the user? For example:

    cd /home/*/asdf1234
    

    How can I use "not" in bash? For example, lets say I want to go to /home/cool but use not capability:

    cd /home/!user*/asdf is this possible?

    Are these bash tricks possible?

    • Emanuel Berg
      Emanuel Berg over 11 years
      Hm, are you in fact asking two questions? The first question works as you describe it (if permissions are OK), with the * wildcard. Not in bash may look like ! or ^; check out man bash and search for those. Last, please make an effort to write better titles to your future questions.
  • poige
    poige over 11 years
    Is there any particular reason for using sed 1q instead of head -n1?
  • pradeepchhetri
    pradeepchhetri over 11 years
    @poige: i like sed and awk :) no other reason.