Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In Windows DOS, I can use the for command to massively increase productivity and performs zillions of things at once. In PowerShell, its even better. On Linux, I struggle.

How would I make a batch of soft-links to some folders on another drive?

If do ln -s 2013-02-* it does something totally nuts and makes links in a random folder.

So I am trying to use command-substitution but testing with echo first, the problem is that

echo 'prepend/'`ls /drive/ -1`

Only prepends the preprend/ string to the first entry. This tells me that although I got command-substitution working with a remove command once, it doesn't work the way I thought (producing a single command for each ls result).

I'm back to needing a for loop. How do I do "foreach $row in (ls -1) do (something)"?

share|improve this question
I find just building the commands in MS Excel much much faster. – Luke Puplett Feb 27 '13 at 9:49
What kind of for loop have you tried under bash? You only show wildcard expansion. Also, with your comment above I am not sure anymore if you want help or are just trolling. – Benjamin Bannier Feb 27 '13 at 9:50
for row in ls -1 ; do echo $row ; done – Shadur Feb 27 '13 at 10:56
up vote 4 down vote accepted

Starting in the folder you want links in, this will work:

ln -s sourcefolder/2013-02-* .

There is of course a perfectly reasonable "for" command:

for var in path/* ; do echo "prepend$var" ; done

Edit: the general means of using one program to provide a list of arguments to another is backticks: ``, so you can do:

for x in `ls -1` ; do program $x ; done

However, ls has some magic so that you don't need to put -1 if you're using it as input to another program; and you can just get a list of files with a * pattern ("glob").

share|improve this answer
Sorry, I have a book and it doesn't have a good example and its very difficult to find help online since for is a noise word, and for --help doesn't work... how do I put a command in the for, such as ln -1? It seems to treat my command as string tokens. – Luke Puplett Feb 27 '13 at 19:28
Yes, everything gets treated as a string token unless marked otherwise. See edit. – pjc50 Feb 27 '13 at 23:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .