Power of Linux command line

  1. Use grep utility to filter out your output. Find all Java processes:
        ps -AF | grep java
  2. How to see terminal history (commands executed in the terminal):
        history
    The command supports scrolling with up / down arrows. Use filtering by substring to find specific command:
        history | grep ssh
  3. How to find files by name:
      from current folder: find . -name '*Listener.java'
      from root: find / -name '*.txt'
  4. How to find files by content:
        grep -lir 'dialog.show()' *.java

    arguments:
    -l show only files with matches
    -i ignore case
    -r recursive

  5. Remove files by extension in the current folder and its subfolders:
        find -name *.pyc -print0 | xargs -0 rm -rf
  6. Search substring in the file:
        less -pYour_pattern filename
  7. How to print file content:
        cat filename
        cat filename | less
        less filename
    less command supports scrolling (up / down keys), search (press '?' or '/', type pattern and press Enter). Press 'q' or Ctrl-C to exit less viewer.

    The scroll forward command (press 'F' in the less viewer) is useful when viewing logs. It scrolls forward if the end of file is reached but the file is growing.

  8. Use kill command to terminate a process. The command requires an integer argument - process ID. The ps command will help to find out the process id.

    For example, if you need to kill a Java process, execute:
        ps -AF | grep java
    and if you see only one process that's it! If there are several java processes then find yours by command line parameters, user or executable path.

    The 2nd column (1st integer) in each process row is the process ID.

  9. If you can't terminate your process using:
        kill processId
    then execute force kill:
        kill -9 processId
  10. ssh command - terminal on remote computer:
        ssh username@host/folder
    username and folder are optional.
  11. Transfer files from / to remote computer:
        scp username@remoteHost:remoteFile localFile
        scp localFile username@remoteHost:remoteFile
  12. How to launch a program on remote computer that the program will not be terminated when you close the remote computer session.
    • Open screen manager: screen -r -d
    • Create a new window in it: ctrl p c
    • Launch your program
    • Leave screen manager: ctrl p d
  13. Display free disk space:
        df
  14. How to see which program listens to specific port?
    netstat -lpn | grep port_no

    The -l parameter shows programs that listen for connections, -p shows processes PIDs, -n doesn't change port numbers to names.