Linux File Operations

Linux File Operations

Objectives:

  • explore the file system
  • explain the file architecture
  • compare files and identify different file types
  • backup and compress data

In Linux “almost” everything is a file, or treated as such.

Linux native file systems:

  • ext3
  • ext4
  • squashfs
  • btrfs

Linux can implement other file systems:

  •  windows (ntfs, fat, vfat)
  • sgi
  • macos (hfs, hfs+)

On a hard disk, partitions can organize data. Each partition can have its own file system.

A file system can be mounted at a mount point (usually an empty directory). If the directory is not empty, the contents of that directory are covered-up by the new file system and will not be available until the file system is unmounted.

# mount a file system
$ sudo mount /dev/sda5 /home

# unmount a filesystem/partition
$ sudo unmount /home

To automatically mount a filesystem every time the system starts up edit the /etc/fstab file.

# show mounted filesystems
$ df -hT

# display read-only or writable status of mounted file system
$ mount

NSF service

# enable NFS
$ sudo systemctl enable nfs

# start NSF
$ sudo systemctl start nsf

File /etc/exports contains the directory permissions. After modifying /etc/exports, run exportfs -av to notify Linux about directories that can be remotely mounted.

Directories

/proc contains virtual files that permit viewing constantly changing kernel data.

/dev contains device nodes.

/var contains files  that are expected to change in size as the system is running.

  • /var/logs – system logs
  • /var/lib – package and database files
  • /var/spool – print queues
  • /var/tmp – temporary files

/etc directory contains system configuration files; no binary programs

/boot directory contains essential files needed to boot the system. For every alternative kernel installed on the system there are four files:

  1. vmlinuz – compressed linux kernel
  2. initramfs – initial ram file system required fr booting (aka initrd)
  3. config – configuration file
  4. system.map – kernel symbol table (used for debugging)

GRUB files are also located in the /boot folder

  • /boot/grub/grub.conf
  • /boot/grub2/grub2.conf

/lib folder contains libraries for programs located in the /bin or /sbin folders.

Comparing Files

diff [options] <fileName1> <fileName2>

diff3 – compare changes make to 3 different files.

# compare the content of 3 files
$ diff3 myFile refFile yourFile
# creating a patch file
$ diff -NUr origFile newFile > patchFile

# apply the patch file
patch -p1 > patchFile
patch originalFile patchFile
# file utility; get information about the file
$ file fileName

Backing up data

Use rsync to backup data.

$ rsync --progress -avrxH sourceDir destDir

Compressing data

  • gzip – most common compression utility
  • bzip2 – produces smaller files than gzip
  • xz – most space efficient
  • zip – similar to gzip
  • unzip – extract all files in the backup file
  • tar – group files in an archive before compressing

Prefix compression commands with time to display compression times and sizes.

$ time tar zcf include.tar.gz include

Copying disk-to-disk

# backup the Master Boot Record
$ dd if=/dev/sda of=sda.mbr bs=512 count=1

# copy one disk to another
$ dd if=/dev/sda of=/dev/sdb

 

Leave a Reply