103.3 Perform Basic File Management

LPIC-1 Exam Objective 103.3: Perform Basic File Management #

Introduction #

In this tutorial, we will cover essential file management commands in Linux. These commands are fundamental for any system administrator, as they allow you to manage files and directories effectively. We will provide examples for Ubuntu/Debian and Enterprise Linux (such as CentOS/RHEL).

Key Knowledge Areas #

  1. Copy, move, and remove files and directories individually.
  2. Copy multiple files and directories recursively.
  3. Remove files and directories recursively.
  4. Use simple and advanced wildcard specifications in commands.
  5. Using find to locate and act on files based on type, size, or time.
  6. Usage of tar, cpio, and dd.

Commands Overview #

  • cp: Copy files and directories.
  • mv: Move or rename files and directories.
  • rm: Remove files and directories.
  • rmdir: Remove empty directories.
  • mkdir: Create directories.
  • ls: List directory contents.
  • touch: Create empty files or update file timestamps.
  • find: Search for files in a directory hierarchy.
  • tar: Archive files.
  • cpio: Copy files to and from archives.
  • dd: Convert and copy files.
  • file: Determine file type.
  • gzip, gunzip, bzip2, bunzip2, xz, unxz: Compress and decompress files.
  • File globbing: Use wildcards to match file names.

Basic File Management Commands #

Copying Files and Directories #

Copy a single file:

cp source_file destination_file

Copy a directory recursively:

cp -r source_directory destination_directory

Example:

cp /etc/hosts ~/hosts_backup
cp -r ~/Documents /media/backup/Documents

Moving and Renaming Files and Directories #

Move or rename a file:

mv source_file destination_file

Move a directory:

mv source_directory destination_directory

Example:

mv ~/hosts_backup ~/backup/hosts
mv ~/Documents /media/backup/Documents

Removing Files and Directories #

Remove a single file:

rm file_name

Remove a directory recursively:

rm -r directory_name

Example:

rm ~/backup/hosts
rm -r /media/backup/Documents

Remove an empty directory:

rmdir directory_name

Creating Directories #

Create a single directory:

mkdir directory_name

Create multiple directories and parent directories:

mkdir -p /path/to/new/directory

Example:

mkdir ~/new_folder
mkdir -p ~/new_folder/sub_folder

Using Wildcards #

Wildcards allow you to match file names based on patterns.

Examples:

cp *.txt /path/to/destination/
mv data?.csv /path/to/destination/
rm file[1-3].txt

Using find to Locate and Act on Files #

Find files by name:

find /path -name "pattern"

Find files by size:

find /path -size +100M

Find files by modification time:

find /path -mtime -7

Example:

find /var/log -name "*.log"
find /home -size +1G
find /tmp -mtime -1

Using tar, cpio, and dd #

Create a tar archive:

tar -cvf archive.tar /path/to/directory

Extract a tar archive:

tar -xvf archive.tar

Example:

tar -cvf backup.tar ~/Documents
tar -xvf backup.tar -C /media/backup

Create a cpio archive:

find /path/to/directory | cpio -ov > archive.cpio

Extract a cpio archive:

cpio -idv < archive.cpio

Example:

find ~/Documents | cpio -ov > backup.cpio
cpio -idv < backup.cpio

Copy data using dd:

dd if=/dev/sda of=/path/to/backup.img

Example:

dd if=/dev/sda of=~/disk_backup.img

File Compression #

Compress a file using gzip:

gzip file_name

Decompress a file using gunzip:

gunzip file_name.gz

Compress a file using bzip2:

bzip2 file_name

Decompress a file using bunzip2:

bunzip2 file_name.bz2

Compress a file using xz:

xz file_name

Decompress a file using unxz:

unxz file_name.xz

Example:

gzip example.txt
gunzip example.txt.gz
bzip2 example.txt
bunzip2 example.txt.bz2
xz example.txt
unxz example.txt.xz

Conclusion #

Mastering these basic file management commands is crucial for effective Linux system administration. Practice these commands regularly to become proficient in managing files and directories in a Linux environment.