Bash loop one liners that are must have


There is often a need to write to do simple tasks such as to process set of files, querying packages etc. Below are the one liners that can be used often not only on Linux and Unices but also on Windows (refer to above image :) ). If someone finds Windows word confusing then please refer to one of my older posts viz. Bash environment on Windows: This is not Cygwin

Following useful one liner will display name of every file in <your_dir> and search for <pattern> in every file in current <your_dir> and display file name if match is found.

# for FILE in `ls <your_dir>`;do  grep <pattern> $FILE && echo $FILE; done

It is often required to create infinite running scripts such as generating system load in software testing. It is fairly straight forward to use while loop in such cases.

# while test 1;do echo “Infinite loop” > /dev/null; done

Replace echo statement with any other load generating scripts if this single one liner does not suffice.

It is also possible to search and replace across different files based on specific pattern. But that will be too much of work in shell programming. However there is fairly easy Perl liner that is available and posted long back as a post Search and Replace across multiple files

By scimandar

1 comment:

Your valuable comments are welcome. (Comments will be moderated.)