Remove/replace line break in bash output

Sometimes it’s much more convenient (or required) to feed a list as one line with separated elements instead of one item per line. Example:

You have this (just a random list of files):

You want this:

Tools like ls and echo have their own parameters to handle this individually, but I found two simple solutions that work for ALL bash output: one makes use of tr and the other of xargs. The latter makes sense if you want the list of elements as command line parameters for another tool (i.e. open all *.c files in this directory in a text editor) while tr is useful if you want to pipe the list to another program or into a file.



ls | grep “\.c$” | tr ‘\n’ ‘ ‘

In this example tr takes the input and replaces each new line character ('\n') with a space character (' '). tr has a few interesting options to manipulate output. The description from the man page pretty much sums it up:

“Translate, squeeze, and/or delete characters from standard input, writing to standard output.”

Found in this reply on Ask Ubuntu by user roadmr.

How to highlight output with grep instead of filtering

Looking for bits of information swirling past you on the screen can be tiring. Thanks to grep one can filter out unneeded text. But what if you want to display everything and just highlight text that you think is relevant? Image ping for some information and an error gets filtered out since you didn’t explicitly search for it (because all YOUR code free from errors, right? 😉 ). Well, turns out grep can do just that:

An example that I just used:

Credit goes to user holygeek on stack overflow.

UPDATE: One function to extract them all!

UPDATE: There is a Python script that has some extra features (like extraction into a dedicated directory and changing of permissions).

I seriously fail to remember all those tar options for each of the supported archives 🙂 So I’m happy to have found a bash function that will simply chose the right command based on the file extension:

Simply put the function at the bottom of your .bashrc file. Either close and re-open the terminal you’re using or type source ~/.bashrc to refresh the changes of your bash environment.

Now you can just type extract and the function will extract the archive and cd into it. Super time-and-brain-energy-saver! 😛 I found this function on the Ubuntu forums in a post by user graysky.