10 Linux Filesystem Interview Questions and Answers
Prepare for your next technical interview with our comprehensive guide on the Linux filesystem, featuring common questions and detailed answers.
Prepare for your next technical interview with our comprehensive guide on the Linux filesystem, featuring common questions and detailed answers.
The Linux filesystem is a critical component of the Linux operating system, providing a structured way to store and organize data. Known for its robustness, flexibility, and security features, the Linux filesystem is widely used in various environments, from personal computers to enterprise servers. Its hierarchical structure and support for multiple file types make it a versatile choice for managing complex data storage needs.
This article offers a curated selection of interview questions focused on the Linux filesystem. By reviewing these questions and their detailed answers, you will gain a deeper understanding of key concepts and practical skills, enhancing your readiness for technical interviews and professional challenges.
In the Linux filesystem, a hard link is a directory entry that associates a name with a file on a filesystem. Hard links point directly to the inode of a file, meaning multiple filenames can reference the same physical data on the disk. If the original file is deleted, the data remains accessible through the hard link.
A soft (symbolic) link is a special type of file that contains a reference to another file or directory in the form of a path. Symbolic links can span across different filesystems and can link to directories. If the original file is deleted, the symbolic link becomes a dangling link, pointing to a non-existent file.
Example commands:
Creating a hard link:
ln original_file hard_link
Creating a symbolic link:
ln -s original_file symbolic_link
To check the disk usage of a specific directory and all its subdirectories in Linux, you can use the du
(disk usage) command. The du
command estimates file space usage and provides a summary of the disk usage of each file and directory.
The basic syntax for checking the disk usage of a specific directory is:
du -sh /path/to/directory
Here, the -s
option provides a summary of the total disk usage, and the -h
option makes the output human-readable by showing sizes in KB, MB, or GB.
If you want to see the disk usage of the directory along with all its subdirectories, you can use:
du -h /path/to/directory
This command will display the disk usage of each subdirectory within the specified directory.
fstab
file, and how would you add a new filesystem to it?The fstab
file, located at /etc/fstab
, stands for “File System Table.” It is a configuration file that contains information about all the available disk partitions and storage devices and how they should be integrated into the filesystem structure of the operating system. Each line in the fstab
file represents a filesystem and contains six fields:
/dev/sda1
)/mnt/data
)ext4
, ntfs
)defaults
, ro
)dump
utility, usually set to 0
or 1
)fsck
, usually set to 0
, 1
, or 2
)To add a new filesystem to the fstab
file, you would follow these steps:
/dev/sdb1
).mkdir /mnt/newdisk
)./etc/fstab
file and add a new line with the appropriate fields.Example entry to add a new filesystem:
/dev/sdb1 /mnt/newdisk ext4 defaults 0 2
This entry tells the system to mount the device /dev/sdb1
to the directory /mnt/newdisk
using the ext4
filesystem with default mount options. The 0
indicates that the filesystem should not be dumped, and the 2
indicates that it should be checked second by fsck
after the root filesystem.
/var
directory.To find all files larger than 100MB in the /var
directory, you can use the find
command in Linux. The find
command is a powerful utility that allows you to search for files and directories based on various criteria.
find /var -type f -size +100M
Explanation:
/var
: Specifies the directory to search in.-type f
: Ensures that only files are considered (not directories).-size +100M
: Finds files larger than 100MB.To monitor free space on all mounted filesystems and send an alert if any fall below 10% free space, you can use a shell script. The script will utilize the df
command to check disk usage and mail
or echo
for sending alerts.
#!/bin/bash # Threshold for free space THRESHOLD=10 # Get the list of filesystems and their free space percentage df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output; do # Extract the percentage and filesystem name usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1) partition=$(echo $output | awk '{ print $2 }') # Check if the free space is below the threshold if [ $usep -ge $((100 - THRESHOLD)) ]; then echo "Alert: Almost out of disk space on $partition ($usep% used)" # Uncomment the following line to send an email alert # echo "Alert: Almost out of disk space on $partition ($usep% used)" | mail -s "Disk Space Alert" [email protected] fi done
To list all open files and the processes that opened them in a Linux filesystem, you can use the lsof
command. lsof
stands for “list open files” and is a powerful utility that provides detailed information about files that are opened by various processes.
Example:
lsof
This command will display a list of all open files along with the associated processes. The output includes columns such as the command name, process ID (PID), user, file descriptor, type, device, size, and the file name.
If you want to filter the results to show open files for a specific user, you can use the -u
option followed by the username:
lsof -u username
To list open files for a specific process ID, use the -p
option followed by the PID:
lsof -p PID
/home
directory to another drive, ensuring that only changed files are copied.To back up the /home
directory to another drive while ensuring that only changed files are copied, you can use the rsync
command. rsync
is a powerful tool for efficiently transferring and synchronizing files between different directories or drives. It only copies the files that have changed, which makes it ideal for backup purposes.
Here is a simple script to achieve this:
#!/bin/bash # Define source and destination directories SOURCE="/home/" DESTINATION="/mnt/backup_drive/home_backup/" # Use rsync to copy only changed files rsync -av --delete $SOURCE $DESTINATION
In this script:
-a
stands for “archive” mode, which preserves permissions, timestamps, symbolic links, and other attributes.-v
stands for “verbose” mode, which provides detailed output of the process.--delete
ensures that files deleted from the source are also deleted from the destination, keeping the backup in sync.In Linux, filesystem permissions and ownership are fundamental concepts that control access to files and directories. Each file and directory has an associated owner and group, and permissions are set to define what actions the owner, group members, and others can perform.
Permissions are represented by a combination of read (r), write (w), and execute (x) rights. These permissions are grouped into three categories:
Permissions can be viewed using the ls -l
command, which displays a string of characters representing the permissions. For example, -rwxr-xr--
indicates that the owner has read, write, and execute permissions, the group has read and execute permissions, and others have only read permissions.
Ownership of a file includes:
To change permissions, the chmod
command is used. To change ownership, the chown
command is used.
Example:
# Change permissions to read, write, and execute for the owner, and read and execute for the group and others chmod 755 example.txt # Change the owner to 'user' and the group to 'group' chown user:group example.txt
Filesystem quotas are used to limit the amount of disk space and the number of inodes (files) that a user or group can use. This helps in managing disk usage and ensuring that no single user can consume all the available disk space, which could affect other users and system performance.
To set up and manage filesystem quotas for users in a Linux environment, follow these steps:
/etc/fstab
file to add the usrquota
and/or grpquota
options to the filesystem where you want to enable quotas. For example:
/dev/sda1 /home ext4 defaults,usrquota,grpquota 0 2
mount -o remount /home
quotacheck
command:
quotacheck -cum /home
quotaon
command:
quotaon /home
edquota
command to set quotas for individual users. This will open a text editor where you can specify the soft and hard limits for blocks and inodes:
edquota username
quota
command to check the quota usage for a user:
quota username
In Linux, checking and repairing filesystems is a task to ensure data integrity and system stability. Several tools and methods are available for this purpose, each suited to different types of filesystems.
fsck (File System Consistency Check): This is a general-purpose tool that can be used to check and repair various types of filesystems. It acts as a front-end for specific filesystem checkers like e2fsck
for ext2/ext3/ext4 filesystems, xfs_repair
for XFS filesystems, and others.
e2fsck: This tool is specifically designed for ext2, ext3, and ext4 filesystems. It checks the filesystem for errors and attempts to repair them. It can be run manually or automatically during system boot.
xfs_repair: This tool is used for XFS filesystems. Unlike e2fsck
, xfs_repair
does not run automatically at boot time. It must be run manually and is known for its efficiency in handling large filesystems.
Steps to Check and Repair Filesystems:
fsck
, e2fsck
, or xfs_repair
depending on the filesystem type.Example usage:
# Unmount the filesystem umount /dev/sdX1 # Check and repair ext4 filesystem e2fsck -p /dev/sdX1 # Check and repair XFS filesystem xfs_repair /dev/sdX1