5.1 File & Directory Utilities, Inodes, Links & Special Device Nodes

Key Takeaways

  • Use ls, stat and file to inspect paths. Compare device and inode numbers when testing object identity.

  • Hard links are names for one inode. A symbolic link stores a target pathname and can refer to directories or cross mounts.

  • Linux can keep storage allocated after the last name is removed while the file is still open or mapped. Use lsof to investigate the affected mount.

  • GNU find evaluates live paths and can batch matching names into a bounded command argument list. Locate reads an index that may be stale.

  • Block and character device nodes use a major/minor pair within their type. A device’s behavior depends on the driver or kernel interface.

Last updated: November 2026

5.1 File & Directory Utilities, Inodes, Links & Special Device Nodes

Identify the path, object type and mounted filesystem before modifying data. Use ls, stat, and file to inspect individual paths, find to traverse live directories, and lsof to connect an open object to a process.

File and directory operations

TaskCommands and considerations
Navigatecd changes the shell's current directory. cd .. moves to the parent in the default logical mode, cd - returns to OLDPWD when set, and bare cd normally uses HOME. Bash's builtin pwd -L reports the logical path and pwd -P resolves symbolic links in it. Use explicit flags when logical versus physical spelling matters.
List entriesls -l shows type, mode, link count, owner, group and usually size and modification time. ls -a includes dot entries, and ls -A omits only . and .. among those entries. Use ls -ld directory for the directory itself. ls -i shows inode numbers. Use ls -lh for human-readable sizes.
Create or remove a directorymkdir -p lab/releases/v1.2 creates missing components and accepts existing directories. mkdir -m 750 private requests mode 0750 for the new directory. With -p, created parent directories follow separate permissions. rmdir removes an empty directory. Its -p option also tries each named parent path in turn after removing the leaf directory.
Copy or moveFor a recursive GNU copy, use cp -R. cp -p requests mode, ownership and timestamp preservation. cp -a includes recursive copying, preservation of symlinks and hard-link relationships, and available attributes. Preservation depends on permissions and destination support. Check the required attributes after the operation.
Remove a namerm unlinks a file name. -i asks before removal, -r recurses into directories, and -f suppresses prompts and ignores absent operands. Permissions, active policy and mount restrictions still apply.

A successful mv implemented with a Linux rename within the same mount keeps the source inode and any open references. If it replaces the destination file, the pathname replacement is atomic. Cross-filesystem GNU mv normally copies to the destination and removes the source after a successful copy. This is a different sequence with different failure and metadata considerations. Inspect mounts and destination paths before a large move.

Metadata, timestamps and comparisons

Use stat to get the object type, mode, owner, inode, size, allocated block count and timestamps reported by the OS. By default GNU stat inspects a symlink itself. Use stat -L to inspect its referent. On GNU systems the following reports a device number, inode number, hard-link count, logical size and allocated blocks:

stat -c 'device=%d inode=%i links=%h size=%s blocks=%b' lab/report.txt
TimestampMeaning
atimeAccess time, updated according to the operation and mount policy. relatime or noatime can suppress updates that would otherwise occur.
mtimeModification time. Writes can update it. Adding or removing a directory entry also modifies the containing directory.
ctimeInode status change time, updated by data or metadata changes such as writes, changes to mode and changes in link count.
Birth timeFile creation time where the filesystem and interface supply it. GNU stat displays a dash when it is unknown.

touch changes access and modification times and creates an empty regular file when the path is absent unless creation is suppressed with -c. Use touch -a for access time, touch -m for modification time, or touch -t 202601011200 lab/report.txt for a specific local timestamp. Changing these timestamps can also update the inode status change time.

file lab/report.txt attempts to classify the object using filesystem checks, magic tests and language or text tests. It can identify formats such as ELF or gzip when the byte pattern and format match. Use the result as a clue and verify with a format-specific tool if the distinction matters.

diff -u old.conf new.conf shows context around line-level changes. sdiff old.conf new.conf presents changes side by side. Use GNU sdiff -o merged.conf old.conf new.conf to merge interactively into a separately named output. Use cmp for a byte comparison.

Inodes and links

A directory entry associates a name with an inode number. The inode supplies file information such as owner, mode, type, time values and hard-link count. On ext4 it also contains or references filesystem data and allocation information. Inode numbers have meaning within a filesystem. Compare both device and inode when checking whether two names refer to the same object. On ext4 many small files can exhaust inodes while blocks remain. Compare df -i and df -h for the affected mount.

OperationHard linkSymbolic link
Createln lab/report.txt lab/report.hard adds a name for the existing object and increases its link count.ln -s report.txt lab/report.soft stores a pathname in a separate symbolic link. The target link count stays the same.
ResolveBoth names identify the same inode and reflect changes to that object.A relative target starts in the directory that holds the symlink. An absolute target starts at the applicable root.
ScopeLinux disallows hard links to directories and rejects links across mount boundaries.Can name a directory or a target on another mounted filesystem, subject to normal pathname resolution.
Remove one nameThe remaining hard link still reaches the object.Removing the target name leaves a dangling link if the stored path no longer resolves. readlink can still read the stored path.

