1

I want to create files with the current date as prefix and some string as the remaining part of the filename.

Something like this:

touch `date +"%Y-%m-%d-"$1`.md hello

where $1 should pick up hello and create me a file called 2014-3-3-Hello.md.

alexia
  • 14,440
  • 8
  • 42
  • 52
Sindhu S
  • 942
  • 1
  • 10
  • 23
  • If you want to use `touch` you need to write your own script (unless it already supports this) – keyser Dec 28 '14 at 13:27
  • Depends on how you interprete "possible". If you want touch to do the name generation, you are out of luck. If you just want to create a file with this name, that's easy. – cmaster - reinstate monica Dec 28 '14 at 13:28
  • is that you want to create a file in a script or you need it to run as command from command line? – SMA Dec 28 '14 at 13:28
  • i want to create a file with current date in the format i give it but also append a string to the filename before creating it. I want to run a one liner (run as command from command line). Thanks. – Sindhu S Dec 28 '14 at 13:30

2 Answers2

5

You can use command substitution:

touch "$(date +"%Y-%m-%d-")hello.md"

If you want to name a number of files, all ending with .md, just wrap the thing in a for loop:

for baseName in hello world foo bar ; do
    touch "$(date +"%Y-%m-%d-")$baseName.md"
done

That will create four files with names like 2014-3-3-hello.md, 2014-3-3-world.md, etc.

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
4

For convenience, you may want to define a custom function (called touchdatemd below) for that:

$ touchdatemd () { touch $(date +"%Y-%m-%d-")"$1".md; }

Test:

$ mkdir test && cd test
$ touchdatemd hello
$ touchdatemd "I love pie"
$ touchdatemd bye
$ ls
2014-12-28-bye.md   2014-12-28-I love pie.md    2014-12-28-hello.md
jub0bs
  • 60,866
  • 25
  • 183
  • 186