Unix-Cheat-Sheet

Author

Tahsin Khan

Published

September 19, 2025

This document gives a brief summary of useful Unix commands adapted from Andries van Tonder’s training materials1. In addition, an example of each command which will be frequently used in this training module is described. Participants are adviced to play with these commands at home to get familiarized with unix command lines.

Note

Anything wihtin {} indicates a user-proved input.

Documentation and Help

command description example
man {command} manual page for the program man cp
{command} --help many programs use the --help flag to print documentation fastqc --help
whatis {command} short description of the program whatis cp

Listing files

command description example
ls list files in the current directory ls
ls {path} list files in the specified path ls genomics/kraken2db
ls -l {path} list files in long format (more information) ls -l genomics/kraken2db
ls -a {path} list all files (including hidden files) ls -a genomics/kraken2db

Change Directories

command description example
cd {path} change to the specified directory cd wgs-training-main
cd or cd ~ change to the home directory cd ~
cd .. move back one directory cd ..
pwd print working directory. Shows the full path of where you are at the moment (useful if you are lost) pwd

Make or Remove Directories

command description example
mkdir {dirname} create a directory with specified name mkdir test
rmdir {dirname} remove a directory (only works if the directory is empty) rmdir test
rm -r {dirname} remove the directory and all it’s contents (use with care) rm -r test

Copy, Move and Remove Files

command description example
cp {source/path/file1} {target/path/} copy “file1” to another directory keeping its name cp /Users/tahsinkhan/genomics/wgs-training-main/README.md /Users/tahsinkhan/genomics/run_output/
cp {source/path/file1} {target/path/file2} copy “file1” to another directory naming it “file2” cp /Users/tahsinkhan/genomics/wgs-training-main/README.md /Users/tahsinkhan/genomics/run_output/cp_README.md
cp {file1} {file2} make a copy of “file1” in the same directory with a new name “file2” cp wgs-training-main/README.md wgs-training-main/new_README.md
mv {source/path/file1} {target/path/} move “file1” to another directory keeping its name mv /Users/tahsinkhan/genomics/wgs-training-main/new_README.md /Users/tahsinkhan/genomics/run_output/new_README.md
mv {source/path/file1} {target/path/file2} move “file1” to another directory renaming it as “file2” mv /Users/tahsinkhan/genomics/wgs-training-main/new_README.md /Users/tahsinkhan/genomics/run_output/mv_README.md
mv {file1} {file2} is equivalent to renaming a file mv /Users/tahsinkhan/genomics/run_output/mv_README.md /Users/tahsinkhan/genomics/run_output/renamed_README.md
rm {filename} remove a file rm /Users/tahsinkhan/genomics/run_output/rename_README.md

View Text Files

command description example
less {file} view and scroll through a text file less wgs-training-main/README.md
head {file} print the first 10 lines of a file head wgs-training-main/README.md
head -n {N} {file} print the first N lines of a file head -n 10 wgs-training-main/README.md
tail {file} print the last 10 lines of a file tail wgs-training-main/README.md
tail -n {N} {file} print the last N lines of a file tail -n 10 wgs-training-main/README.md
head -n {N} {file} | tail -n 1 print the Nth line of a file head -n 5 wgs-training-main/README.md | tail -n 1 wgs-training-main/README.md
cat {file} print the whole content of the file cat wgs-training-main/README.md
cat {file1} {file2} {...} {fileN} concatenate files and print the result
zcat {file} and zless {file} like cat and less but for compressed files (.zip or .gz)

Find Patterns

Finding (and replacing) patterns in text is a very powerful feature of several command line programs. The patterns are specified using regular expressions (shortened as regex), which are not covered in this document. See this Regular Expressions Cheat Sheet for a comprehensive overview.

command description example
grep {regex} {file} print the lines of the file that have a match with the regular expression pattern grep docker wgs-training-main/README.md

Wildcards

command description example
* match any number of characters
? match any character only once
Examples
ls sample* list all files that start with the word “sample” ls kraken2db/database*
ls *.txt list all the files with .txt extension ls kraken2db/*.txt
cp * {another/directory} copy all the files in the current directory to a different directory cp kraken2db/* test/

Redirect Output

command description example
{command} > {file} redirect output to a file (overwrites if the file exists) cat test/inspect.txt > new_inspect.txt
{command} >> {file} append output to a file (creates a new file if it does not already exist) cat test/unmapped_accessions.txt >> new_inspect.txt

Combining Commands with | Pipes

command description example
<command1> | <command2> the output of “command1” is passed as input to “command2” given below
ls | wc -l count the number of files in a directory ls new_inspect.txt | wc -l
cat {file1} {file2} | less concatenate files and view them with less cat test/inspect.txt test/unmapped_accessions.txt | less
cat {file} | grep "{pattern}" | wc -l count how many lines in the file have a match with “pattern” cat test/inspect.txt | grep "Pseudomonas" | wc -l

In addition you can visit linux command cheat sheet that can be useful for working within Docker containers.

References

1.
Asare, P. & Tonder, A. van. Introduction to bacterial genomics. (2023).
Back to top