I wrote a support script for cp, called CP (note capital letters) that's intended to do exactly this. So your command would actually look like: mkdir -p "/my directory/name with/spaces" & cp "my filename with spaces.txt" "$_"
If you're dealing with names with spaces, you'll need to quote them so that the different words aren't treated as different arguments to mkdir or cp. However, I don't know of any modern shells that don't support $_ certainly Bash, Dash, and zsh all do.Ī final note: the command I've given at the start of this answer assumes that your directory names don't have spaces in. Note that $_ is not part of the POSIX standard, so theoretically a Unix variant might have a shell that doesn't support this construct. So the cp command expands to cp myfile.txt /foo/bar, which copies myfile.txt into the newly created /foo/bar directory. In this case, that's the /foo/bar we passed to mkdir. Per the Bash manual, it:Įxpands to the last argument to the previous command This means the cp command won't try to execute if mkdir fails for one of the many reasons it might fail.įinally, the $_ we pass as the second argument to cp is a "special parameter" which can be handy for avoiding repeating long arguments (like file paths) without having to store them in a variable.
#Linux foobar alternative manual
The & list operator, as documented in the POSIX standard (or the Bash manual if you prefer), has the effect that cp myfile.txt $_ only gets executed if mkdir -p /foo/bar executes successfully. (Without -p, it will instead throw an error. Meaning that when calling mkdir -p /foo/bar, mkdir will create /foo and /foo/bar if /foo doesn't already exist. The -p argument, per the docs, will cause mkdir toĬreate any missing intermediate pathname components The mkdir utility, as specified in the POSIX standard, makes directories. There's a few components to this, so I'll cover all the syntax step by step. To copy myfile.txt to /foo/bar/myfile.txt, use: mkdir -p /foo/bar & cp myfile.txt $_