Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Carlo Lobrano
Carlo Lobrano

Posted on

     

Let mkdir create parents directories

I noticed that it is more than a year since the last post, so I thought that a small new content would have been easier to write.
This in particular refers to an extension I wrote some time ago for themkdir system call to make it a bit more proactive 😄.

90% of the times, I usemkdir to create a new folder in the current directory, so the few times I want to create a new folder in a deeper path I always forget to add the--parent flag.

me/blog$mkdirfoo/bin/barmkdir: cannot create directory ‘foo/bin/bar’: No such file or directory
Enter fullscreen modeExit fullscreen mode

as theMan-pages would tell you, since the parent directoryfoo/bin does not exist,mkdir fails to createbar, but what ifmkdir were so kind to tell me just that, and suggests to add the flag on my behalf? 😄

This is actually somehow easy.

First of all, you need a way to define new functions in your shell. I usezsh, but inbash would be the same: write a function definition in a file and source it in.bashrc or.zshrc.

This comes from my.zshrc

## Functions and aliases#source ~/.dot/.config/cconf/zsh/functions.zsh
Enter fullscreen modeExit fullscreen mode

What to do formkdir?

  1. override it with a custom function ➡️function mkdir() {}
  2. find the parent directorymkdir expects to exist already ➡️echo $1 | grep -E -q '[\S+/]+'
  3. if it doesn't exist, ask if it must be created or not ➡️echo "Press ENTER to run mkdir with --parents."

Ok, point 2 probably requires some explaination, so let's back to the example above

me/blog$mkdirfoo/bin/bar
Enter fullscreen modeExit fullscreen mode

bar is the folder to be created, whilefoo/bin/ is the parent.
Thegrep expression above looks for any sequence of substrings ending with slash/:foo/,foo/bar/, etc., but notbar, orfoo, which would be a directory in thecurrent folder.

So putting all these pieces together:

functionmkdir(){# extend mkdir with custom features# propose adding "--parents" flag.echo$1 |grep-E-q'[\S+/]+'if[[$?== 0&&!-d$1]];then# one of the directories in $1 path do not exist# suggest adding "--parents" flag.echo"# Some parents in$1 do not exist. Press ENTER to run mkdir with --parents."read        command mkdir--parents$@echo"# done"elsecommand mkdir$@fi}
Enter fullscreen modeExit fullscreen mode

Let's try again

me/blog$mkdirfoo/bin/bar# Some parents in foo/bin/bar do not exist. Press ENTER to run mkdir with --parents.# doneme/blog$tree |grep-e foo-e bar-e bin├── foo│   └── bin│       └── bar
Enter fullscreen modeExit fullscreen mode

Of course in a similar manner you can overriderm as well (seehere).

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Engineer & OpenSource contributor.Love Linux, Python and Golang, addicted to (Neo)VIM
  • Location
    Italy
  • Work
    Software Engineer in Italy
  • Joined

More fromCarlo Lobrano

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp