What does the expression "for x in *;" mean?

3

I saw this expression in a SOEN question, but I did not quite understand what it does.

for x in *;
do
    echo $x;
done

This printed the list of folders in the directory I was in.

Is that what this expression is for?

What would be this * asterisco?

    
asked by anonymous 26.08.2015 / 18:44

2 answers

3

This, written the way it is, does the same thing as the command ls

* is being used as a wildcard.

Try this way:

for x in *.zip;do echo $x; done;

You can use the same rules you use in ls , for example:

for x in ./*/*.zip;do echo $x; done;

or

for x in /var/log/*.log;do tail $x; done;

As a practical example, the following code clears the contents of the logs in the development environment:

for x in /var/log/*.log;do echo '' > $x; done;

    
26.08.2015 / 18:49
3

The asterisk indicates the reading of everything in the folder.

The variable X receives the names of the files individually, which are printed by the echo constructor.

    
26.08.2015 / 18:49