In software development, there is the concept of SRP or the Single Responsibility Principle. When working with the individual components of software like classes or functions you should ensure that they each do only one thing, and do one thing very effectively. In Linux, this practice could not be more apparent. Linux is filled to the brim with programs that embrace SRP to the extreme. Most included programs do one particular thing and do it quite well.

There are programs for reading files, editing files, moving files, etc. Each one of these programs has a distinct purpose and doesn’t try to include the kitchen sink of functionality. To add in a lot of different functionality would degrade the quality of the originally intended purpose.

But what happens if you need all kinds of different functionality wrapped up into one single command? Linux commands are meant to be chained together with the help of pipes or through scripting. You can take the output of one command and quite literally “pipe” it into the input of another. You can also write a Bash script that composes many of these commands together to form one cohesive set of actions.

Let’s look at some great one-liner examples of different commands that incorporate a multitude of functionality in a compact package. Some of these are piped together while others are multi-line actions composed into a single line to be more concise.

1. Watch a file without watch

Depending on the distribution you’re working with, you may or may not have the watch program. This is a great little utility that lets you literally “watch” a particular file and display the contents every n number of seconds. Not all distributions have watch, but fear not there is another way. This is super handy if you need extra customization for your watching logic or if you’re working on resource-constrained embedded platforms.

while true; do cat file.txt; echo -e "\n\n$(date)\n\n"; sleep 1; done

A simple while loop takes care of similar watch-like logic. You can change the number of newlines displayed between the output, insert other commands after $(date) and update the sleep interval to a frequency of your liking.

#technology #developer #linux #programming #bash

5 Simple Linux Tools For Enhanced Productivity
1.45 GEEK