π 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
df
anddu
- 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:
ln
creates both hard and symbolic links for file reuse and shortcuts.df
shows overall disk space;du
analyzes directory-level usage.stat
andfile
give detailed file info;touch
is 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 :