Very handy one liner shell tricks every linux/unix developer should know – 2019

If you know these one liners then your life will be much easier while working on Linux/Unix env.

1. For loop for generating data

If you need a dummy data file to test your new script you just finished writing then below line comes in handy. You can replace `some content $i’ with any data you want as per your requirement.

for i in {1..100}; do echo some content here $i; done >> tempFile.txt

2. For loop for duplicating files

Imagine where you need 100’s or even 1000’s of files generated in a folder for doing a performance test of your Nodejs (any for that matter) application

for i in {1..1000}; do cp tempFile.txt  ./tempFile_$i.txt; done 

3. List only top 10 old/new files

In servers its common that we will have 1000’s of files in working directories but if you want to get list of older/newer files by order then below command is the savior

Older files first

ls -rt | head
#-t sort by time
#-r reverse that order

With date and other meta info

Newer files

For newer files, use below command. the file tempFile.100.txt is the most latest one.

With date and other meta info

4. While loop – read a file line by line and modify it on fly then write to another file

If you have a very big file and you want to modify something in each line then below command will be helpful. Most of the times sed or awk will be good for the job, but if you are not familiar with sed or awk syntax then a simple while loop can do the job.

5. Print contents of a .zip text file without extracting it

This is very useful command while searching in bunch of zipped log files

zcat -c fileName.zip

6. Search contents of a .zip text file without extracting it

Use pipe combination to above command to search in its contents as below

zcat -c fileName.zip | grep pattern_here

Navigate through the contents back and forth

zcat -c fileName.zip | less

You can try experimenting above list with different pipe combinations to see the actual power of each, and there are many such but these are part of my top list while am working in our linux boxes. I will update this list as I come across.

What are your life saviors? please let me know in comments, I’m happy to add them here.