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

Note: We at TechSutram take our ethics very seriously. More information about it can be found here.
Mandar Pise Opinions expressed by techsutram contributors are their own. More details

Mandar is a seasoned software professional for more than a decade. He is Cloud, AI, IoT, Blockchain and Fintech enthusiast. He writes to benefit others from his experiences. His overall goal is to help people learn about the Cloud, AI, IoT, Blockchain and Fintech and the effects they will have economically and socially in the future.

No comments:

Post a Comment

    Your valuable comments are welcome. (Moderated)