Menu

Dipesh Majumdar

Blog and Paintings

Some useful Linux Commands - 3

  1. Do you really want to know if something is running on a port?

    Use this - 

    netstat -an | grep 80 | grep LISTEN
    tcp        0      0 :::80                       :::*                        LISTEN

  2. How to append a line to a text file?
    This is very easy:
    echo -e "Some Line which needs to be appended to a file\n" >> this_is_file.txt
  3. How to append aline to a text file? That means how to add a line in beggining of a file?
    (echo "Some Line to be added in beginning of file"; cat this_is_file.txt) > this_is_file_Temp.txt
    mv this_is_file_Temp.txt this_is_file.txt
  4. How to concatenate 3 files (file1,file2 and file3) - one after another - in one single file?
    There should be many ways - but this should work -
    ( cat file1.txt;cat file2.txt; cat file3.txt) >one_single_file.txt
  5. With xargs escape double quotes doesn't work. How to make it work? Hint:  Try using -0 with xargs
    app_user@hostname:~$ echo -e "{xyz:\"pqr\",y:"
    {xyz:"pqr",y:
    app_user@hostname:~$ echo -e "{xyz:\"pqr\",y:" |xargs echo
    {xyz:pqr,y:
    app_user@hostname:~$ echo -e "{xyz:\"pqr\",y:" |xargs -0 echo
    {xyz:"pqr",y:
  6. keep generating a random no. between 1 and 100 till the number is 50 or more

    i=$(( ( RANDOM % 100 ) + 1 ));while [ $i -lt 50 ]; do echo $i;i=$(( ( RANDOM % 100 ) + 1 ));echo $i;done;
  7. Another way to generate random number is
    echo $(shuf -i 1-100 -n 1)
    Use case (create a  k8s job which outputs hello if random no. between 1 to 100 is greater than 50 and says disgusting otherwise
    k -n mns run shufflejob --restart OnFailure --image busybox -- /bin/sh -c 'if [ $(shuf -i 1-100 -n 1) -gt 50 ];then echo hello;else echo disgusting; fi'

Go Back

Comment