How to use the command for (bash)

With the for command you can repeat a command for a number of times. This is useful when you have to do the same operations on the same class of files for many times. Here we go with some examples of what can be done:

# for i in $(ls) ; do echo $i ; done

This command shows the list of files of a directory in bash.

If you would transfer the html files in one directory to another directory there's no need to move them one by one with the GUI. You can do it like so:

# a=1 ; for i in `find . -iname ".html"` ; do echo "$i" ; cp "$i" /mnt/sda3/memory/; let a=a+1 ; echo "$a files copied" ; done

These commands copy the html files from the current directory to /mnt/sda3/memory/. This is useful when in the directory there are files with different extension.

With the following command line you can decompress all the zip files of a direcory:

# for i in .zip ; do unzip $i ; done

As you can see the for command can be very powerful.