SMKS SMKS - 2 months ago 12
Linux Question

Matching directories in a directory

Directory workspace contains like 5 projects:

project-1
project-2
project-3
project-4
project-5


I want to match all folders in this directory, but the array does not get populated when I use a variable:

workspace="~/workspace";

myDirectories=(${workspace}/project-*/);


If I pass it in manually it picks up the directories without a problem.

myDirectories=(~/workspace/project-*/);


I know it is something simple I am missing, but it is bugging me!

Answer

You have used quotes in workspace variable declaration like:

workspace="~/workspace"

So the ~ won't be expanded as the home directory, but would be treated literally.

You can:

  • Leave ~ outside quotes (note that, quotes are not strictly necessary in this case):

    workspace=~/"workspace"
    
  • Or use $HOME instetad of ~:

    workspace="$HOME"/workspace
    
  • Or use absolute paths

Comments