Wildcards (like *
) only expand into a list of matching filenames when they're not in quotes. One possibility would be to leave $2
unquoted in the function:
archive() { tar cjf "`(date '+%Y.%m.%d-%Hh%Mm%Ss')`-$1.tar.bz2" $2; }
...but that can have some unpleasant consequences. Most obviously, if you're trying to archive a folder with spaces or some other shell metacharacters in the name, it'll get confused because those aren't in quotes either.
A much better solution is to pass the folder+pattern unquoted in the first place, so it expands before the function gets invoked. You run
archive dev dev/*
...and the shell expands it to something like
archive dev dev/file1 dev/file2 dev/file3
But then you have to write the function so it doesn't just archive $2
, but all arguments starting at $2
. This isn't too hard; just record $1
, use shift
to remove it from the arg list, then use "$@"
to get all remaining arguments. And as long as we're using local variables, I'd put the destination filename in one as well (just for general neatness):
archive() {
local name_dest="$1"
local archive_name="$(date '+%Y.%m.%d-%Hh%Mm%Ss')-${name_dest}.tar.bz2"
shift
tar cjf "${archive_name}" "$@"
}