Quantcast
Channel: User Kamil Maciorowski - Super User
Viewing all articles
Browse latest Browse all 645

Answer by Kamil Maciorowski for How to cd into the directory a tarball decompresses to?

$
0
0

cd: too many arguments means that cd got more than one operand.

In general this does not have to be an error; depending on the shell, cd may or may not accept more than one operand. In Bash cdused to work in such cases, but apparently your Bash is "recent enough" and thus it doesn't. (But even if it did, it would use exactly one of the operands, which in general might or might not be the one you really want.)

So the problem is that install-tl-* in cd install-tl-* expands to multiple arguments and you really want to use just one of them.

An easy way to pick one result is to set positional parameters of the shell to the result(s) of the expansion and then work with them:

set -- install-tl-*cd -- "$1"      # use the first result

or

set -- install-tl-*cd -- "${!#}"   # use the last result

(These -- are not really needed in this particular case, they may be handy if the pattern is different.)

Note that the order of the results depends on the locale (LC_COLLATE). 20240521 in your example pathname looks like a date in a format good for sorting; in every sane locale such pathnames should sort well. To get to the directory with the latest date, use the snippet with cd "${!#}".

${!#} is not portable. It works in Bash and I'm using it here because I know your shell is Bash. In case you ever need a portable way, here's a trick to get the last positional parameter without ${!#}:

for last; do :; donecd -- "$last"

You may find these shell functions useful:

cdf () {   # f is for first   cd -- "$1"}cdl () {   # l is for last   cd -- "${!#}"}

Inside a shell function the arguments passed to the function become positional parameters. Now you can use cdf install-tl-* and cdl install-tl-*. One of them (most likely the latter) should do what you want.


Viewing all articles
Browse latest Browse all 645

Trending Articles