For a lab example, start with only lab/report.txt, then run both link commands in the table. Compare the entries with ls -li lab/report.* and stat. You should see two names for the original inode and a separate symlink whose target is report.txt. Renaming the original name leaves the hard link usable. The symlink reports the old path until a path with that name is restored or the link is replaced. If the last hard link is removed while the file is still open or mapped, the remaining reference can keep the object's storage allocated.

Live search and an indexed shortcut

GNU find tests live directory entries. Quote name patterns so the shell passes the pattern intact.

FilterMeaning
-name '*.log', -iname '*.log'Match base names, with the second form ignoring case.
-type f, -type d, -type lSelect regular files, directories or symbolic links with the default no-follow behavior.
-size +104857600cLogical size greater than 100 MiB, measured in bytes. GNU suffixes such as k, M, and G select binary units and round up before comparing. For example -size 1M matches from 1 through 1,048,576 bytes.
-mtime -7, -mtime +30For past timestamps, the first matches a time less than seven full days ago. The second matches at least 31 full days ago. GNU find discards fractional days in its default comparison.
-perm 644, -perm -644, -perm /644Exact mode bits, all requested bits, or any requested bits, respectively. The permission predicate tests mode bits.
-uid 1005, -user webadmin, -group developers, -nouserSelect numeric or named identities, or identify a UID that cannot be resolved using the system's configured lookup.

For a disposable tree, compare the call patterns:

find ./lab/logs -type f -name '*.log' -exec wc -c -- {} \;
find ./lab/logs -type f -name '*.log' -exec wc -c -- {} +

The first form invokes wc once for each match. The second batches matches into argument lists bounded by command-size limits and can invoke wc repeatedly for different batches. Each pathname stays an individual argument even if it contains spaces or newlines. In a batch with multiple files wc may also print a total. Inspect search results before using an action that changes files.

locate uses an index in common implementations such as plocate, so it can be fast and may omit new paths or return stale entries. Index scope, refresh schedule and location depend on the host. updatedb refreshes the configured index. Use find when the task requires live metadata or confirmation of the current path. For shell lookup rather than file search, Bash's type -a ls can show aliases, functions or executable paths.

Investigate a disk-usage discrepancy

Use lsof to inspect files that visible processes are using. lsof -p 4512 scopes the listing to a PID, lsof -i TCP:443 -sTCP:LISTEN selects TCP listeners on port 443, and lsof +D ./lab/logs scans that directory tree, which can be expensive on large trees.

Suppose df -h /var shows little available space, and a permitted du -xsh /var traversal accounts for much less on the same mount. Check for files hidden by mount changes, metadata or reserved capacity, and open unlinked regular files. lsof +L1 selects visible open entries with link counts below one. If /var is itself the affected mount, lsof +aL1 /var scopes that check to it. Match the device, object type, process and link count. Logical size alone does not show allocated blocks. If a daemon still writes to an unlinked log, consult its documented reopen or restart procedure and verify that references close and space is recovered.

Block and character devices

A device node is an interface identified by a type and a major/minor pair. The kernel uses that pair within the type to address the registered device. Examine ls -l and system device information. A disk such as /dev/sda may appear as type b with numbers 8, 0, while a serial terminal can appear as c. A block device provides block-oriented access such as a disk, partition or device-mapper volume. Character device operations depend on the driver, including terminal interfaces and kernel pseudo-devices.

PathBehavior
/dev/nullWrites are discarded and ordinary reads return end of file.
/dev/zeroReads supply NUL bytes. Writes are discarded.
/dev/urandomSupplies pseudorandom bytes from the kernel RNG. For cryptographic applications that must wait for initialization at early boot, use an appropriate library backed by getrandom().
/dev/randomA legacy interface whose blocking behavior is kernel-version dependent. On Linux 5.6 and later it normally blocks only during early boot.

In the lab directory, head -c 16 /dev/zero > lab/sixteen.bin writes a finite sample to a regular file. Use wc -c lab/sixteen.bin to see 16 bytes. Use a new file name if you need to preserve a prior sample.

Test Your Knowledge

On a GNU/Linux lab host, which command passes each matching log pathname intact to wc and lets find batch paths into multiple arguments per invocation?

A

find ./lab/logs -type f -name '*.log' -exec wc -c -- {} +

B

find ./lab/logs -type f -name '*.log' -exec wc -c -- {} ;

C

find ./lab/logs -type f -name '*.log' -print

D

wc -c -- $(find ./lab/logs -type f)

Test Your Knowledge

Comparing the same /var mount, df reports little free space and du accounts for much less usage. A service is suspected of still writing to a removed log. Which command selects visible open files whose link count is zero on that mount?

A

df -i /var

B

locate /var/log

C

lsof +aL1 /var

D

du -xsh /var

Test Your Knowledge

On Linux, /mnt/storage and /etc are on distinct mounted filesystems. A user has the needed path permissions and wants to name the /mnt/storage/conf directory from /etc. Why does ln /mnt/storage/conf /etc/conf_link fail?

A

Only root can create any hard link

B

Linux rejects hard links to directories and across these mounts

C

A path passed to ln must always be enclosed in quotes

D

An absolute source path requires changing into its directory first

Sections you finish are checked off in the contents.