The Basics

A compact reference for navigating files, finding text, and connecting commands. Quote paths that contain spaces.

2.1 Navigation

Linux uses a hierarchical tree structure starting at the root (/).

# Print working directory (where am I?)
pwd

# List files
ls            # basic listing
ls -l         # long format (permissions, size, date)
ls -la        # include hidden files (dotfiles like .bashrc)
ls -lh        # human-readable file sizes (KB, MB, GB)

# Change directory
cd /path/to/dir     # absolute path
cd relative/path    # relative path
cd ~                # home directory
cd ..               # parent directory
cd -                # previous directory (toggle)
                    # Note: extremely useful when jumping between
                    # a data directory and a script directory

2.2 File Operations

Creating, Copying, Moving, Renaming

# Create
touch file.txt          # create empty file (or update timestamp)
mkdir mydir             # create directory
mkdir -p a/b/c          # create nested directories (parent/child/grandchild)

# Copy
cp file.txt backup.txt          # copy file
cp -r mydir/ mydir_backup/      # copy directory recursively

# Move / Rename
mv file.txt newname.txt         # rename
mv file.txt /other/path/        # move

Deleting

rm bypasses the desktop Trash. Check paths before removing files; restoration may require a backup.

rm file.txt             # delete file
rm -r mydir/            # delete directory recursively
rm -ri mydir/           # interactive — asks before every file

Wildcards (Globbing)

  • * matches anything (e.g., *.csv matches all CSV files)
  • ? matches exactly one character (e.g., file?.txt matches file1.txt but not file10.txt)

2.3 Viewing & Searching

Viewing Files

# View entire file
cat file.txt            # dump whole file to terminal (bad for large files)
less file.txt           # paginated view (q to quit, / to search)

# View parts
head file.txt           # first 10 lines
head -n 20 file.txt     # first 20 lines
tail file.txt           # last 10 lines
tail -f logfile.log     # follow a log file in real-time

grep: The Search Tool

grep filters input line-by-line. Essential for bioinformatics (finding specific sequences, counting FASTA records, etc.).

# Basic search
grep "pattern" file.txt

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

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

# Recursive search (all files in current directory)
grep -r "TODO" .

# Invert match (lines that do NOT match)
grep -v "error" log.txt

# Count matching lines
grep -c '^>' sequences.fasta

2.4 Permissions

Every file has a mode string (e.g., -rwxr-xr--) defining who can do what.

The Bits

Symbol Meaning Octal Value
r Read 4
w Write 2
x Execute 1

Changing Permissions (chmod)

# Symbolic mode (easier to read)
chmod u+x script.sh      # add execute for the owner
chmod u+w file.txt        # add write for owner (u=user)
chmod go-r private.key    # remove read for group and others

# Numeric mode (faster to type)
chmod 755 script.sh       # rwx (7) for owner, r-x (5) for group/others
chmod 644 file.txt        # rw- (6) for owner, r-- (4) for group/others
chmod 600 private.key     # rw- (6) for owner, --- (0) for group/others

2.5 Pipes & Redirection

Redirection

Operator Behavior
> Overwrite output to a file
>> Append output to the end of a file
2> Redirect stderr (errors) only
echo "Analysis Complete" > status.txt    # creates/overwrites file
echo "Error found" >> log.txt            # appends to file

Pipes (|)

Passes standard output from the left command to standard input of the right command. Standard error remains separate unless redirected.

# Count number of Python files
find . -type f -name "*.py" | wc -l

# Check for errors in the last 50 lines of a log
tail -n 50 app.log | grep "Error"

# Unique sorted values (classic combo)
sort -u data.txt
# This is the Unix philosophy in action — small tools chaining
# together to solve complex problems without a custom script.

The file-count example assumes filenames contain no newlines. Shell globs such as * normally omit leading-dot names. For directories, r permits listing names, w permits changing entries, and x permits traversal; access can also depend on parent permissions and ACLs. rm bypasses the desktop Trash, but does not securely erase file contents.

References