Skip to content

Bash Command Reference

Quick reference guide for commonly used Linux/Bash commands on the cluster.


Change Directory

# Go to home directory
cd
cd ~

# Go to specific directory
cd /path/to/directory

# Go up one level
cd ..

# Go up two levels
cd ../..

# Go to previous directory
cd -

# Go to subdirectory
cd subdirectory

List Files

# List files
ls

# List with details (permissions, size, date)
ls -l

# List including hidden files
ls -a

# List all with details
ls -la

# List with human-readable sizes
ls -lh

# List sorted by modification time (newest first)
ls -lt

# List sorted by size
ls -lS

# List recursively
ls -R

Current Location

# Print working directory (where am I?)
pwd

# Show directory structure
tree
tree -L 2  # Limit depth to 2 levels

File and Directory Management

Create

# Create directory
mkdir my_directory

# Create nested directories
mkdir -p project/data/raw

# Create empty file
touch file.txt

# Create file with content
echo "Hello World" > file.txt

Copy

# Copy file
cp file.txt backup.txt

# Copy directory
cp -r directory/ backup_directory/

# Copy preserving permissions and timestamps
cp -p file.txt backup.txt

# Copy with progress (verbose)
cp -v file.txt backup.txt

# Copy multiple files to directory
cp file1.txt file2.txt destination/

Move/Rename

# Rename file
mv old_name.txt new_name.txt

# Move file to directory
mv file.txt directory/

# Move multiple files
mv file1.txt file2.txt destination/

# Move directory
mv old_dir/ new_dir/

Delete

# Delete file
rm file.txt

# Delete multiple files
rm file1.txt file2.txt

# Delete directory
rm -r directory/

# Delete with confirmation
rm -i file.txt

# Force delete (be careful!)
rm -f file.txt

# Delete empty directory
rmdir empty_directory/

Viewing Files

Display File Contents

# Display entire file
cat file.txt

# Display with line numbers
cat -n file.txt

# Display multiple files
cat file1.txt file2.txt

# Display first 10 lines
head file.txt

# Display first 20 lines
head -n 20 file.txt

# Display last 10 lines
tail file.txt

# Display last 50 lines
tail -n 50 file.txt

# Follow file as it grows (useful for logs)
tail -f output.log

Page Through Files

# View file page by page
less file.txt
# Inside less:
#   Space: next page
#   b: previous page
#   /pattern: search forward
#   q: quit

# Alternative pager
more file.txt

File Information

# File type
file data.dat

# File size and details
ls -lh file.txt

# Disk usage of file/directory
du -h file.txt
du -sh directory/

# Count lines, words, characters
wc file.txt
wc -l file.txt  # Just lines

Searching

Find Files

# Find by name
find . -name "file.txt"

# Find by name (case-insensitive)
find . -iname "FILE.txt"

# Find by extension
find . -name "*.txt"

# Find modified in last 7 days
find . -mtime -7

# Find larger than 100MB
find . -size +100M

# Find and execute command
find . -name "*.tmp" -delete

Search File Contents

# Search for pattern in file
grep "pattern" file.txt

# Case-insensitive search
grep -i "pattern" file.txt

# Search recursively in directory
grep -r "pattern" directory/

# Show line numbers
grep -n "pattern" file.txt

# Show files with matches (not the lines)
grep -l "pattern" *.txt

# Invert match (lines NOT containing pattern)
grep -v "pattern" file.txt

# Count matches
grep -c "pattern" file.txt

File Compression

Tar Archives

# Create tar.gz archive
tar -czf archive.tar.gz directory/

# Create tar archive (no compression)
tar -cf archive.tar directory/

# Extract tar.gz archive
tar -xzf archive.tar.gz

# Extract to specific directory
tar -xzf archive.tar.gz -C /destination/

# List contents without extracting
tar -tzf archive.tar.gz

# Extract single file from archive
tar -xzf archive.tar.gz path/to/file

Gzip

# Compress file
gzip file.txt  # Creates file.txt.gz

# Decompress
gunzip file.txt.gz

# Keep original file
gzip -k file.txt

# Compress with verbose output
gzip -v file.txt

Zip

# Create zip archive
zip archive.zip file1.txt file2.txt

# Create zip of directory
zip -r archive.zip directory/

# Extract zip
unzip archive.zip

# List contents
unzip -l archive.zip

# Extract to specific directory
unzip archive.zip -d /destination/

File Transfer

Within Cluster

# Copy to remote directory on cluster
cp file.txt /remote/path/

# Use rsync for large transfers
rsync -avh --progress source/ destination/

# Sync only newer files
rsync -avh --update source/ destination/

To/From Local Computer

From your local computer terminal:

# Upload file to cluster
scp file.txt username@xlence.disfeb.unimi.it:/path/on/cluster/

