Linucate
~ Linucate_

103.3 Perform basic file management

All Levels

Introduction

This lesson is about everyday file work.

The main question is:

How do you create, copy, move, find, archive, and remove files safely from the command line?

What you should be able to do after this lesson:

  • Use cp, mv, rm, mkdir, touch, and find.
  • Understand globbing such as *.txt.
  • Work with archives such as tar.
  • Recognize cpio and dd.

Basic File and Directory Commands

Create an empty file

touch notes.txt

Create a directory

mkdir project
mkdir -p project/docs

Copy

cp file1.txt file2.txt
cp -r dir1 dir2

Move or rename

mv oldname.txt newname.txt
mv file.txt /tmp/

Remove

rm file.txt
rmdir emptydir

Recursive removal is a separate step:

rm -r olddir

Important beginner warning:

  • rm -r is powerful
  • always double-check the path before pressing Enter

File Globbing

The shell expands wildcard patterns before the command runs.

Common examples:

  • *.txt = all .txt files
  • file?.txt = one-character wildcard
  • [ab]* = names starting with a or b
  • [0-9]* = names starting with a digit

This is called file globbing.

Find Files

find is one of the most important commands in LPIC.

Find by name

find . -name "*.txt"

Example output:

./101.1 Determine and configure hardware settings.txt
./101.2 Boot the system.txt
./101.3 Change runlevels and boot targets, shutdown and reboot.txt
./102.1 Design hard disk layout.txt
./102.2 Install a boot manager.txt

What this shows:

  • find returns paths, not just file names
  • using . means "start searching from the current directory"

Find only directories

find . -type d

Find by size

find . -size +100M

Find by age

find . -mtime -7

Act on found files

find . -name "*.log" -exec ls -l {} \;

This is useful when you want to do something with each match, not just list it.

Check What a File Is

file archive.tar.gz

Example output from file /bin/ls:

/bin/ls: ELF 64-bit LSB pie executable, x86-64, dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, stripped

What this shows:

  • file tells you what something really is
  • this example shows that /bin/ls is a compiled executable

This helps when the extension is unclear or misleading.

Archives with tar

Create archive

tar -cf backup.tar project/

Extract archive

tar -xf backup.tar

Create compressed archive

tar -czf backup.tar.gz project/
tar -cJf backup.tar.xz project/

Compression Tools

gzip file.txt
gunzip file.txt.gz

bzip2 file.txt
bunzip2 file.txt.bz2

xz file.txt
unxz file.txt.xz

Awareness of cpio and dd

cpio

Archive tool you should recognize, even if tar is more common in daily use.

Very simple idea:

  • tar is the archive tool you will meet most often
  • cpio is still exam-relevant and worth recognizing by name and purpose

dd

Low-level copy tool.

Example:

dd if=image.iso of=/dev/sdX bs=4M status=progress

Be careful:

  • dd can overwrite disks very quickly
  • the wrong of= target can destroy data

Safer beginner habit:

  • identify disks first with lsblk
  • only then think about a dd write command

Practice Step by Step

1) Copy a directory safely
  1. Create a destination: mkdir backup
  2. Copy recursively: cp -r project backup/
  3. Verify: ls backup
2) Find log files changed in the last week
  1. Run: find /var/log -type f -mtime -7
  2. Read the result as: regular files under /var/log modified in the last 7 days.

Cheat Sheet

  • touch = create empty file or update timestamp
  • mkdir -p = create directories recursively
  • cp = copy files
  • cp -r = copy directories recursively
  • mv = move or rename
  • rm = remove file
  • rm -r = remove recursively
  • find = locate files by rules
  • file = identify file type
  • tar = create and extract archives
  • gzip, bzip2, xz = compress files
  • cpio = archive tool to recognize
  • dd = low-level copy tool, use with care
🎯

Test Your Knowledge

Complete the quiz to assess your understanding of this course's concepts.