How to ... in Linux (in examples)

Assembled by Ross Bannister, University of Reading

Commands to find things out about your system

Find out which version of linux your computer is running

lsb_release -a
more /etc/*-release
uname -r

Find out your computer’s MAC address

ifconfig -a

Find out your computer’s ip address

ip addr show

Find out information about your processor(s) - e.g. 32- or 64-bit

more /proc/cpuinfo

Find out where a package is stored

which command
whereis command     #(gives further information)

Find out how much memory the computer has

more /proc/meminfo

Find a file

find . -name file-name -print

Does the computer have USB2.0 compatibility?

lspci | grep -i usb

System management

Configuration editor


Command for reboot

sudo reboot


sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade     #(to upgrade distribution)

Removing a package

sudo apt-get remove package-name

Create a bootable USB stick

Download required iso file
Load start-up disc creator

Variables in linux

Prompt variables

$PS1     #(for single line prompt variable)
$PS2     #(for multiple lines)
#E.g. in .bashrc:
PS1=$USER"> "

Variables useful in scripts

$#     #(number of arguments)
$1     #(first argument, e.g.)
$*     #(all arguments)

To return the Nth item in a list

item=$(echo $LIST | awk ’{print $’$N’}’)
item=${LIST[N]}  # N=0 is first item

Number of items in a list


To put the list of files in a list variable

# or

Bash parameter expansion

part1="${file#*-}"     # strip part before -
part2="${part1%%.*}"   # strip after the first .
part3="${file##*.}"    # get part after the last .

Bash string manipulation

STRING="text string"
${#STRING}        # Returns the string length
${STRING:pos}     # Returns the substring after pos (0 is start of string)
${STRING:pos:len} # Returns the substring after pos for len characters

Bash format specification

printf "format-descriptor" variable-to-output
# e.g.
printf "%02u" 4     # Will output 04

Bash arithmetic

let N1=$A+$B
let N2=$A-$B

Linux script examples

Store the number of lines in a file

TEMP=‘wc -l filename‘
N_FILES=$(echo $TEMP | awk ’{print $’1’}’) 

Create a file from within a script

cat > output-file-name << EOF
# Stuff-to-put-in-file

Examples of for loops

for VARIABLE in item1 item2 ...
  echo $VARIABLE
for NUM in {1..10}
  echo $NUM
for NUM in $(seq $START $FINISH); do
  echo $NUM

Testing files

if [-e file-name]; then
  echo "File exists"
  echo "File does not exist"
# (else branch optional)
# -e = file exist test
# -f = file is not a directory or device
# -s = file non-zero
# -d = file is a directory
# -b = file is a block device
# -c = file is a character device
# -p = file is a pipe
# -h = file is a symbolic link
# -L = file is a symbolic link
# -S = file is a socket
# -t = file is associated with terminal
# -r = file has read permissions
# -w = file has write permissions
# -x = file has execute permissions

Other examples of if conditions

if [ $a == $b ]; then
  echo “a equals b”

Read a file line-by-line

# Go through filename line-by-line
while read -r LINE; do
  let COUNT=$COUNT+1
  echo $COUNT $LINE
done < filename


As another user

sudo --login -u username
su username     #(preferred)


ctrl-alt-F2     #(login to shell)
ctrl-alt-F7     #(graphics mode)
ctrl-alt-T      #(terminal)


Encryption and decryption

gpg -o encrypted-file -c original-file            #(encryption)
gpg -o decrypted-file --decrypt encrypted-file    #(decription)

Change ownership of files and directories

chown user file-or-directory     #(changes owner)
chown :user file-or-directory    #(changes group)
chown user: file-or-directory    #(changes owner and group)

Change permissions

chmod abc file-or-directory  #(a for user, b for group, c for world)
# a,b,or c = 4*read + 2*write + execute
# (read, write, execute = 0 or 1)
# Examples
# Read only = 4
# Write only = 2
# Read and write = 6
# Read, write, and execute = 7
# Read and execute = 5

Set-up password-less ssh

# Generate a pair of keys
# Run from home directory on local host (press ENTER at all prompts, except ’y’):
# Append the file generated (.ssh/ to .ssh/authorized_keys on the remote host
# Run on local host:
ssh_copy_id -i ~/.ssh/ remote-host-name
# If the above does not work, then do the following
# Run on local host:
rsync ~/.ssh/ remote-host-name:.ssh/
# Log into remote host
# Run on remote host:
cat ~/.ssh/ >> ~/.ssh/authorized_keys

To add a password to a pdf file

pdftk file.pdf output password_protected_file.pdf owner_pw password user_pw a_different_password

File utilities

To split-up a large file into smaller ones

split --bytes=1M big-file small-files-prefix

To reassemble

cat small-files-prefix* > new-big-file

To remove the first x-1 characters from each line of a text file

cut -cx- file
# (will need to redirect output to another file)

Searching files

grep search-term file-name
# options
# -i ignore case
grep -P "\t" file-name     searches for special characters (here \t=tab)
grep "^vTAB"     alternative method to above (^v=ctrl-v, TAB=tab key)

Making a single output with columns from separate files

paste -d ’,;’ file-name1 file-name2 file-name3...     (,; ... are delimiting characters, redirect to output file)

substitute string1 with string2 (globally) in a file

sed -i ’s/string1/string2/g’ file-name
# (tab character is [\t])
# (The s command with sed allows you to use any delimitor (/ in the above)
#  whatever follows s is used)

Printing and document manipulation

Printing a pdf file from the command line to a printer connected to the computer

lpr file-name.pdf     (default printer, default settings)
lpr file-name.pdf -P printer-name -o number-up=2     (prints to specified printer with two pages per side)
# Other options:
# -#copies
# -o sides=two-sided-short-edge
# -o sides=two-sided-long-edge     (default)
# -o page-ranges=1,4-8
lp file-name.pdf -d printer-name -o number-up=2     (prints to specified printer with two pages per side)
# Other options:
# -n copies
# -o sides=two-sided-short-edge
# -o sides=two-sided-long-edge     (default)
# -o page-ranges=1,4-8

Converting a pdf file to multiple pages (e.g. for poster printing)

pdfposter -p2x2a4 Poster.pdf ManyA4Pages.pdf

Printing to a printer on another network (via ssh)

cat file-name.pdf | ssh user@host "lp -d printer-name -U user-name other-options (as above)"

To combine two or more pdfs

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=output-file.pdf input-file1.pdf input-file2.pdf ...

To remove pages from a pdf

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dQUIET -dFirstPage=first-page -dLastPage=last-page -sOutputFile=output-file.pdf input-file.pdf



# To use netcdf routines include the following in source code:
INCLUDE ’/usr/include/’
# To compile code using netcdf, include the following linker options:
-I/usr/include/ -L/usr/lib -lnetcdff -lnetcdf

C (file.c)


C++ (file.C or file.cpp)

# To use netcdf routines include the following in source code:
#include <netcdf.h>
# To compile code using netcdf:
g++ -I/usr/include file-name.cpp -L/usr/lib -lnetcdf_c++ -lnetcdf

Tips for hardware fixes

Enabling network

nmcli nm enable true

Increase resolution of the screen

# A machine has max resolution of 1024x768, but my monitor has 1280x1024
# What resolutions are currently available on the computer?
# Find out what resolution you require (e.g. monitor documentation)
# e.g. 1280x1024 Hor 63.98 kHz Vert 75
# If this required resolution is not available, then add the following commands to the .profile file
cvt 1280 1024 75
xrandr --newmode "1280x1024_75.00" 109.00 1280 1368 1496 1712 1024 1027 1034 1063 -hsync +vsync
# (N.B. the stuff following --newmode is copied from the output of the cvt command)
xrandr --addmode VGA1 1280x1024_75.00
# The new resolution will then appear in the list of monitor resolutions available.

Sound system utitities

pulse audio

Remapping the keyboard

# Example to map Sun keyboard AltGr key to act as right Ctrl key
# Place the following in .profile
setxkbmap -option ctrl:ralt_rctrl

Running commands

Repeat commands

watch -n delay-in-seconds linux-command
# or
while true; do
sleep delay-in-seconds
# linux-command

Output to standard output and another file simultaneously

linux-command | tee output-file

To list background jobs


Make a foreground job into a background job

# ctrl-z

Preparing a job to continue to run in the background after logging-out

linux-command &
# (process-id is displayed)
disown process-id
# (ignore comment about running jobs)
# or
nohup linux-command < Input-stuff > output-stuff

To suspend a job

kill -STOP process-id

To continue the job

kill -CONT process-id

Running a remote command via ssh

ssh user@host "remote command"

Configuring Wi-Fi

Eduroam settings

Set-up improvements for Ubuntu

Decrease swapiness

This swapiness is a number 0-100 (the lower the number the longer before Ubuntu uses disc swap space).
# To find out value:
cat /proc/sys/vm/swappiness
# To change the value:
sudo vi /etc/sysctl.conf
# and add the following line to the end of this file:
# Decrease swap space (from default 60) to a more reasonable level
# (this is for mechanical disc drives, set to 1 for solid-state discs)

Turn on the firewall

sudo ufw enable
# To check:
sudo ufw status verbose


edit → preferences → advanced → network → override automatic cache management, set to 50 MB

Upgrading software

Upgrading lyx beyond current ubuntu repository

sudo add-apt-repository ppa:lyx-devel/release
sudo apt-get update
sudo apt-get install lyx
Might need to repeat the middle command until no errors are reported. The system may complain when you next log-in (say “report fault” to not to be informed again).

Creating and using software libraries

Create a (non-shared) library in fortran

Suppose that we have some fortran subroutine in a number of files (e.g. file1.f90, file2.f90)
# Compile the fortran
f95 -c file1.f90 compiler-options
f95 -c file2.f90 compiler-options
# Make an archive
ar -rcs libexample.a file1.o file2.o
# Make available to everyone
sudo mv libexample.a /usr/local/lib

Compiling code to incorporate routines in the library

f95 UserCode.f90 -L/usr/local/lib -lexample compiler-options

Setting up ssh server

Notes from
Install the server software
sudo apt-get install openssh-server

Configure the software (file /etc/ssh/sshd_config). First make a backup copy
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.factory-defaults
sudo chmod a-w /etc/ssh/sshd_config.factory-defaults

Edit the file /etc/ssh/sshd_config:
Set the firewall to allow connection on port 22
sudo ufw allow 22

Restart the ssh daemon
sudo restart ssh
or if that does not work
sudo systemctl restart ssh

To check that sshd is running
ps -A | grep sshd
sudo ss -lnp | grep sshd