0

I am trying to create a file using the mkdir -p command.

Using mkdir -p ~/1/2/3/4, how would I put files (w,x) into /2, and (y,z) into /3 all in a single command?

I know I could use a touch command after creating the directories, though I am trying to figure out how to do all of this with regular expressions using a single command.

As a newbie trying to figure his way around, a solution to this problem would be greatly appreciated. Thanks!

muru
  • 69,900
  • 13
  • 192
  • 292
  • Do you mean one command, or a one liner? `mkdir` only creates directories. You could do a one liner like `mkdir -p ~/1/2/3/4;touch ~/1/2/w ~/1/2/x; touch ~/1/2/3/y ~/1/2/3/z` or `mkdir -p ~/1/2/3/4;touch ~/1/2/w ~/1/2/x ~/1/2/3/y ~/1/2/3/z` but I guess you already know that. Are you saying that you want to replace that long-winded one liner with a shorter regex of some sort? – Greenonline Nov 20 '21 at 03:46
  • Does this answer your question? [How to create a file and parent directories in one command?](https://unix.stackexchange.com/questions/305844/how-to-create-a-file-and-parent-directories-in-one-command) – Greenonline Nov 20 '21 at 03:58
  • I meant one command. Like you said, I was looking to use a shorter regex if it's possible. – Opensource1397 Nov 20 '21 at 05:07
  • 1
    The words "file", "mkdir" and "regular expressions" don't have much (if anything) to do with each other. – muru Nov 20 '21 at 05:21
  • mkdir is one of the core Unix commands. Unix commands are designed to do one thing, do it well, and work with each other. They are not huge "applications" doing anything that comes to mind, which are common today. mkdir's job is to create a directory. – Vilinkameni Nov 20 '21 at 08:53

1 Answers1

0

Given that you’re creating directories with no files in them in the end (4), I wouldn’t expect a general-purpose command to handle your files-and-parent-directories scenario exactly anyway.

Without creating a new command, the best you can do is

mkdir -p ~/1/2/3/4; touch ~/1/2/{w,x,3/{y,z}}

The latter isn’t a regex, it’s brace expansion.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164