Skip to main content

Chapter 3.4 - File Management

Learning Objectives​

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

  • Edit files with vi/vim and nano
  • Compare files
  • Archive and compress
  • Manage symbolic and hard links

File Editing​

nano (Simple)​

Simple and user-friendly editor:

# Open a file
nano fichier.txt

# nano shortcuts:
# Ctrl+O : Save
# Ctrl+X : Exit
# Ctrl+W : Search
# Ctrl+K : Cut line
# Ctrl+U : Paste

Advantages:

  • Easy to use
  • Shortcuts displayed
  • Ideal for beginners

vim (Advanced)​

Powerful and configurable editor:

# Open a file
vim fichier.txt

# vim modes:
# Normal (default): Navigation
# Insert (i): Editing
# Command (:): Commands

# Basic commands:
# i : Insert mode
# Esc : Return to normal mode
# :w : Save
# :q : Quit
# :wq : Save and quit
# :q! : Quit without saving
# dd : Delete line
# yy : Copy line
# p : Paste

Advantages:

  • Very powerful
  • Available everywhere
  • Efficient once mastered

File Comparison​

diff​

Compare two files:

# Simple comparison
diff file1.txt file2.txt

# Unified (more readable)
diff -u file1.txt file2.txt

# Ignore spaces
diff -w file1.txt file2.txt

# Recursive on directories
diff -r dir1/ dir2/

cmp​

Compare byte by byte:

cmp file1.txt file2.txt

Archiving and Compression​

tar​

Archive files:

# Create an archive
tar -cf archive.tar fichiers/
tar -czf archive.tar.gz fichiers/ # With gzip compression
tar -cjf archive.tar.bz2 fichiers/ # With bzip2 compression

# Extract
tar -xf archive.tar
tar -xzf archive.tar.gz
tar -xjf archive.tar.bz2

# List contents
tar -tf archive.tar

# Options:
# -c : Create
# -x : Extract
# -f : File
# -z : gzip
# -j : bzip2
# -v : Verbose

zip/unzip​

ZIP format:

# Create
zip archive.zip fichier1.txt fichier2.txt
zip -r archive.zip repertoire/

# Extract
unzip archive.zip
unzip archive.zip -d destination/

# List
unzip -l archive.zip

gzip/gunzip​

Simple compression:

# Compress
gzip fichier.txt # Creates fichier.txt.gz

# Decompress
gunzip fichier.txt.gz
gzip -d fichier.txt.gz

Pointers to other files:

# Create a symbolic link
ln -s cible.txt lien.txt
ln -s /chemin/absolu/cible lien

# See where the link points
ls -l lien.txt
readlink lien.txt

# Remove
rm lien.txt # Only removes the link

Characteristics:

  • Can point to files or directories
  • Can point to non-existent files
  • Can cross file systems
  • Identified by l in ls -l

Direct reference to the same inode:

# Create a hard link
ln cible.txt lien.txt

# Verify (same inode)
ls -li cible.txt lien.txt

Characteristics:

  • Same inode as the original
  • Cannot point to directories
  • Cannot cross file systems
  • Removing the original does not remove the link

Advanced Operations​

split​

Split a file:

# Split into files of 100 lines
split -l 100 gros_fichier.txt partie_

# Split by size
split -b 10M gros_fichier.txt partie_

join​

Join files:

join file1.txt file2.txt

sort​

Sort files:

# Sort
sort fichier.txt

# Sort numerically
sort -n nombres.txt

# Sort in reverse
sort -r fichier.txt

# Sort by column
sort -k 2 fichier.txt

uniq​

Remove duplicates:

# Remove duplicate lines
uniq fichier.txt

# Count occurrences
uniq -c fichier.txt

# Show only unique lines
uniq -u fichier.txt

Summary​

In this chapter, you learned:

Editing: nano (simple), vim (advanced)
Comparison: diff, cmp for file comparison
Archiving: tar, zip for creating archives
Compression: gzip, bzip2 for compression
Links: Symbolic (ln -s) and hard (ln)
Operations: split, join, sort, uniq


Next Steps​

Chapter 3.5: File Permissions
Lab 3.1: Navigation and Creation


Chapter created on: December 2024