Friday, November 25, 2011

Bash one-line for loops

Bash has become the standard shell on many Linux distributions.
One of my favorite features in Bash are the one-line loops, specially the for ones. Even though you might have listened many times that Bash scripting can become a nightmare due to its complicated and rigid syntax, one-line loops can save you a lot of time.  
As an example, imagine that you have two folders and you need to copy all the files from one folder to the other:
$ ls
folder1  folder2
$ cp folder1/* ../other_folder
$ cp folder2/* ../other_folder
That's alright, but now imagine that you have 1000 folders. Will you manually execute 1000 commands? The answer is obviously no. Here's when one-line bash loops become handy:
$ for i in `ls -d */`;do cp $i/* ../other_folder;done

Let's have a look at how the previous line works. The basic syntax for a for loop in bash is: 
for variable in some_list; do command(s); done 
In our example, we iterate over a list of directories in the current folder using the command `ls -d */`. Note that the "`" is used to execute a command and to be able to assign its output to a variable.

As a second example, imagine that you need to count the number of lines of a bunch of files whose names are comprised of a prefix, such as, "FILE_NAME_" and a suffix that can be a number ranging from 1 to N.
A possible solution using one-line bash loops would be:
$ for i in {1..N}; do cat "FILE_NAME_$i" | wc -l; done
You could also have a list of file names in a file and iterate over them:
$ cat files.list
$ for file in `cat files.list`;do echo $file; done

I hope this post will help you to take advantage of one of my favorite features in Bash :)

No comments:

Post a Comment