UNIX Shell Basics
explainshell.com - Lean fancy shell commands by breaking them down into individual components and providing explanations for each part.
Linux shell for beginners - A comprehensive guide for beginners to learn Linux shell commands and concepts.
Useful Programs and Unix Basics - A collection of useful Unix commands and programs for bioinformatics.
## Basic command
pwd # Show current directory
cd /path/to/directory # Change directory
cd .. # Go up one level
cd - # Go to previous directory
cd ~ # Go to home directory
ls # List files and folders
ls -lhrt # List files with details, human-readable sizes, sorted by time, reverse order
mkdir folder_name # Create a new folder
mkdir -p /path/to/folder # Create nested folders
mv old_name new_name # Rename or move file/folder
rm file_name # Remove file
rm -r folder_name # Remove folder recursively
rm -f file_name # Force remove file without prompt
rm -rf folder_name # Force remove folder recursively without prompt
cp source_file destination_file # Copy file
cp -a /source/folder /destination/folder # Copy folder recursively with attributes
scp user@remote:/path/to/remote/file /local/destination/ # Copy file from remote server
scp /local/file user@remote:/path/to/remote/destination/ # Copy file to remote server
cat file_name # Display file content
zcat file_name.gz # Display compressed file content
less file_name # View file content with pagination
zless file_name.gz # View compressed file content with pagination
head -n 10 file_name # Show first 10 lines of a file
tail -n 10 file_name # Show last 10 lines of a file
wc -l file_name # Count number of lines in a file
history # Show command history
top # Display running processes
htop # Interactive process viewer (may need installation)
chmod 755 file_name # Change file permissions
chmod +x file_name # Make file executable
chown user:group file_name # Change file owner and group
tar xzvf archive.tar.gz # Extract tar.gz archive
tar czvf archive.tar.gz /path/to/folder # Create tar.gz archive
tar xjvf archive.tar.bz2 # Extract tar.bz2 archive
tar cjvf archive.tar.bz2 /path/to/folder # Create tar.bz2 archive
unzip archive.zip # Extract zip archive
zip -r archive.zip /path/to/folder # Create zip archive
ping google.com # Check network connectivity
ifconfig # Show network interfaces (may need installation)
ip addr show # Show network interfaces (modern alternative to ifconfig)
wget http://example.com/file # Download file from the internet
curl -O http://example.com/file # Download file using curl## Intermediate command
grep "pattern" file_name # Search for a pattern in a file
grep -r "pattern" /path/to/directory # Recursively search for a pattern in a directory
sort file_name # Sort lines in a file
sort -u file_name # Sort and remove duplicate lines
uniq file_name # Remove duplicate lines (requires sorted input)
find /path/to/search -name "pattern" # Find files/folders by name
find /path/to/search -type f -size +100M # Find files larger than 100MB
cut -f1,3 file_name # Extract specific columns from a file (tab-delimited)
awk '{print $1, $3}' file_name # Extract specific columns using awk
sed 's/old/new/g' file_name # Replace text in a file
diff file1 file2 # Compare two files line by line
diff -r dir1 dir2 # Compare two directories recursively
ln -s /path/to/target /path/to/symlink # Create a symbolic link
tree /path/to/directory # Display directory structure as a tree (may need installation)
tree -L 2 /path/to/directory # Display directory structure up to level 2
du -h /path/to/directory # Show disk usage of a directory
du -sh /path/to/directory # Show total disk usage of a directory
du -h --max-depth=1 /path/to/directory | sort -h # Show disk usage of subdirectories sorted by size
rsync -av /source/folder/ /destination/folder/ # Sync folders
rsync -auzP /source/folder/ user@remote:/destination/folder/ # Sync to remote server with compression and progress## Advanced command
| # Pipe output of one command to another
awk '{sum += $1} END {print sum}' file_name # Sum values in the first column
sed -n '2,5p' file_name # Print lines 2 to 5 of a file
xargs -n 1 rm < file_list.txt # Remove files listed in a file## Show current shell
echo $SHELL
## Change default shell to bash
chsh -s /bin/bash
chsh -s /bin/zshFile Compression
- Commone tar flags:
-x= extract-f= file-v= verbose-z= gzip compression-j= bzip2 compression-J= xz compression-c= change to directory-C= specify directory-p= preserve permissions--strip-components=N= remove N leading path components--no-same-owner= do not preserve ownership--no-same-permissions= do not preserve permissions--wildcards= enable wildcard matching--exclude='pattern'= exclude files matching pattern--include='pattern'= include only files matching pattern--transform='s/old/new/'= rename files during extraction--skip-old-files= skip files that already exist--overwrite= overwrite existing files--keep-newer-files= keep files that are newer than the archive--no-overwrite-dir= do not overwrite existing directories--no-clobber= do not overwrite existing files--remove-files= remove files after adding them to the archive--no-recursion= do not recurse into directories--no-auto-compress= do not automatically detect compression--no-check-device= do not check device numbers when extracting--no-same-time= do not preserve modification times--no-same-permissions= do not preserve permissions--no-same-owner= do not preserve ownership--no-same-time= do not preserve modification times
## Extract .tar.gz file
tar -xf fuke.tar
tar -xzvf file.tar.gz
tar -xzf file.tgz
## Extract .tar.bz2 file
tar -xjf file.tar.bz2
## Extract .tar.xz file
tar -xJf file.tar.xz
## Extract to specific directory
tar -xzf file.tar.gz -C /path/to/directory
## List contents without extracting
tar -tzf file.tar.gz
tar -tf file.tar## Extract zip files
unzip file.zip
unzip file.zip -d /path/to/directory
## List contents without extracting
unzip -l file.zip
## Extract .gz file
gunzip file.gz
gunzip -dk file.gz # keep original file
## Extract .bz2 file
bunzip2 file.bz2
## Extract .xz file
unxz file.xz
## Extract .7z file
7z x file.7z
## Extract .rar file
unrar x file.rar- Common tar flags for compression
-c= create archive-f= file name-z= gzip compression (.tar.gz)-j= bzip2 compression (.tar.bz2)-J= xz compression (.tar.xz)-v= verbose output
tar -cf archive.tar file1 file2 directory/
## gzip compressed
tar -czf archive.tar.gz file1 file2 directory/
## bzip2 compressed
tar -cjf archive.tar.bz2 file1 file2 directory/
## .tar.xz compressed
tar -cJf archive.tar.xz file1 file2 directory/Shortcut
These shortcuts work in most Unix-like terminals including Terminal.app on macOS, GNOME Terminal, and Windows Terminal with WSL.
Cursor Movement
| Action | Shortcut |
|---|---|
| Move to the beginning of line | Ctrl + A |
| Move to the end of line | Ctrl + E |
| Move one word backward | Opt + ← |
| Move one word forward | Opt + → |
| Move one character backward | ← |
| Move one character forward | → |
| Jump to the start of buffer | Alt + < |
| Jump to the end of buffer | Alt + > |
Text Editing
| Text Editing | Shortcut |
|---|---|
| Delete the current line | Ctrl + U |
| Delete to end of the line | Ctrl + K |
| Clear the screen | Ctrl + L |
| Delete one word backward | Ctrl + W |
| Delete one character forward | Ctrl + D |
| Delete one character forward | Fn + Backspace |
| Delete one character backward | Backspace |
| Delete from cursor to start | Alt + Backspace |
| Delete from cursor to word end | Alt + D |
Process Management
| Process Management | Shortcut |
|---|---|
| Terminate a process | Ctrl + C |
| Suspend a process | Ctrl + Z |
| Exit the shell | Ctrl + D |
| Send process to background | Ctrl + Z then bg |
| Bring background process to fg | fg |
History & Commands
| History & Commands | Shortcut |
|---|---|
| Undo the last change | Ctrl + _ |
| Recall previous command | ↑ |
| Recall next command | ↓ |
| Search command history | Ctrl + R |
| Exit history search | Ctrl + G |
| Show history | history |
| Run the last command | !! |
Run last pw command |
!pw |
| Run command # from history | !<number> |
Auto-Completion & Misc
| Auto-Completion & Misc | Shortcut |
|---|---|
| Auto-complete command | TAB |
| List matching commands | TAB TAB |
| Cycle through matches | TAB TAB TAB... |
| Open last edited command | Ctrl + X Ctrl + E |
| Transpose two characters | Ctrl + T |
| Capitalize word at cursor | Alt + C |
| Convert word to lowercase | Alt + L |
| Convert word to uppercase | Alt + U |
| Swap position with prev word | Ctrl + W Alt + T |
Permissions
Only the user can copy, execute, and change
chmod 700 /path/to/folder700 means:
- Owner: 7 (read = 4, write = 2, execute = 1; 4+2+1 = 7)
- Group: 0 (no permissions)
- Others: 0 (no permissions)
All can read and execute, only the owner can change
chmod 755 /path/to/folder- Owner: 7 (read = 4, write = 2, execute = 1; 4+2+1 = 7)
- Group: 5 (read = 4, execute = 1; 4+1 = 5)
- Others: 5 (read = 4, execute = 1; 4+1 = 5)
Give a user named someuser read, write, and execute permissions on the /path/to/folder directory
setfacl -m u:someuser:rwx /path/to/folderVerify the Permissions
getfacl /path/to/folderRemove all ACL entries for a user
setfacl -x u:someuser /path/to/folderChange owner
sudo chown username:groupname /path/to/folderData transfer
- Transfering data efficiently between local and remote systems:
wget: Download files directly from the internet to your local machine or server.scp: Securely copy files between local and remote systems over SSH.rsync: Synchronize files and directories efficiently, with options for selective copying and deletion.
## Basic command
rsync -av /source/folder/ /destination/folder/
## Sync source to destination
rsync -av --delete /source/folder/ /destination/folder/
## If you need two-way sync, run both directions
rsync -av --delete /folder1/ /folder2/
rsync -av --delete /folder2/ /folder1/
## Network sync
rsync -av --delete /local/folder/ user@remote:/remote/folder/
## Copy only new files (files that don't exist in the destination)
rsync -av --ignore-existing /source/folder/ /destination/folder/
## Copy only if source is newer
rsync -av --update /source/folder/ /destination/folder/--archive: Preserves permissions, timestamps, symbolic links, etc.--verbose: Shows progress--delete: Removes files in destination that don’t exist in source--dry-run: Preview changes without actually syncing--update: Only copy newer file--ignore-existing: Skips files that already exist in destination (regardless of age)--update: Only copies if source file is newer than destination--no-perms: Don’t preserve Unix permissions--no-times: Don’t preserve modification times--inplace: Write directly without temp files
- Sync remote server folders to local using rsync
## Remote server details
remote_user="username"
remote_host="hpcio2" # hpc2021-io1.hku.hk,hpc2021-io2.hku.hk
## Define folders to sync (absolute paths)
## Format: "local_absolute_path:remote_absolute_path"
folders_to_sync=(
# "/mnt/m/Reference:/lustre1/g/path_my/Reference"
# "/mnt/m/WES/DFSP/Annovar:/lustre1/g/path_my/pipeline/somatic_variants_calling/data/DFSP/Annovar"
"/mnt/m/WES/SARC/BAM:/lustre1/g/path_my/pipeline/somatic_variants_calling/data/SARC/BAM"
# Add more folders as needed in the same format
)
## Sync each folder
for folder_pair in "${folders_to_sync[@]}"; do
# Split the pair into local and remote paths
local_path="${folder_pair%%:*}"
remote_path="${folder_pair#*:}"
echo "Syncing ${remote_user}@${remote_host}:${remote_path} to ${local_path}"
# Create local directory structure
mkdir -p "${local_path}"
# Sync the folder contents from remote to local (trailing slash on source copies contents)
rsync -av --update --progress --inplace --no-perms --no-times \
"${remote_user}@${remote_host}:${remote_path}/" \
"${local_path}/"
echo "-----------------------------------"
done- Sync local folders to remote server
## Remote server details
remote_user="username"
remote_host="hpcio1" # hpc2021-io1.hku.hk
## Define folders to sync (absolute paths)
## Format: "local_absolute_path:remote_absolute_path"
folders_to_sync=(
"/mnt/f/Reference:/lustre1/g/path_my/Reference"
# Add more folders as needed in the same format
)
## Sync each folder
for folder_pair in "${folders_to_sync[@]}"; do
# Split the pair into local and remote paths
local_path="${folder_pair%%:*}"
remote_path="${folder_pair#*:}"
echo "Syncing ${local_path} to ${remote_user}@${remote_host}:${remote_path}"
# Create remote directory structure
ssh ${remote_user}@${remote_host} "mkdir -p ${remote_path}"
# Sync the folder contents (trailing slash on source copies contents)
rsync -av --update --progress \
"${local_path}/" \
"${remote_user}@${remote_host}:${remote_path}/"
echo "-----------------------------------"
doneNano Text Editor
- Open a file with:
nano filename.txt - Use the arrow keys to navigate.
- Save the changes with
ctrl + o, - Confirm the filename by pressing
Enter. - Exit nano with
ctrl + x.
Searching Text
- To find words or phrases within a file:
- Initiate search with
ctrl + w. - Type the search term and press
Enter. - Exit search mode with
ctrl + c.
Customizing Nano
Enhance Nano’s functionality:
nano -miA file.txt-m: Enable mouse support.-i: Auto-indent new lines.-A: Enable syntax highlighting.
Vim Text Editor
- Vim text editor has three main modes:
- Normal mode:
Esc, input commands - Insert mode:
i, insert text - Visual mode:
v, select text
- Normal mode:
Working Modes
i: Insert mode
I: Insert at beginning of line o: Insert new line below
O: Insert new line above
a: Append after cursor
A: Append at end of line
Esc: Command mode (normal)
v: Visual mode (select text)
V: Visual line mode
Ctrl + v: Visual block mode
Text Editing
| Editing | Shortcut |
|---|---|
| Delete char | x |
| Delete line | dd |
| Delete word | dw |
| Delete to end of line | d$ |
| Delete to start of line | d0 |
| Copy line | yy |
| Copy word | yw |
| Paste after cursor | p |
| Paste before cursor | P |
| Undo | u |
| Redo | Ctrl + r |
| Change text | c |
| Replace char | r |
| Join line with next | J |
Search & Replace
| Search & Replace | Shortcut |
|---|---|
| Search text | /text |
| Next match | n |
| Previous match | N |
| Search backward | ?text |
| Search current word | * |
File Operations
| Action | Shortcut |
|---|---|
| Save | :w |
| Quit | :q |
| Save and quit | :wq |
| Quit without saving | :q! |
| Save and quit (shortcut) | ZZ |
| Quit without saving (shortcut) | ZQ |
Tmux
Tmux Configuration
Consider adding these aliases to your shell configuration for quick tmux access.
# Terminal aliases for tmux
alias t="tmux"
alias ta="t a -t"
alias tls="t ls"
alias tn="t new -s"
alias tk="t kill-session -t"
alias tks="t kill-server"Change the default prefix in .tmux.conf:
set-option -g prefix C-a
bind-key C-a send-prefixSession Management
| Action | Command/Shortcut |
|---|---|
| List sessions | tmux list-sessions |
| Attach to session | tmux attach-session -t target-session |
| Switch between sessions | Ctrl + A + s |
| Switch to latest session | Ctrl + A + l |
| Detach from session | Ctrl + A + d |
Window Management
| Action | Shortcut |
|---|---|
| List all windows | Ctrl + A + w |
| Rename current window | Ctrl + A + , |
| Switch to next window | Ctrl + A + → |
| Switch to previous window | Ctrl + A + ← |
| Create new window | Ctrl + A + c |
| Kill current window | Ctrl + A + q |
Pane Management
| Action | Shortcut |
|---|---|
| Switch to pane above | Shift + ↑ |
| Switch to pane below | Shift + ↓ |
| Switch to pane left | Shift + ← |
| Switch to pane right | Shift + → |
| Kill current pane | Ctrl + A + x |
Copy Mode
| Action | Shortcut |
|---|---|
| Enter copy mode | Drag mouse to select text |
| Paste copied text | Ctrl + A + ] |
Screen
Add these aliases to your shell configuration for easier screen management.
# Terminal aliases for screen
alias s="screen" # start a screen session
alias ss="s -S" # start a named screen session
alias sr="s -r" # reattach to a screen session
alias sls="s -ls" # list current running screen sessionsBasic Commands
| Action | Command |
|---|---|
| Start screen session | screen |
| Start named session | screen -S session_name |
| Reattach to session | screen -r |
| List running sessions | screen -ls |
Window Management
| Action | Shortcut |
|---|---|
| Create new window | Ctrl + A + C |
| Kill current window | Ctrl + A + K |
| List all windows | Ctrl + A + W |
| Go to window 0-9 | Ctrl + A + 0-9 |
| Go to next window | Ctrl + A + N |
| Toggle between windows | Ctrl + A + Ctrl + A |
| Rename current window | Ctrl + A + A |
Region Management
| Action | Shortcut |
|---|---|
| Split horizontally | Ctrl + A + S |
| Split vertically | Ctrl + A + \| |
| Switch between regions | Ctrl + A + Tab |
| Close all regions but current | Ctrl + A + Q |
| Close current region | Ctrl + A + X |
Session & Copy Mode
| Action | Shortcut |
|---|---|
| Detach from session | Ctrl + A + D |
| Start copy mode | Ctrl + A + [ |
| Paste copied text | Ctrl + A + ] |
| Show help | Ctrl + A + ? |
| Quit screen | Ctrl + A + Ctrl + \ |
- VSCode: Official Keyboard Shortcuts Reference
- Vim: Vim Cheat Sheet
- Tmux: Tmux Cheat Sheet
- Terminal: Bash Reference Manual
WSL
System info
## Install the neofetch package
sudo apt install neofetch -y
## Show system info
neofetch --memory_unit gibWSL Change sudo password
In the windows terminal, run the following command to change the sudo password:
wsl -u root
## Input the new password
passwd <username>WSL configuration
- WSL global config:
C:\Users\<UserName>\.wslconfig - WSL distro config:
\\wsl.localhost\Ubuntu-24.04\etc\wsl.conf
Conda
Installation
# Miniforge linux
wget "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-x86_64.sh"
# Miniforge Mac
wget "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-arm64.sh"
# Install it
bash Miniforge3-$(uname)-$(uname -m).sh
# Add conda to PATH if found
if [ -f "$HOME/miniconda3/etc/profile.d/conda.sh" ]; then
. $HOME/miniconda3/etc/profile.d/conda.sh
fiConfiguration
# Init
conda init --all
# Use mamba for faster solving
conda update -n base conda
conda install -n base conda-libmamba-solver
# Config
conda config --set solver libmamba
conda config --set always_yes true
conda config --set auto_activate_base false
# Setup channels
conda config --add channels bioconda
conda config --add channels conda-forge
#conda config --set channel_priority strict
# Install software from a specific channel
conda install -c conda-forge numpyEnvironment
# Create R environment
conda create -n renv r-4.3 r-languageserver r-tidyverse r-irkernel r-httpgd
# Install bioconductor packages, all package names are lower case
conda install -n renv bioconductor-deseq2 bioconductor-edger
# Install repository packages
conda install -n renv r-qs r-fs r-tidyverse
# Remove a specific environment
conda remove --name renv --all
# Remove a software in a environment
conda remove --name renv package_name
# Export and save the conda env file with all software information
conda env export -n renv > renv.yml
# Export and save the conda env file with all software information
# without the prefix and bundle information
conda env export -n renv --no-builds > renv.yml
conda env export -n renv --no-builds --file renv.yml
# Create a new environment with a environment file
conda env create --file renv.yml
# Rename an environment
conda create --name new_env_name --clone old_env_name
conda activate new_env_name
conda remove --name old_env_name --allPixi
## Auto-activate pixi when entering project directory
eval "$(pixi completion --shell bash)" # or zsh