How to create nonexistent subdirectories recursively using Bash?

后端 未结 4 878
悲哀的现实
悲哀的现实 2020-11-30 17:59

I am creating a quick backup script that will dump some databases into a nice/neat directory structure and I realized that I need to test to make sure that the directories e

相关标签:
4条回答
  • 2020-11-30 18:07

    You can use the -p parameter, which is documented as:

    -p, --parents

    no error if existing, make parent directories as needed

    So:

    mkdir -p "$BACKUP_DIR/$client/$year/$month/$day"
    
    0 讨论(0)
  • 2020-11-30 18:08

    While existing answers definitely solve the purpose, if your'e looking to replicate nested directory structure under two different subdirectories, then you can do this

    mkdir -p {main,test}/{resources,scala/com/company}
    

    It will create following directory structure under the directory from where it is invoked

    ├── main
    │   ├── resources
    │   └── scala
    │       └── com
    │           └── company
    └── test
        ├── resources
        └── scala
            └── com
                └── company
    

    The example was taken from this link for creating SBT directory structure

    0 讨论(0)
  • 2020-11-30 18:10
    $ mkdir -p "$BACKUP_DIR/$client/$year/$month/$day"
    
    0 讨论(0)
  • 2020-11-30 18:11
    mkdir -p newDir/subdir{1..8}
    ls newDir/
    subdir1 subdir2 subdir3 subdir4 subdir5 subdir6 subdir7 subdir8
    
    0 讨论(0)
提交回复
热议问题