Linux Command Line Reference

Install Linux

Topic Index

Create, Read, and Edit Files

TOP      Reference Links: Touch - Vi - Nano - Cat - Less - More

Create an empty file using TOUCHtouch <file location><file name>

Create and edit a new or existing text file using VI:  vi <file location><file name>
Vi and Nano are both text editors that can be used interchangably. Both come pre-installed by most distributions. Nano is a bit more noob friendly while Vi is significantly more capable.

Use CAT to show the contents of a file:  cat /etc/fstab

Use MORE instead of CAT to show one screen full at a time:  more /var/log/syslog

Find Things

TOP      Reference Links: command

Users and Groups

TOP      Reference Links: Adduser - Addgroup - Deluser - Delgroup

List of all current users:  cat /etc/passwd
List of all current groups:  cat /etc/group

Create new user using ADDUSER: sudo adduser <options> <username>

Common options:
  • -c adds a comment, usually the user's full name
  • -e sets an expiration date for the account using format YYYY-MM-DD
  • -m creates user home directory
  • -M suppresses creation of a user home directory
  • -G adds user to an existing group, add multiple groups separated by commas without spaces

Example: sudo adduser -c "William L. Smith" -e 2019-12-25 -m -G www-data,video wlsmith

Delete a user:  sudo deluser <username>
Delete a user and all of their files:  sudo deluser --remove-all-files <username>

Change user password:  sudo passwd <username> (will be prompted for root password then user password)

Create new group:  sudo addgroup <groupname>
Delete a group (works only when no users assigned to group):   sudo delgroup <groupname>

Add existing user to a group:  sudo adduser <username> <groupname>
Remove user to a group:  sudo deluser <username> <groupname>

Manage Ownership and Rights

TOP      Reference Links:  LS - Chown - Chmod

File permissions are divided up two ways.  Control of files may be individually controlled for users, groups, and everyone else (others).  Additionally, for each type of user it is possible to specify whether each may read, write, or execute a given file.
Use LS to list the files and ownership information:  ls -al
File Ownership and Permissions

Modify file/folder ownership using CHOWN:  sudo chown <options> <username:groupname> </path/file>

Common options:
  • -R: operate on files and directories recursively
  • -v: output a diagnostic for every file processed

Modify file permissions using CHMOD:  sudo chmod <options> <octal-mode> </path/file>

Common options:
  • -R: operate on files and directories recursively
  • -v: output a diagnostic for every file processed

The table below shows how to calculate the appropriate octal-mode value for your intended use case.

File Permission Octal Math

Strategy for user/group permissions for /var/www

The goal here is to allow myself to edit files on my local machine and then transfer them directly to /var/www.  Additionally, this allows me to edit files directly on the server as myself (versus root).

  • Set user and group permissions for /var/www/html
  • Add myself to the www-data group: sudo adduser wwwbill www-data
  • Change ownership of /var/www to me: sudo chown wwwbill:www-data -R /var/www
  • Change file and directory permissions: sudo chmod 2755 -R /var/www
  • Still need a reference/citation for the "sticky bit" code above.

Partitions and Formatting

TOP      Reference Links: Fdisk

List all partitions:  sudo fdisk -l

Manage Files and Folders

TOP      Reference Links: Mkdir - Rm - Mv

Create a directory:  mkdir <directory>

Remove a folder and it's contents (including subfolders):   rm <options> <file/folder name>

Common options:
  • -i: prompt for confirmation of every file
  • -f: force, ignore nonexistent files, never prompt
  • -R: remove directories their contents recursively
  • -v: output a diagnostic for every file processed
  • Single File Example: rm ~/textfile.txt
  • Folder Example: rm -Rf /var/www/html/

Move a file:  mv <current file location> <new file location>

Example: mv ~/sandbox/index.htm /var/www/html/

Using MV to rename a file:  mv <current filename> <new filename>

Example: mv /etc/fstab /etc/fstab.archive

Bash Aliases

TOP      Reference Links: Ref 1 - Ref 2 - Ref 3

Make an alias that works only in the current session:  alias <shortcut>='>full command>

Example: alias up='sudo apt-get update && sudo apt-get -y upgrade'

Use entries in ~/.bash_aliases to make aliases permanent.
Some Linux distributions store aliases in ~/.bashrc
These are some of the aliases that I use:


TOP      Reference Links: Cron

Edit current user CRON tasks:  crontab -e
Edit root user CRON tasks:  sudo crontab -e

Format for cron tasks:

Each of the sections is separated by a space, with the final section having one or more spaces in it. No spaces are allowed within Sections 1-5, only between them. Sections 1-5 are used to indicate when and how often you want the task to be executed. This is how a cron job is laid out: minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command 01 04 1 1 1 /usr/bin/somedirectory/somecommand

Crontab Options

  • The -l option causes the current crontab to be displayed on standard output.
  • The -r option causes the current crontab to be removed.
  • The -e option is used to edit the current crontab using the editor specified by the EDITOR environment variable.

Cron task for saving the current US and SE NOAA radar image every 10 minutes:   */10 * * * * /home/carl/radar/

Application and Process Management

TOP      Reference Links: Ps

Create list of all installed packages: &nbps; dpkg --get-selections | grep -v deinstall > ~/All-Packages-20160224.txt

List running processes:  ps -ef

List processes being run by user 'carl':  pgrep -l -u carl

List processes called 'htop':  ps -ef | grep htop

Kill running process

Kill running process with "htop" - just use the arrow keys to highlight the offending process and press <F9>

List all installed packages: dpkg --get-selections > list.txt
To reinstall all these packages in another computer: sudo dpkg --set-selections < list.txt && sudo apt-get -u dselect-upgrade

Hardware Information & Storage Management

TOP      Reference Links: Ls

List Drive Partitions by UUID:  ls -l /dev/disk/by-uuid/

Remember to do uname with several options

Mount USB Stick on Linux

  • Plug in the USB key and figure out what it is called: sudo fdisk -l
  • Output from this command will be something like "/dev/sdb1"
  • Create a mount point for the USB key: sudo mkdir /media/usb
  • Mount the USB key: sudo mount /dev/sdb1 /media/usb

To unmount the USB key:  sudo umount /media/usb

Network Management

TOP      Reference Links: Netstat

Listening Ports:  sudo netstat -anltp | grep "LISTEN"

Time and Date

TOP      Reference Links: Date

Set Date/Time:  sudo date -s "2 OCT 2006 18:00:00"

OS and Kernel Info

TOP      Reference Links: Uname

Kernel:  uname -a

Distro Info:

ls_release -a
cat /etc/lsb_release
cat /etc/
cat /etc/*-release

Scheduled Backup Using Rsync

TOP      Reference Links: Rsync - Rsync Examples

Syntax:  rsync [options]... [Source] [Destination]

My desktop example for backing up to an external, but locally mounted, drive:

sudo rsync -arv --exclude '/home/carl/.local/share/Trash' --exclude '/home/carl/.cache' --exclude '/home/carl/.config' /home/carl /media/carl/backup

Don't schedule this as a cron job unless the destination location is always going to be available.
I set this up as a .bash_alias bu.