# Upload directory
scp -r directory/ username@xlence.disfeb.unimi.it:/path/on/cluster/

# Download file from cluster
scp username@xlence.disfeb.unimi.it:/path/on/cluster/file.txt ./

# Download directory
scp -r username@xlence.disfeb.unimi.it:/path/on/cluster/directory/ ./

# Rsync to cluster (resume-capable)
rsync -avz --progress file.txt username@xlence.disfeb.unimi.it:/path/

# Rsync from cluster
rsync -avz --progress username@xlence.disfeb.unimi.it:/path/file.txt ./

Process Management

View Processes

# Show your processes
ps

# Show all processes
ps aux

# Show your processes with details
ps -u $USER

# Interactive process viewer
top

# Better interactive viewer
htop

# Show processes in tree
pstree

Manage Processes

# Run command in background
command &

# List background jobs
jobs

# Bring job to foreground
fg
fg %1  # Specific job number

# Send job to background
bg

# Kill process by PID
kill PID

# Force kill
kill -9 PID

# Kill by name
pkill process_name

# Kill all processes matching name
killall process_name

Disk Usage

Check Space

# Show disk usage
df -h

# Show directory sizes
du -h
du -sh *  # Summary of each item

# Show largest directories
du -h --max-depth=1 | sort -hr

# Find large files
find . -type f -size +100M -exec ls -lh {} \;

Quota (if enabled)

# Check your quota
quota -s

Text Processing

Basic Editing

# Edit with nano (beginner-friendly)
nano file.txt
# Ctrl+O: Save
# Ctrl+X: Exit

# Edit with vim
vim file.txt
# i: Insert mode
# Esc: Command mode
# :wq: Save and quit
# :q!: Quit without saving

# Replace text in file
sed 's/old/new/g' file.txt > new_file.txt

# Replace in-place
sed -i 's/old/new/g' file.txt

Cut and Paste

# Extract columns (tab-delimited)
cut -f 1,3 file.txt

# Extract columns (comma-delimited)
cut -d ',' -f 1,3 file.csv

# Extract characters
cut -c 1-10 file.txt

# Paste files side-by-side
paste file1.txt file2.txt

Sort and Unique

# Sort lines
sort file.txt

# Sort numerically
sort -n numbers.txt

# Sort by column
sort -k 2 file.txt

# Remove duplicates
sort file.txt | uniq

# Count duplicates
sort file.txt | uniq -c

# Show only duplicates
sort file.txt | uniq -d

Permissions

View Permissions

# Show file permissions
ls -l file.txt
# Output: -rw-r--r-- 1 user group size date file.txt
#         |  |  |
#         |  |  └─ Others: read
#         |  └──── Group: read
#         └─────── Owner: read, write

Change Permissions

# Make executable
chmod +x script.sh

# Remove execute permission
chmod -x script.sh

# Set specific permissions (rwxr-xr-x)
chmod 755 script.sh

# Set read/write for owner only
chmod 600 private_file.txt

# Recursively change directory permissions
chmod -R 755 directory/

Change Owner (rarely needed)

# Change owner (usually requires admin rights)
chown user:group file.txt

# Recursively
chown -R user:group directory/

Environment and Variables

View Environment

# Show all environment variables
env

# Show specific variable
echo $HOME
echo $PATH
echo $USER

# Show shell
echo $SHELL

Set Variables

# Set variable (current session only)
export MY_VAR="value"

# Use variable
echo $MY_VAR

# Add to PATH
export PATH=$PATH:/new/path

# Make permanent (add to ~/.bashrc)
echo 'export MY_VAR="value"' >> ~/.bashrc
source ~/.bashrc

Module Environment

# Show loaded modules
module list

# Show module environment variables
module show software/version

Command History

View History

# Show command history
history

# Show last 20 commands
history 20

# Search history
history | grep pattern

# Reverse search (interactive)
Ctrl+R  # Then type to search

Reuse Commands

# Run previous command
!!

# Run command N from history
!123

# Run last command starting with "git"
!git

# Run last command with substitution
^old^new

Clear History

# Clear current session history
history -c

# Prevent command from being saved (prefix with space)
 command_not_saved

Aliases and Shortcuts

Useful Aliases

Add to ~/.bashrc:

# Navigation
alias ..='cd ..'
alias ...='cd ../..'
alias ~='cd ~'

# Listing
alias ll='ls -lh'
alias la='ls -lah'
alias lt='ls -lth'

# Safety nets
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Disk usage
alias du='du -h'
alias df='df -h'

# Grep with color
alias grep='grep --color=auto'

# Quick file view
alias h='head'
alias t='tail'

After adding, reload:

source ~/.bashrc

Terminal Sessions (tmux)

Why Use tmux?

  • Keep sessions running after disconnect
  • Multiple windows in one SSH connection
  • Recover work after network interruption

Basic tmux Commands

# Start new session
tmux

