πŸ—ƒοΈ Linux/Unix: File & Directory Operations
Estimated reading: 4 minutes 283 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 :
Share

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

Or Copy Link

CONTENTS
Scroll to Top