|
In our previous month's article on scripting, we discussed the importance of regular expressions. But the question is, can the regular expressions alone make the scripting powerful? The answer is No. Then how can we make our scripts really powerful? It is indeed the core set of commands, along with simple and easy file operations, that imparts the functionality to scripting and makes it powerful.
Let us talk about few of the most popular and very useful commands and how they are associated with regular expressions. The first command in the queue is “ls”, which lists the file in various formats based on its options. “ls -l” is a long detailed listing, “ls -t” is a chronological listing, “ls -r” is a reverse sorted listing, and so on. However, the simplest command remains “ls” without any options. It is indeed the core set of commands along with simple and easy file operations, imparts the functionality to scripting and makes it powerful. Now, where do we look for more details on these commands or simply speaking, where do we get “Help” as in Windows terminology. “man” is the command, signifying manual pages. Type “man ls” in the shell prompt to get the complete manual page for “ls” command. For that matter, if you need help on man itself, type “man man”. Search Options Moving on, how do we search for a file in a folder and all its subfolders? Yes, I mean something similar to the search utility of Windows. This is how you do it in the scripting world: find <folder> -name <filename to search> Are you of the opinion that Windows GUI search is far easier? Might be, but not as powerful as scripting. Yes, here you can do a similar wildcard search as well. Say for example, to find all *.c files in a particular folder, type in: find <folder> -name “*.c” What if we want to find *.c plus all *.h files. Okay, put the regular expression: find <folder> -name “*.[ch]” How about searching something inside a file? Use “grep” command. And I bet this is far more powerful than the Windows stuff. The whole power of regular expressions is supported by it. Say, I want to find all the files containing decimal numbers of at least 5 digits. It's simple. Type in: grep -lr “[0-9]{5,}” <folder> You might wonder why you would like to do such a weird search. Believe me, if you are an automation guy or thinking of getting into it, this might be the simplest situation you would face. Then, what are these options of -l & -r? The “-l” option enables only listing of the files, otherwise even the lines from the files containing the patterns are displayed. The “-r” option is obvious, it tells grep to do a recursive search through it's subfolders and their subfolders and so on. If you simply want to list all lines in some files containing a pattern, replace the folder by file, as: grep -lr “[0-9]{5,}” <folder> So, grep is pretty simple and handy when it comes to finding a pattern and a file or files. Let us dive deeper and look at the innocent find. The way find works is first take a directory to find in, and then take a condition to apply on various parameters in the directory. It lists all the stuff matching the condition. The condition is built upon by ANDing all the sub-options, which in turn are conditions. Few popular ones are: | Option | Meaning | | -type f | Find all regular files | | -type d | Find all directories | | -perm <perm> | Find all with the <perm> permissions | | -mtime <-/+n> | Find all modified less than, equal to, or greater than n * 24 hrs ago | | -name <pattern> | Search file with the regexp <pattern> | | -size <size> | Search file of various sizes | Find is an extensive command and the above is just a glimpse of it. To get and enjoy the gory details, do a “man find” In our next article, we will further continue on more shell commands and the file indirections, which should equip us enough to use them in writing a real working script. |