I present to you a compilation of the most useful Linux commands for File System Operations
File System Analysis
Check file integrity
Use fsck
to check and repair errors on file systems.
fsck /dev/hda1
fsck.ext2 /dev/hda1
fsck.ext3 /dev/hda1
fsck.vfat /dev/hda1
fsck.msdos /dev/hda1
Check for bad sectors
Detect bad sectors on a device.
badblocks -v /dev/hda1
Formatting File Systems
Format a device
Prepare a device for use with a specific file system.
mkfs /dev/hda1 # Linux
mkfs -t vfat 32 -F /dev/hda1 # FAT32
mke2fs /dev/hda1 # ext2
mke2fs -j /dev/hda1 # ext3
Other formatting options
- Format a device to
ext4
:
mkfs.ext4 /dev/hda1 # ext4
- Format with specific options:
mkfs.ext4 -O ^metadata_csum /dev/hda1 # Format disabling metadata checksum
Mounting File Systems
Mount devices
Associate a file system with a mount point.
mount /dev/hda2 /mnt/hda2 # Hard drive
mount /dev/fd0 /mnt/floppy # Floppy drive
mount /dev/cdrom /mnt/cdrom # CD/DVD
mount /dev/hdc /mnt/cdrecorder # Rewritable CD/DVD
mount /dev/sda1 /mnt/usbdisk # USB pen-drive
mount -o loop file.iso /mnt/cdrom # ISO image
Unmount devices
Detach a file system from the mount point.
umount /dev/hda2
fuser -km /mnt/hda2 # Force unmount
Additional mounting options
- Mount with specific permissions:
mount -o rw /dev/hda2 /mnt/hda2 # Mount in read-write mode
- Mount NTFS file systems:
mount -t ntfs-3g /dev/sda1 /mnt/ntfs # NTFS
ISO Images and CDROM Burners
Work with ISO images
Manage CD/DVD images and burning.
mount -o loop cd.iso /mnt/iso # Mount ISO image
mkisofs /dev/cdrom > cd.iso # Create ISO image from CD
mkisofs -J -allow-leading-dots -R -V "Label CD" -iso-level 4 -o ./cd.iso data_cd # From a directory
cdrecord -v dev=/dev/cdrom cd.iso # Burn ISO image
cdrecord -v gracetime=2 dev=/dev/cdrom -eject blank=fast -force # Clean rewritable CD
Verify the content of an ISO image
- List files in an ISO image without mounting it:
isoinfo -l -i cd.iso
Working with SWAP
Manage swap space
Create and activate swap partitions.
mkswap /dev/hda3 # Create swap file
swapon /dev/hda3 # Activate swap
Deactivate swap
swapoff /dev/hda3 # Deactivate swap