# Start named session
tmux new -s mysession

# Detach from session (Ctrl+b, then d)
Ctrl+b d

# List sessions
tmux ls

# Attach to session
tmux attach
tmux attach -t mysession

# Kill session
tmux kill-session -t mysession

Inside tmux

# All commands start with: Ctrl+b

# Create new window
Ctrl+b c

# Switch to next window
Ctrl+b n

# Switch to previous window
Ctrl+b p

# Split pane horizontally
Ctrl+b "

# Split pane vertically
Ctrl+b %

# Navigate panes
Ctrl+b arrow_keys

# Detach
Ctrl+b d

Useful One-Liners

File Operations

# Count files in directory
ls -1 | wc -l

# Find and delete all .tmp files
find . -name "*.tmp" -delete

# Copy only new/modified files
rsync -avhu --progress source/ destination/

# Create backup with timestamp
cp file.txt file_$(date +%Y%m%d_%H%M%S).txt

# Compare two files
diff file1.txt file2.txt

# Show file differences side-by-side
diff -y file1.txt file2.txt

Data Processing

# Count unique lines
sort file.txt | uniq | wc -l

# Find top 10 largest files
find . -type f -exec du -h {} \; | sort -hr | head -10

# Extract specific column from CSV
cut -d ',' -f 2 data.csv

# Sum numbers in a column
awk '{sum+=$1} END {print sum}' numbers.txt

# Replace spaces with underscores in filenames
for f in *\ *; do mv "$f" "${f// /_}"; done

System Information

# Show current load
uptime

# Show memory usage
free -h

# Show running processes by memory usage
ps aux --sort=-%mem | head -10

# Show running processes by CPU usage
ps aux --sort=-%cpu | head -10

# Check if file exists
[ -f file.txt ] && echo "File exists" || echo "File not found"

Wildcards and Patterns

Common Wildcards

# * matches any characters
ls *.txt          # All .txt files
ls file*.txt      # file1.txt, file2.txt, fileABC.txt

# ? matches single character
ls file?.txt      # file1.txt, fileA.txt (not file10.txt)

# [] matches character range
ls file[1-3].txt  # file1.txt, file2.txt, file3.txt
ls file[abc].txt  # filea.txt, fileb.txt, filec.txt

# {} for brace expansion
cp file.txt{,.backup}  # Copies file.txt to file.txt.backup
mkdir -p project/{data,results,scripts}

Redirection and Pipes

Output Redirection

# Write output to file (overwrite)
command > output.txt

# Append to file
command >> output.txt

# Redirect errors
command 2> errors.txt

# Redirect both output and errors
command &> all_output.txt

# Discard output
command > /dev/null

# Discard errors
command 2> /dev/null

Pipes

# Send output to another command
ls -l | grep ".txt"

# Chain multiple commands
cat file.txt | grep "pattern" | sort | uniq

# Count matches
grep "pattern" file.txt | wc -l

# Page through long output
ls -la | less

# Save and display simultaneously
command | tee output.txt

Tips and Tricks

1. Tab Completion

Press Tab to auto-complete: - File and directory names - Commands - Variables

Press Tab twice to see all possibilities.

2. Command Line Shortcuts

Ctrl+A    # Move to start of line
Ctrl+E    # Move to end of line
Ctrl+U    # Delete from cursor to start
Ctrl+K    # Delete from cursor to end
Ctrl+W    # Delete previous word
Ctrl+L    # Clear screen (like 'clear')
Ctrl+C    # Cancel current command
Ctrl+D    # Logout (or EOF)
Ctrl+Z    # Suspend current process

3. Quick Directory Navigation

# Save current directory
pushd /some/path

# Return to saved directory
popd

# Show directory stack
dirs

4. Check Command Type

# Show command location
which python3

# Show all locations
which -a python

# Show command type
type ls
type cd

5. Command Timing

# Time a command
time ./script.sh

# More detailed timing
time -p command

Getting Help

Man Pages

# View manual page
man command

# Search manual pages
man -k keyword

# Short description
whatis command

Command Help

# Built-in help
help cd

# Command options
command --help
command -h

# Info pages
info command

Common Mistakes to Avoid

1. Spaces in Commands

# Wrong
var = value

# Correct
var=value

2. Quoting Filenames with Spaces

# Wrong
cd My Documents

# Correct
cd "My Documents"
cd My\ Documents

3. Deleting Important Files

# Dangerous
rm -rf *

# Safer (with confirmation)
rm -ri directory/

# Always double-check before:
ls directory/  # Check what you're about to delete
rm -r directory/

4. Wrong Directory for Output

# Always verify location first
pwd
ls -la

# Then run command
command > output.txt

Additional Resources

Online resources: - Bash Guide for Beginners - Explain Shell - Paste commands for explanations


Support

For help with Linux/Bash commands:


Last Updated: October 2025