Skip to content

Instantly share code, notes, and snippets.

@brijesh-deb
Last active August 7, 2021 14:52
Show Gist options
  • Save brijesh-deb/3869f5a027a12d1d93c27e526e7c36fe to your computer and use it in GitHub Desktop.
Save brijesh-deb/3869f5a027a12d1d93c27e526e7c36fe to your computer and use it in GitHub Desktop.
#cheatsheet #Linux

Linux CheatSheet

  • Check linux version: uname -a, lsb_release -a
  • List all open port in a server: nmap localhost
  • Change PATH: export PATH=$PATH:/usr/jdk1.7/bin
  • Java
    • Javac -cp xxx.jar yyy.java (compile)
    • Java -cp .:xxx.jar yyy (run)
  • Open a port: iptables -A FORWARD -p tcp --sport 80 -j ACCEPT
  • Check firewall setting: Iptables -L
  • Send a mail: mail -s “Hello world” you@youremailid.com [ provided SMTP server is runnin]
  • Check if SMTP server is running
    • Telnet 127.0.0.1 25 [checks if SMTP is running is localhost; 25 is default SMTP port]
  • Netstat
    • Netstat -tnlp [Lists diff processes running]
  • Kill a process: Kill <>
  • Remove a directory: rmdir <directory_name> or rm -r [ recursive]
  • Check log file: tail -f <xxx.log>
  • Unzip a .zip file: unzip .zip
  • Search for a specific process: ps -ef|grep jetty
  • Delete directory and content recursively: rm -r
  • Search for a string in directory/subdirectory recursively
    • Grep -ir "search me" . [ searches recursively starting present directory]
      • i: ignore case
      • r: recursive
  • Check size of a directory
    • du -ch [ c: grand total at the end, h : human readable]
    • du -ch|grep total [ gives only the total size of directory/subdirectory]
    • du -sh [ provides summary including size]
  • Display installed hard disk and size
    • Fdisk -l|grep '^Disk /dev/'
  • Display info about CPU and processing unit: lscpu
  • Check amount of used, free and total amount of RAM
    • Free -m
    • Cat /proc/meminfo
  • Check the current user: Whoami
  • Change to super user: su << for this need to have superuser password>>
  • Change to sudo user: sudo -u root –i << for this current user needs to have sudo privilege>>
  • Display hidden files in a folder: Ls -a
  • Shutdown the system (as root user)
    • Halt
    • Init 0
  • Reboot the system (as root user)
    • reboot
    • Init 6
  • Change user
    • Su <>
    • Su - [changes to root user, needs root user password]
  • List all environment variable: Env
  • List all process running (like Task Manager in Windows): Top
  • To know wherefrom a particular command is run: Which <> [eg: which mvn/ls/npm]
  • History of commands are stored in /home/de393369/.bash_history
  • To know details of some command: Man <> [ eg man cat]
  • Check user details: Finger <>
  • Check user id, group id of an user: Id <>
  • Important files
    • /etc/passwd - user details like userid, group id etc
    • /etc/shadow - user password details
    • /etc/login.def - has defaults like uid min/max, password size etc.
    • /etc/skel/* - files that will be copied to home dir when new user is added
    • /etc/group - has group details
    • /etc/sysconfig/network-scripts - has details of network card
  • Utility to check errors in password file
    • Pwck
    • Pwconv
  • Get list of users logged in a server: W
  • Who command: Who -b [ last boot time], Who -q [ show how many users are there]
  • Check what an user has logged in last: Last <>
  • Adding a user: Useradd <> [gets default setting from /etc/default/useradd]
  • Remove a user: Userdel -r <> [ deletes user and home directory]
  • Download from network/internet: wget
  • Untar a .gz file: tar -xzf hadoop-1.2.1.tar.gz
  • Remove a file from a WAR file
    • zip -d MCE.war WEB-INF/lib/axis.jar [ removes axis.jar from MCE.war]
    • zip -d MCE.war WEB-INF/lib/cxf*.jar [ removes all jars starting with cxf from MCE.war]
  • Add a file to a WAR file: zip -ur MCE.war WEB-INF/lib/axis.jar [ adds axis.jar to MCE.war]
  • Rename a file: mv oldfile newfile
  • Find string in specific files in directory: find . -name "module.xml"|xargs grep -i "org.springframework.spring"
  • List content of a jar file: jar -tvf
  • Vim command [ http://www.worldtimzone.com/res/vi.html]
    • Shft + G [ end of file]
    • :0 [move to the start of the file]
    • Cntl + U [ 1 page up]
    • Cntl + d [ 1 page down]
    • Highlight text in Vi editor to copy to windows clipboard
    • /xxxx [ search for a pattern; case sensitive]
  • #!/bin/ksh
    • 1st line in shell script
    • Tells the OS that ksh interpreter is to be used to run the script
  • Return to home directory: *cd ~*
  • Getting help: * --help *
  • Run a shell script: Bash XXXXX.sh
  • List of processes listening upon a port: Netstat -tulpn
  • Get IP address: ifconfig
  • Set environment variables
    • Set variables(eg. JAVA_HOME) in .bash_profile
      export JAVA_HOME=/usr/java/jdk1.8.0_91
      PATH=$PATH:$JAVA_HOME/bin
      export PATH
      
    • Run "source ~/.bash_profile"
  • Download from internet: wget [url]
  • Extract file from archive: tar xvf file.tar
  • Create a tar archive of all *.c file in current directory: tar cvf file.tar *.c

MacOS

  • List of all processes: netstat -vanp tcp
  • Find process using a port (e.g. 3000)
    • netstat -vanp tcp | grep 3000
    • lsof -i tcp:3000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment