Linux/Unix: File Links & File System Basics β ln, df, du, stat, file, touch
Introduction β Why Learn File Links and File System Commands?
Linux/Unix file systems offer powerful features like hard and symbolic links, storage reporting, and file metadata access. Commands like ln, df, du, stat, and file are essential for storage analysis, troubleshooting, and managing complex systems.
In this guide, youβll learn:
- How file links (
ln) work and when to use hard vs soft links - How to check disk usage and storage with
dfanddu - How to inspect files using
stat,file, andtouch - Practical examples to understand file-level operations
ln β Create File Links (Hard & Symbolic)
Syntax:
ln [options] target link_name
Description:
Creates links between filesβeither hard links or symbolic (soft) links.
Types of Links:
| Type | Description |
|---|---|
| Hard Link | Points directly to the inode of the file. Data shared. |
| Symbolic Link | Points to the file path (like a shortcut). |
Examples:
ln file1.txt file1_hardlink.txt # Create hard link
ln -s file1.txt file1_symlink.txt # Create symbolic link
Tip: Use ls -li to compare inodes of original and hard link.
π½ df β Display Disk Space Usage
Syntax:
df [options] [path]
Description:
Reports available and used disk space for mounted file systems.
Example:
df -h # Human-readable (GB/MB)
df -T /home # Show filesystem type
Output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 100G 40G 60G 40% /
du β Display File and Directory Sizes
Syntax:
du [options] [path]
Description:
Summarizes disk usage of files and directories.
Example:
du -sh * # Show sizes of files/folders in current dir
du -ah /var/log # Show all files with sizes
Output:
4.0K config.log
12M apache2
20M total
stat β Display File Information
Syntax:
stat filename
Description:
Shows detailed metadata about a file, including size, permissions, timestamps, and inode.
Example:
stat file1.txt
Output:
Size: 4096 Blocks: 8 IO Block: 4096 regular file
Access: 2025-06-15 12:30:45.000000000 +0530
Modify: 2025-06-14 10:45:00.000000000 +0530
Change: 2025-06-14 10:46:10.000000000 +0530
file β Identify File Type
Syntax:
file filename
Description:
Detects the type of a fileβnot by its extension, but by content and header signatures.
Example:
file myimage.jpg
file script.sh
Output:
myimage.jpg: JPEG image data
script.sh: Bourne-Again shell script, ASCII text executable
touch β Create Empty File or Update Timestamp
Syntax:
touch filename
Description:
- Creates an empty file if it doesnβt exist.
- Updates access/modification timestamps if the file exists.
Example:
touch newlog.txt # Create an empty file
touch -t 202501010000 file # Set custom timestamp
Useful for:
- Creating placeholder files
- Simulating old/new file activity
Summary β Recap & Next Steps
These core commands give you visibility and control over how Linux stores and links files. From creating symbolic links to analyzing space usage and reading file metadata, these tools form the foundation of Linux file system mastery.
Key Takeaways:
lncreates both hard and symbolic links for file reuse and shortcuts.dfshows overall disk space;duanalyzes directory-level usage.statandfilegive detailed file info;touchis great for creating or modifying timestamps.- Knowing file system utilities is essential for sysadmins, developers, and anyone scripting in bash.
FAQs
Whatβs the difference between hard and soft links?
Hard links point to the same inode (data), while symbolic links point to the file path. If the original is deleted, symbolic links breakβhard links donβt.
How can I see disk usage in a human-readable format?
Use:
df -h
Can I use du to find the biggest folders?
Yes:
du -sh * | sort -hr
How do I update only a file’s timestamp?
Use:
touch filename
What does file check in a file?
It examines file headers and content to determine actual type, not based on file extension.
Share Now :
