Score:0

How can I use the same wildcard in different parts of a command?

ng flag

Say I have the following command which creates a symbolic link from one file to another giving the link the same name without the extension.

sudo ln -s ~/foobar/abc.sh ~/fubar/abc

How can I do the same thing, but for ~/foobar/*.sh?

user10489 avatar
in flag
Likely the sudo is unnecessary here. Using sudo when it isn't needed will eventually cause you problems.
Score:3
cn flag

Renaming files, and in your case, creating symlinks, mmv will suffice.

mmv -s '~/foobar/*.sh' '~/fubar/#1'
  • -s For the resulting link to aim back at the source, either the source name must begin with a '/', or the target must reside in either the current or the source directory. If none of these conditions are met, the link is refused. However, source and target can reside on different devices and the source can be a directory.
Score:2
cn flag
raj
for fn in ~/foobar/*.sh ; do
sudo ln -s $fn ~/fubar/`basename $fn .sh`
done

This assumes that none of the *.sh files has spaces if the filename. If it does, you need to quote both arguments to ln -s command:

for fn in ~/foobar/*.sh ; do
sudo ln -s "$fn" "~/fubar/`basename $fn .sh`"
done
raj avatar
cn flag
raj
@steeldriver the OP wants to create symlinks in **different** directory. Therefore I used `basename`. Your command doesn't do it.
hr flag
Oops... spelling > me
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.