17

In Linux shell, am I able to create a directory but the name would be a string returned from another program? And if I am able, how to?

In particular I am asked to create a new directory in my Home, and its name would be the minute on my computer's clock. Let's suppose /home/john/01/, 01 is the minutes on my clock.

I was thinking something like pipeline date +"%M" | mkdir but I do not know how I am gonna put that returned number into the mkdir program.

Finally another idea came to my mind, something like this mkdir (date +"%M") but this as well is a mistake. Any help please?

muru
  • 193,181
  • 53
  • 473
  • 722
Mr T
  • 323
  • 1
  • 2
  • 11

4 Answers4

26

The concept you're looking for is command substitution, which uses the syntax $(command)

mkdir /home/john/$(date +%M)

You may also see the older 'backtick' syntax, `command`

steeldriver
  • 131,985
  • 21
  • 239
  • 326
20

mkdir $(date +%Y%m%d) or I personally use mkdir $(date +%Y%m%d_%H%M%S) for hh:mm:ss. date --help will give you the different formats if you need something more.

terdon
  • 98,183
  • 15
  • 197
  • 293
Philippe Gachoud
  • 5,800
  • 3
  • 41
  • 50
10

You can do it easily using following command:

$ min=$(date +"+%M"); mkdir $min
Humble
  • 153
  • 12
  • 3
    Also a nice way, good to know that you can declare a variable and then give that value to mkdir program, thanks! :) – Mr T Dec 31 '15 at 16:31
5

You can do that by typing the following command:

mkdir ~/$(date | awk -F':' '{print $2}')

The command creates a directory in home folder and gives the current minute as name.

Raphael
  • 7,985
  • 5
  • 34
  • 51