Master Linux File Management: Directories, Paths, and Essential Commands
This guide explains Linux file management fundamentals, covering the hierarchical directory structure, naming conventions, absolute and relative paths, common shell commands for viewing, creating, moving, copying, and deleting files and directories, as well as file attributes, types, and the differences between hard and soft links.
1. Overview of File Management
Linux file management revolves around creating, copying, moving, viewing, editing, compressing, searching, and deleting files. Knowing the location of configuration files, such as the system hostname, is essential before making changes.
1.1 System Directory Structure
All operating systems organize files in a tree‑like directory hierarchy. Windows uses multiple roots (C:, D: …) while Linux uses a single root /. Below is a typical CentOS 7 layout:
Files and directories form an inverted tree structure.
The filesystem starts at the root /.
File names are case‑sensitive.
Hidden files begin with a dot ..
Path separator is /.
1.2 Naming Rules
Maximum filename length: 255 characters.
Maximum full path length (including directories): 4095 characters.
All characters except NUL and / are allowed.
Names are case‑sensitive.
1.3 Important Directories
/boot– boot loader and kernel files. /bin – commands usable by all users. /sbin – system‑admin commands. /lib and /lib64 – shared libraries. /etc – configuration files. /home/USERNAME – regular user home directories. /root – root user home directory. /media – mount points for removable media. /mnt – temporary mount points. /dev – device files. /opt – third‑party applications. /tmp – temporary files. /usr – most user‑installed software. /var – variable data such as logs. /proc – virtual files representing running processes. /sys – hardware information. /srv – data for services.
2. Paths: Absolute vs. Relative
An absolute path starts with / and specifies the full location, e.g., /home/alice/file. A relative path is interpreted from the current working directory, e.g., ./a.txt or ../b.txt. The entries . and .. represent the current directory and its parent, respectively.
2.1 Examples
# absolute path
[root@bgx /]# touch /home/alice/file1
[root@bgx /]# touch ~/file2
# relative path
[root@bgx /]# mkdir abc
[root@bgx /]# touch ../file3
[root@bgx /]# touch abc/file53. Common File Operations
3.1 Viewing File Status
# stat anaconda-ks.cfg
File: ‘anaconda-ks.cfg’
Size: 1747 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 33574992 Links: 1
Access: (0600/-rw-------) Uid: (0/ root) Gid: (0/ root)
Access: 2019-08-22 12:09:03.288000381 +0900
Modify: 2019-08-22 11:47:12.262947345 +0900
Change: 2019-08-22 11:47:12.262947345 +09003.2 Creating Files
Use touch to create an empty file or update timestamps. Options: touch -a file – modify access time only. touch -m file – modify modification time only. touch file{a..z} – create multiple files using brace expansion.
3.3 Wildcards
*– matches any string. ? – matches a single character. ~ – expands to the user’s home directory. [abc] – matches one of the listed characters. [0-9] – matches any digit. [:lower:], [:upper:], [:alpha:], [:alnum:] – POSIX character classes.
3.4 Creating Directories
# mkdir -p /opt/project/src # create parent directories as needed
# mkdir -v newdir # show created directory3.5 Displaying Directory Trees
Install tree and use it to visualise the hierarchy:
# yum install -y tree
# tree -d -L 2 /home/od
/home/od/
├── but
├── dir1
├── dir2
├── dir3
├── dir4
├── dir5
│ └── dir6
└── diu3.6 Removing Files and Directories
rmdir dir– removes an empty directory. rm -rf dir – forcefully removes a non‑empty directory.
3.7 Copying Files and Directories
# cp -i source dest # prompt before overwriting
# cp -r src_dir/ dest_dir/ # recursive copy of a directory
# cp -a src dest # archive mode (preserves attributes)3.8 Moving / Renaming
# mv -i oldname newname # interactive prompt
# mv -v file1 file2 /tmp/ # verbose output3.9 Deleting Files
# rm -i file # ask before each removal
# rm -rf /tmp/unwanted # remove recursively without prompts3.10 Viewing File Content
cat– concatenate and display. tac – display in reverse order. less / more – paginated view. head / tail – show beginning or end of a file; -n specifies line count, -f follows growth. grep – filter lines matching a pattern; options -i, -E, -n, -A, -B, -C control case, regex, and context.
4. File Attributes and Types
Using ls -l shows ten columns: type/permissions, link count, owner, group, size, modification date, and name. The first character indicates the file type ( - regular file, d directory, b block device, c character device, l symbolic link, s socket, p pipe).
4.1 Determining Exact Type
For ambiguous cases, the file command inspects the file’s contents:
# file /etc/hosts
/etc/hosts: ASCII text
# file /bin/ls
/bin/ls: ELF 64-bit LSB executable, x86-64, …
# file /dev/sda
/dev/sda: block special5. Links
5.1 Symbolic (Soft) Links
Created with ln -s target linkname. They store the path to the target, can span filesystems, and become dangling if the target is removed.
# touch /root/file
# ln -s /root/file /tmp/file_bak
# ll /tmp/file_bak
lrwxrwxrwx 1 root root 10 ... /tmp/file_bak -> /root/file5.2 Hard Links
Created with ln target linkname. Both names refer to the same inode; they cannot be made for directories and cannot cross filesystem boundaries. Deleting one name does not affect the underlying data as long as another hard link exists.
# ln /root/file /tmp/file_hard
# ll /tmp/file_hard
-rw-r--r-- 2 root root … /tmp/file_hard5.3 Differences
Soft links store a pathname; hard links share the same inode.
Soft links can point to directories and cross partitions; hard links cannot.
Removing the original file breaks a soft link but not a hard link.
Both types increase the link count shown by ls -l.
6. Useful One‑Liners
df -h– display filesystem usage. du -sh * – size of each item in the current directory. ls -R – recursive listing. find /etc -type f -name "*.conf" – locate configuration files. sort -t: -k2 -n file.txt | uniq -c – sort by the second field numerically and count duplicates.
These commands form the core toolkit for navigating and managing files on any Linux system.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
