How do I create directories named after each month?
With -I
, xargs
gets one argument per line as opposed to the default of one argument per (blank or newline delimited, possibly quoted) word without -I
(and implies -n
). So in your example date
is called only once with {}
expanded to the whole output of echo
(which is on one line), minus the trailing newline.
Here you can do (note that that -d
is a GNU extension):
printf '%s\n' {1..12}/01 | xargs -I {} date -d {} +%b | xargs mkdir --
(note that it won't work correctly in locales where month name abbreviations contain spaces or quote characters; with GNU xargs
, you can work around that by using xargs -d '\n' mkdir --
)
Now, to get the list of month abbreviations in your locale, querying the locale directly would make more sense:
(IFS=';'; set -o noglob; mkdir -- $(locale abmon))
(see also locale -k LC_TIME
to see all the locale data in the LC_TIME
category).
Or natively in zsh
:
zmodload zsh/langinfo
mkdir -- ${(v)langinfo[(I)ABMON_*]}
At least on GNU systems, in some locales, month abbreviations are padded to fixed width with spaces:
$ LC_ALL=et_EE.UTF-8 locale title abmon
Estonian locale for Estonia
jaan ;veebr;märts;apr ;mai ;juuni;juuli;aug ;sept ;okt ;nov ;dets
$ LC_ALL=zh_TW.UTF-8 locale title abmon
Chinese locale for Taiwan R.O.C.
1月; 2月; 3月; 4月; 5月; 6月; 7月; 8月; 9月;10月;11月;12月
You may want to remove that padding.
The leading spaces would be removed by xargs -I
, but not the trailing ones. With zsh
:
zmodload zsh/langinfo
set -o extendedglob
mkdir -- ${${${(v)langinfo[(I)ABMON*]}##[[:space:]]#}%%[[:space:]]#}
Try a loop?
$ for m in {1..12}; do
> date -d "$m"/01 +%b
> done
jan
feb
mar
apr
maj
jun
jul
aug
sep
okt
nov
dec
If you want to make a directory for each month, I would do something like:
for m in {1..12}; do newdir=$(date -d "$m"/01 +%b); mkdir "$newdir"; done
Your command does not work, because of using -I
changes the delimiter of xargs:
-I replace-str
Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character.
You can add -d " "
to xargs to make it work. But you don't even need -I{}
in your case:
Try this,
echo {1..12}/01 | xargs -n1 date +%b -d | xargs mkdir