Working with Directories and File Systems
In this step, we will explore how to use the stat
command with directories and file systems. This knowledge is valuable for system administration tasks and understanding storage usage.
Stat Command with Directories
The stat
command works with directories in the same way it works with files. Let's create a test directory and examine its metadata:
mkdir -p ~/project/test_dir
stat ~/project/test_dir
You should see output similar to what we saw for files, but with "directory" instead of "regular file" in the file type field.
The main difference between files and directories is that directories typically have more links (one for each immediate subdirectory plus two for the directory itself and its parent).
Let's create a subdirectory and see how it affects the link count:
mkdir -p ~/project/test_dir/sub_dir
stat ~/project/test_dir
You should notice that the link count has increased by 1.
The stat
command can also provide information about file systems using the -f
or --file-system
option. This option shows details like the file system type, block size, and usage statistics.
Let's examine the file system that contains our project directory:
stat -f ~/project
The output should include:
- File system type
- Block size
- Total blocks
- Free blocks
- Available blocks (for non-root users)
- Total inodes
- Free inodes
This information is useful for monitoring disk space usage and planning capacity.
Combining Options
We can combine the file system option with the custom format option to extract specific information:
stat -f -c 'File System: %T\nTotal size: %b blocks\nFree: %f blocks\nAvailable: %a blocks' ~/project
This command displays the file system type, total blocks, free blocks, and available blocks.
Following Symbolic Links
By default, when the stat
command encounters a symbolic link, it shows information about the link itself, not the file it points to. Let's create a symbolic link and see this in action:
echo "Target file content" > ~/project/target_file.txt
ln -s ~/project/target_file.txt ~/project/symlink.txt
stat ~/project/symlink.txt
The output should show that the file type is "symbolic link" and the size is the length of the path to the target file.
To see information about the target file instead of the link, use the -L
or --dereference
option:
stat -L ~/project/symlink.txt
Now the output should show information about the target file, including its size and type as "regular file".
Practical Application: Finding Large Directories
One practical application of the stat
command is to find large directories. Let's create a script that calculates directory sizes:
echo '#!/bin/bash
for dir in "$@"; do
if [ -d "$dir" ]; then
size=$(du -s "$dir" | cut -f1)
echo "Directory: $dir, Size: $size KB"
else
echo "$dir is not a directory"
fi
done' > ~/project/dir_size.sh
chmod +x ~/project/dir_size.sh
Now, let's run the script on our test directory:
~/project/dir_size.sh ~/project/test_dir
This script uses the du
command to calculate directory sizes, complementing the metadata information provided by the stat
command.
By understanding how to use the stat
command with directories and file systems, you can gain valuable insights into your Linux system's storage organization and usage.