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, andfind. - Understand globbing such as
*.txt. - Work with archives such as
tar. - Recognize
cpioanddd.
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 -ris powerful- always double-check the path before pressing Enter
File Globbing
The shell expands wildcard patterns before the command runs.
Common examples:
*.txt= all.txtfilesfile?.txt= one-character wildcard[ab]*= names starting withaorb[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:
findreturns 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:
filetells you what something really is- this example shows that
/bin/lsis 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:
taris the archive tool you will meet most oftencpiois 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:
ddcan overwrite disks very quickly- the wrong
of=target can destroy data
Safer beginner habit:
- identify disks first with
lsblk - only then think about a
ddwrite command
Practice Step by Step
1) Copy a directory safely
- Create a destination:
mkdir backup - Copy recursively:
cp -r project backup/ - Verify:
ls backup
2) Find log files changed in the last week
- Run:
find /var/log -type f -mtime -7 - Read the result as:
regular files under
/var/logmodified in the last 7 days.
Cheat Sheet
touch= create empty file or update timestampmkdir -p= create directories recursivelycp= copy filescp -r= copy directories recursivelymv= move or renamerm= remove filerm -r= remove recursivelyfind= locate files by rulesfile= identify file typetar= create and extract archivesgzip,bzip2,xz= compress filescpio= archive tool to recognizedd= low-level copy tool, use with care
Test Your Knowledge
Complete the quiz to assess your understanding of this course's concepts.
