Skip to main content

Chapter 3.3 - Navigation and Basic Commands

Learning Objectives​

By the end of this chapter, you will be able to:

  • Navigate the file system efficiently
  • Use basic commands for files
  • Understand wildcards and patterns
  • Manipulate files and directories

# Where am I?
pwd

# Change directory
cd /home/user1
cd ~ # Home directory
cd # Home directory (shortcut)
cd .. # Parent directory
cd - # Previous directory
cd ../.. # Two levels up

# List
ls # Simple list
ls -l # Details (long format)
ls -la # All (including .files)
ls -lh # Human-readable sizes
ls -lt # Sorted by date
ls -ltr # Sorted by date (reverse)
ls -R # Recursive

Useful ls Options​

# By size
ls -lhS

# By extension
ls -lX

# Colors
ls --color=auto

# Ignore certain patterns
ls --ignore='*.txt'

File Manipulation​

Create Files​

# Create an empty file
touch file.txt

# Create multiple files
touch file1.txt file2.txt file3.txt

# Create with specific timestamp
touch -t 202401011200 file.txt

Display Content​

# Display all
cat file.txt

# Display with line numbers
cat -n file.txt

# Display page by page
less file.txt
more file.txt

# First lines
head file.txt
head -n 20 file.txt

# Last lines
tail file.txt
tail -n 20 file.txt
tail -f file.txt # Follow in real-time

Copy and Move​

# Copy
cp source.txt destination.txt
cp file1.txt file2.txt /home/user1/
cp -r directory/ destination/ # Recursive

# Move/Rename
mv old.txt new.txt
mv file.txt /home/user1/
mv *.txt /home/user1/ # All .txt

Delete​

# Delete a file
rm file.txt

# Recursive delete
rm -r directory/
rm -rf directory/ # Force (caution!)

# Delete with confirmation
rm -i file.txt

# Delete multiple
rm file1.txt file2.txt file3.txt
rm *.txt # All .txt

Directories​

Create Directories​

# Create a directory
mkdir my_dir

# Create multiple
mkdir dir1 dir2 dir3

# Create with parents
mkdir -p path/to/full/dir

# With permissions
mkdir -m 755 my_dir

Delete Directories​

# Empty directory
rmdir empty_dir

# Directory with content
rm -r dir
rm -rf dir # Force (caution!)

Wildcards and Patterns​

Special Characters​

# * : Any characters
ls *.txt # All .txt
ls file* # Starts with "file"
ls *log* # Contains "log"

# ? : Single character
ls file?.txt # file1.txt, file2.txt, etc.
ls ???.txt # Exactly 3 characters

# [] : Specific characters
ls file[123].txt # file1.txt, file2.txt, file3.txt
ls file[0-9].txt # file0.txt to file9.txt
ls file[a-z].txt # filea.txt to filez.txt

# {} : Expansion
mkdir {dir1,dir2,dir3}
cp file.{txt,bak}

Practical Examples​

# All .log files
ls *.log

# Files starting with "test"
ls test*

# Files with 3 characters
ls ???

# Backup files
ls *.bak *.old *.tmp

find​

# By name
find /home -name "file.txt"
find /home -iname "file.txt" # Case insensitive

# By type
find /home -type f # Files
find /home -type d # Directories

# By size
find /home -size +100M # More than 100MB
find /home -size -1k # Less than 1KB

# By date
find /home -mtime -7 # Modified last week
find /home -mtime +30 # Modified more than 30 days ago

# Execute a command
find /home -name "*.txt" -exec rm {} \;
find /home -name "*.log" -delete

locate​

# Faster than find
locate file.txt

# Update the database
sudo updatedb

# Ignore case
locate -i file

Summary​

In this chapter, you learned:

Navigation: pwd, cd, ls with options
Files: touch, cat, less, head, tail
Manipulation: cp, mv, rm with options
Directories: mkdir, rmdir, rm -r
Wildcards: *, ?, [], for patterns
Search: find, locate to find files


Next Steps​

Chapter 3.4: File Management
Chapter 3.5: File Permissions


Chapter created on: December 2024