2

I often need to create folders that start with the current date.

For example 190627_ABCD.

I've tried to create an alias command that gets the date to be printed:

a newf 'mkdir `date '+%y%m%d'_`+='

But this results in printing up the += at the end, which I was trying to use as an instruction to concatenate.

The idea being that I can add the ABCD in the command. So to get 190627_ABCD I type in the shell:

newf ABCD

But it isn't working for me. Grateful for any assistance!

dessert
  • 39,392
  • 12
  • 115
  • 163
ZakS
  • 203
  • 2
  • 6

2 Answers2

2

As explained in Writing Aliases in csh and tcsh, you can use history expansion (since, in csh, history expansion occurs before alias expansion):

myhost:~> alias newf 'mkdir -v `date "+%y%m%d_\!:1"`'
myhost:~> newf ABCD
mkdir: created directory '190627_ABCD'
steeldriver
  • 131,985
  • 21
  • 239
  • 326
1

If you want your alias to accept multiple arguments (as well), use the history expansion !* and foreach to loop over them:

foreach name (!*)
  mkdir -v `date +%y%m%d_"$name"`
end

Example

myhost:~> alias newf 'foreach name (\!*)\
? mkdir -v `date +%y%m%d_"$name"`\
? end'
myhost:~> alias
newf    foreach name (!*)
mkdir -v `date +%y%m%d_"$name"`
end
myhost:~> newf a b
mkdir: created directory '190627_a'
mkdir: created directory '190627_b'
dessert
  • 39,392
  • 12
  • 115
  • 163