Linux Commands Cheat Sheet

Linux Commands Cheat Sheet
Photo by Gabriel Heinzer / Unsplash

This is a quick reference guide of common Linux commands for people in a hurry.

Installing new software

sudo apt-get update

This is the step that actually retrieves information about what packages can be installed, including what updates to currently installed packages packages are available, from Internet sources.

sudo apt-get install <package-name>

Find what we want to install.

apt-cache search <package-name>

This will install the package that you specify.

Example:

sudo apt-get install vim

If the package isn't present in the search. You can add a PPA (personal package archive) of that package directly from the web as follows:

sudo add-apt-repository ppa:<repo-name>
sudo apt-get update
sudo apt-get install <package-name>

Long List

Run ls -l in a directory. Here's a sample output:

-rw-r--r--   1 omkar  staff     53 Jan  5 21:01 package.json
drwxr-xr-x   6 omkar  staff    192 Jan  5 21:04 public
  1. First letter, d or - in this case, tells us whether the file is a directory or a regular file.
  2. rwxr-xr-x – permissions.
  3. 1 – number of hard-links.
  4. omkar – owner of the file.
  5. staff – to which group this file belongs to.
  6. 53 – size.
  7. Jan 5 21:01 – modification/creation date and time.
  8. package.json – file/directory name.

Permissions

Change the permission of a file.

chmod 754 <filename>

This sets the rwx permission for owner, rx for group, and r permission for everyone else.

Each number is translated to rwx based on the following calculation: 4 is read, 2 is write, and 1 is execute.

  • rwx = 4+5+1 = 7
  • rx = 4+1 = 5
  • rw = 4+2 = 6
  • no permission = 0
  • r = 4

Ports

Find which process is using a particular port.

$ sudo lsof -i :3306

mysqld  26270 mysql   21u  IPv4 3165371      0t0  TCP localhost:mysql (LISTEN)
$ sudo netstat -tupln

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      26270/mysqld        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      570/sshd            
tcp6       0      0 :::22                   :::*                    LISTEN      570/sshd            
udp        0      0 127.0.0.1:323           0.0.0.0:*                           29852/chronyd       
udp        0      0 0.0.0.0:68              0.0.0.0:*                           305/dhclient        
udp6       0      0 ::1:323                 :::*                                29852/chronyd

This post is a work-in-progress.