Linux Cheat SheetΒΆ

If you are not familiar with the Linux command, below lists some basic commands that will get you moving.

DIRECTORY NAVIGATION

# To go up one level of the directory tree.  (Change into the parent directory.)
cd ..

# Go to the $HOME directory
cd

# Change to the /etc directory
cd /etc

FILE AND DIRECTORY COMMANDS

# List all files in a long listing (detailed) format
ls -al

# Display the present working directory
pwd

# Create a directory 
mkdir directory
# e.g. create directory called 'MODELS'
mkdir MODELS

# Remove (delete) file
rm file
# e.g. remove file called 'nanopods.dat'
rm nanopods.dat

# Remove the directory and its contents recursively
rm -r directory

# Force removal of file without prompting for confirmation
rm -f file

# Forcefully remove directory recursively
rm -rf directory

# For careful users: remove file with confirmation
rm -i file

# Copy file1 to file2
cp file1 file2

# Copy source_directory recursively to destination. If destination exists, copy source_directory into destination, otherwise create destination with the contents of source_directory.
cp -r source_directory destination

# Rename or move file1 to file2. If file2 is an existing directory, move file1 into directory file2
mv file1 file2

# Create symbolic link to linkname
ln -s /path/to/file linkname

# View the contents of file
cat file

# Browse through a text file
less file

# Display the first 10 lines of file
head file

# Display the last 10 lines of file
tail file

# Display the last 10 lines of file and "follow" the file as it grows.
tail -f file

# Opening a file using a text editor (e.g.'emacs')
emacs file 

FILE PERMISSIONS

Linux chmod example
        PERMISSION      EXAMPLE

         U   G   W
        rwx rwx rwx     chmod 777 filename
        rwx rwx r-x     chmod 775 filename
        rwx r-x r-x     chmod 755 filename
        rw- rw- r--     chmod 664 filename
        rw- r-- r--     chmod 644 filename

# NOTE: Use 777 sparingly!

        LEGEND
        U = User
        G = Group
        W = World

        r = Read
        w = write
        x = execute
        - = no access

ARCHIVES (TAR FILES)

# Create tar file named 'archive.tar' containing directory.
tar cf archive.tar directory
# e.g. create tar file 'simulations_run1.tar' containing all files in directory SIMULATIONS_RUN1
tar cf simulations_run1.tar SIMULATIONS_RUN1

# Extract the contents from archive.tar.
tar xf archive.tar

# Create a gzip compressed tar file name archive.tar.gz.
tar czf archive.tar.gz directory

# Extract a gzip compressed tar file.
tar xzf archive.tar.gz

# Create a tar file with bzip2 compression
tar cjf archive.tar.bz2 directory

# Extract a bzip2 compressed tar file.
tar xjf archive.tar.bz2

SEARCH

# Search for pattern in file
grep 'pattern' file
# e.g. search for the words 'Total time' in the file OUTPUT
grep 'Total time' OUTPUT

# e.g. search for the 10 lines following the words 'Total time' in file OUTPUT
grep -A10 'Total time' OUTPUT

# e.g. search for the 10 lines before the words 'Total time' in file OUTPUT
grep -B10 'Total time' OUTPUT

# e.g. search for the 10 lines following the words 'Total time' in file OUTPUT and print to screen the final line of the 10
grep -A10 'Total time' OUTPUT | tail -1

# e.g. search for all 'Final energy' values in file OUTPUT and print the 5th column to screen
grep 'Final energy' OUTPUT | awk '{print $5}'

# e.g. search for all 'Final energy' values in file OUTPUT and print the 5th column to file final_energy.dat
grep 'Final energy' OUTPUT | awk '{print $5}' >> final_energy.dat

# e.g. from file forces_energy.dat copy the 1st and 3rd columns to file forces_energy_Z.dat
awk < forces_energy.dat '{print $1, $3}' >> forces_energy_Z.dat

# Search recursively for pattern in directory
grep -r pattern directory

# Find files and directories by name
locate name

# Find files in /home/john that start with "prefix".
find /home/john -name 'prefix*'

# Find files larger than 100MB in /home
find /home -size +100M

SSH LOGINS

# Connect to host as your local username.
ssh host

# Connect to host as user
ssh user@host

# Connect to host using port
ssh -p port user@host

FILE TRANSFERS

# Secure copy file.txt to the /tmp folder on server
scp file.txt server:/tmp

# Copy *.html files from server to the local /tmp folder.
scp server:/var/www/*.html /tmp

# Copy all files and directories recursively from server to the current system's /tmp folder.
scp -r server:/var/www /tmp

# Synchronize /home to /backups/home
rsync -a /home /backups/