Creating Files with touch
Syntax
touch [options] filename
Key Options
| Option |
Description |
-m |
Update modification timestamp |
-d |
Set specific datetime |
Examples
# Create file in current directory
$ touch example.txt
# Create file with absolute path
$ touch /tmp/sample.txt
# Create multiple files
$ touch file1.txt file2.txt file3.txt
$ touch {1..5}.log
# Create files in different locations
$ touch /var/log/app.log ~/notes.txt /tmp/debug.tmp
Viewing File Contents with cat
Syntax
cat [options] filename
Key Options
| Option |
Description |
-n |
Number all lines |
-b |
Number non-empty lines |
-E |
Show line endings |
Example
# Display configuration file
$ cat /etc/network/interfaces
Monitoring File Ends with tail
Syntax
tail [options] filename
Key Options
| Option |
Description |
-f |
Follow real-time updates |
-n |
Show last N lines |
-F |
Track rotating files |
Examples
# Show last 15 lines of log
$ tail -15 /var/log/syslog
# Monitor authentication log
$ tail -f /var/log/auth.log
Viewing File Beginnings with head
Syntax
head [options] filename
Key Option
| Option |
Description |
-n |
Show first N lines |
Examples
# View first 10 lines
$ head -10 /etc/services
# Extract lines 20-25
$ head -25 file.log | tail -6
Pattern Matching with grep
Syntax
grep [options] pattern filename
Key Options
| Option |
Description |
-i |
Case-inesnsitive search |
-v |
Invert match |
-r |
Recursive search |
-n |
Show line numbers |
-w |
Whole-word match |
-A |
Display lines after match |
-B |
Display lines before match |
-C |
Display context lines |
Example
# Find IP addresses in config
$ ip addr | grep 'inet\b'
Countign Elements with wc
Syntax
wc [options] filename
Key Options
| Option |
Description |
-l |
Count lines |
-w |
Count words |
-c |
Count bytes |
-L |
Find longest line |
Example
# Get line count of password file
$ wc -l /etc/passwd
Output Generation with echo
Syntax
echo [options] string
Examples
# Print text
$ echo "System Configuration"
# Generate sequences
$ echo {A..F}
$ echo Item{1..100}
# Create multiple files
$ touch report_{Jan,Feb,Mar}.txt
# Create directory structure
$ mkdir -p project/{src,bin,docs}
Paging Through Files with less
Syntax
less [options] filename
Key Options
| Option |
Description |
-N |
Display line numbers |
-i |
Case-insensitive search |
Navigation
| Key |
Action |
| Space |
Next page |
| b |
Previous page |
| /pattern |
Search forward |
| g |
Go to start |
| G |
Go to end |
| q |
Quit |
Basic Paging with more
Syntax
more [options] filename
Key Options
| Option |
Description |
-num |
Set screen size |
+linenum |
Start at specific line |
Sorting Data with sort
Syntax
sort [options] filename
Key Options
| Option |
Description |
-n |
Numerical sort |
-r |
Reverse order |
-k |
Sort by column |
-t |
Specify field separator |
Examples
# Sort numbers
$ sort -n data.txt
# Sort IP addresses by third octet
$ sort -t. -k3n ip_list.txt
Filtering Duplicates with uniq
Syntax
uniq [options] filename
Key Options
| Option |
Description |
-c |
Count occurrences |
-d |
Show duplicates only |
Example
# Count log entries
$ cut -d' ' -f1 access.log | sort | uniq -c | sort -nr
Building Commands with xargs
Syntax
command | xargs [options] command
Key Options
| Option |
Description |
-n |
Items per command |
-d |
Custom delimiter |
-I |
Replace placeholder |
-t |
Print commands before execution |
Examples
# Process files in batches
$ find . -name "*.tmp" | xargs -n5 rm
# Copy found files
$ find /logs -name "*.log" | xargs -I {} cp {} /backup
Character Translation with tr
Syntax
tr [options] set1 set2
Key Options
| Option |
Description |
-d |
Delete characters |
-s |
Copmress repeated characters |
Example
# Convert to uppercase
$ echo "hello" | tr 'a-z' 'A-Z'
Inspecting Files with stat
Syntax
stat [options] filename
Key Options
| Option |
Description |
-f |
Filesystem information |
-L |
Follow symbolic links |
Example
# View file metadata
$ stat document.pdf
# Check filesystem details
$ stat -f /home