Advanced Linux Command Ease your Life

Vineet Kumar
3 min readAug 6, 2020
  1. Delete large File content immediately: Suppose large file name is access.log then different way to remove large content
    a. # > access.log
    b. # true > access.log
    c. # cat /dev/null > access.log
    d. # cp /dev/null access.log
    e. # dd if=/dev/null of=access.log
    g. # echo -n “” > access.log
    h. # truncate -s 0 access.log
  2. Search top 5 large file size files:
    #find /var/lib/docker/ -type f -exec du -Sh {} + | sort -rh | head -n 5
    # du -a /var/lib/docker/ | sort -rh | head -5
  3. Replacing words or characters: If you are working with any text file then to replace every instance of any word say “version” with “story” in
    myfile.txt, you can use sed command as:
    # sed 's/version/story/g' myfile.txt
    Additionally, if you want to ignore character case then you may use gi instead of g as:
    # sed 's/version/story/gi' myfile.txt
    To Replaca a whole line with input start of line parametre
    #sed -i -e '/^#PasswordAuthentication/s/^.*$/PasswordAuthentication yes/' /etc/ssh/sshd_config
    Delete Pirtucular lines
    sed -i -e '259,260 d' settings.php
    Add a sentence after a pirticular line (For Example add ‘charset’ => ‘utf8mb4’ after 258 line no in setting.php file)
    sed -i -e "258 a 'charset' => 'utf8mb4'," sites/default/setting.php
    NOTE:
    including sed 1q (quit after first line), sed -n 1p (only print first line, but read everything), awk ‘FNR == 1’ (only print first line, but again, read everything)
  4. Install packages with no proxy in UBUNTU LINUX
    apt-get -o Acquire::http::proxy=false install kgraphviewer
  5. The name nohup stands for “no hangup.” The hangup (HUP) signal, which is normally sent to a process to inform it the user has logged off (or “hung up”), is intercepted by nohup, allowing the process to continue running.
    nohup command [command-argument …]
    Run the command mycommand. It does not receive input. All output, including any error messages, is written to the file nohup.out in the working directory, or in your home directory. If mycommand is running when you log out or close the terminal, mycommand does not terminate.
    To check logs :cat nohup.out Example: #nohup rsync -arzvp <soure_file> <Destination_file_path> &
    Above command will excecute in background and Now check the progress with the following command
    #tailf -f nohup.out
  6. Troubleshooting and Collecting Information for linux: dmesg records all the system changes done or occur in real time.
    #dmesg | tail -20
    #dmesg | grep usb or dmesg | grep eth
    #watch “dmesg | tail -20”
  7. USE tmux utility run Run Process in Background after close the session:
    Tmux sessions are persistent, which means that programs running in Tmux will continue to run even if you get disconnected.To Create new session
    tmux new -s mysession
    First press CTRL+b then press d. This detaches your session. You then press CTRL+d to log out of ssh. Your detached session still keeps running on the server. You can attach it later by following command when you log on to server.
    tmux attach-session -t my-session
    More commands is as below
    Show all sessions: $tmux ls
    Attach to particular session: $tmux a -t <session-name>
    Split Pane with Horizontal layout : Ctrl + b %
    Split Pane with Vertical layout : Ctrl + b "
    Toggle pane zoom: CTRL+b z
    Close current pane: CTRL+b x
  8. awk uses (To print rows and column):
    Ex: awk 'NR==1{print $0}' /etc/*release
    NR=Retrieves total count of processed records i.e which line no will be print (indirectly says that it is responsible for print rows . For Ex. NR>1 print after line no 1) .
    print $0 : $0 for the whole line. i.e. responsible for print column separated by tab by default . (By default it will take tab separater)
    $1 for the first field
    $n for the nth field
    -F fs To specify a file separator.Output is separated by comma then use -F;
    awk -F; 'NR==1{print $0}' /etc/*release
    You can type your awk script in a file and specify that file using the -f option.
    awk -F: -f testfile /etc/passwd
  9. Inline For Loop (Use for loop as a command Line):
    Ex:Take Backup of pdf files
    for i in *.pdf ; do cp $i / backup / ; done;
    Ex: Rename file
    for i in *.txt; do mv $i `basename $i sh`.bkp;done
    Ex: Remove all docker containers
    for i in docker ps -aq | awk '{print $1}' ; do docker rm -f $i ; done;
  10. Enjoy

--

--