πŸ—ƒοΈ Linux/Unix: File & Directory Operations
Estimated reading: 4 minutes 35 views

πŸ”— 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 and du
  • How to inspect files using stat, file, and touch
  • 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:

TypeDescription
Hard LinkPoints directly to the inode of the file. Data shared.
Symbolic LinkPoints 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 and file 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ”΅ Linux/Unix: File Links (ln), File System Basics (df, du, stat, file, touch)

Or Copy Link

CONTENTS
Scroll to Top