-4

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)"?

3
  • I find just building the commands in MS Excel much much faster. Feb 27, 2013 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. Feb 27, 2013 at 9:50
  • for row in ls -1 ; do echo $row ; done Feb 27, 2013 at 10:56

1 Answer 1

5

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").

2
  • 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. Feb 27, 2013 at 19:28
  • 1
    Yes, everything gets treated as a string token unless marked otherwise. See edit.
    – pjc50
    Feb 27, 2013 at 23:30

You must log in to answer this question.

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