Linux Learning Chapter 4: Moving Around the File System

📢 This article was translated by gemini-3-flash-preview

Chapter 4: Moving Around the File System

The Linux file system stores all information on the computer.

DirectoryDescription
/binContains common Linux user commands like ls, sort, date, and chmod.
/bootContains the bootable Linux kernel, initial RAM disk, and boot loader configuration files (GRUB).
/devContains files representing system device access points. These include terminals (tty*), hard drives (hd* or sd*), RAM (ram*), and CD-ROMs (cd*). Users can access devices directly via these files, though apps usually hide them.
/etcContains administrative configuration files. Most are plain text and can be edited with any text editor if you have the right permissions.
/homeContains directories for every user with a login account (except root, which uses /root).
/mediaA standard location for auto-mounting devices (especially removable media). Usually uses the volume name as the mount point.
/libContains shared libraries needed by apps in /bin and /sbin to boot the system.
/mntBefore /media became standard, /mnt was the common mount point for many devices. Some bootable Linux systems still use it for partitions and remote file systems. Many use it for temporary mounts.
/miscSometimes used for auto-mounting file systems on request.
/optDirectory structure for storing add-on application software.
/procContains information about system resources.
/rootThe home directory for the root user. Kept separate from /home for security reasons.
/sbinContains administrative commands and daemons.
/sysContains parameters for tuning block storage and managing cgroups.
/tmpContains temporary files used by applications.
/usrContains user docs, games, graphics files (X11), libraries (lib), and other commands/files not needed during boot. Files in /usr are generally static after installation and can technically be mounted as read-only.
/varContains data directories used by various apps. Includes files for FTP (/var/ftp) or Web (/var/www) servers, system logs (/var/log), and spool files (/var/spool, like mail/cups). Files here change frequently. On servers, /var is often a separate partition.

4.1 Basic File System Commands

cd, pwd, mkdir, rmdir, ls, touch

4.2 Using Metacharacters and Operators

Certain special characters are known as metacharacters or operators.

4.2.1 File Matching Metacharacters (Wildcards)

MetacharacterDescription
*Matches any number of characters
?Matches any single character
[…]Matches any character within the brackets; can include ranges of letters or numbers separated by a hyphen.

Example: ls [a-g]* lists files or directories starting with letters a through g.

4.2.2 File Redirection Metacharacters

MetacharacterDescription
<Directs file content into a command. This is default behavior and often omitted. Example: less bigfile is same as less < bigfile.
>Directs standard output (stdout) to a file. Overwrites the file if it exists.
2>Directs standard error (stderr) to a file.
&>Directs both stdout and stderr to a file.
»Appends command output to the end of an existing file.

Example command:

1
man chmod | col -b > /tmp/chmod

Formats the man page (man), strips backspaces (col -b), and saves output to /tmp/chmod (overwriting it if it exists).

1
echo "Hello World!" >> ~/hello

Appends Hello World! to the file ~/hello.


Another type of redirection is called here text (or here document):

1
2
3
4
5
6
7
ed /etc/resolv.conf << resendit
a
nameserver 100.100.100.100
.
w
q
resendit

This uses the ed editor to automate adding a DNS server IP to /etc/resolv.conf (usually in a script run by root) using the content between the resendit tags.

4.2.3 Brace Expansion

Using {} allows you to expand a set of characters across filenames, directories, or arguments:

1
touch memo{1,2,3,4,5}

Creates 5 files: memo1 through memo5.

1
touch {John,Bill,Sally}-{Breakfast,Lunch,Dinner}

Creates 9 files.

1
touch {a..f}{1..5}

Creates 30 files from a1 to f5.

4.3 Listing Files and Directories

Usually, ls is aliased to ls --color=auto. Check aliases with:

1
alias ls

Use ls -la to see detailed info (-l) and hidden files (-a).

ColumnFileDirectory
1PermissionsPermissions
2Number of linksNumber of links
3OwnerOwner
4GroupGroup
5Size in bytesSize of the directory file itself (not contents)
6Last modification date/timeLast modification date/time
7FilenameDirectory name

Notes:

  1. Time/date format depends on the LANG variable.
  2. An executable might have s permissions (-rwsr-sr-x), meaning any user can run it, but the process runs with the ownership of the app owner/group (SetUID/SetGID).
  3. If permissions end in t (drwxrwxr-t), it’s a “sticky bit”. Users can add files, but cannot delete other users’ files.
  4. SetGID on a directory makes new files inherit the directory’s group. If you see capital S or T instead of execution bits, it means SetGID or the sticky bit is set, but the execution bit is off.
  5. A plus sign at the end (-rw-rw-r-+) indicates extended attributes like ACLs or SELinux. A dot indicates SELinux attributes.

cd ~yexca goes to yexca’s home. cd - returns to the previous working directory (stored in $OLDPWD). cd . refers to the current directory ($PWD).

ls flags: -t sorts by modification time; -F adds / to directories, * to executables, and @ to symlinks; --hide=yexca hides specific files/dirs; -S sorts by size; -d shows info about the directory itself instead of its contents.

4.4 Understanding File Permissions and Ownership

The first column of ls -l:

The first character is the file type:

LetterType
-File
dDirectory
lSymbolic link
bBlock device
cCharacter device
sSocket
pNamed pipe

The next nine characters are permissions: first three for user (u), middle three for group (g), last three for others (o). (Acronym: ugo).

Permissions mean slightly different things for files vs. directories:

PermissionFileDirectory
ReadView file contentList contents of the directory
WriteChange content, rename, or deleteAdd or delete files/subdirectories within it
ExecuteRun file as a programEnter the directory (cd), search it, or access file metadata

Check any file/dir permissions with ls -ld.

In Fedora and RHEL, new users get their own group with the same name. This is the User Private Group (UPG) scheme.

4.4.1 Using chmod (Numeric) to Change Permissions

r = 4; w = 2; x = 1

1
chmod -R 755 ~/myfile

Recursively (-R) sets ~/myfile and its contents to 755 (rwxr-xr-x).

4.4.2 Using chmod (Symbolic) to Change Permissions

1
chmod ug+rx files
1
chmod -R o-x ~/myfile

4.4.3 Using umask for Default Permissions

Normal user defaults: files rw-rw-r--, directories rwxrwxr-x. Root user defaults: files rw-r--r--, directories rwxr-xr-x. These are set by umask. Type umask to see the value (e.g., 0002).

The umask value is subtracted from 666 (files) or 777 (dirs). A umask of 002 results in 775 for dirs and 644 for files (execution bits are off by default for files).

  • To change umask temporarily: umask [value]. Example: umask 000 creates wide-open files/dirs.
  • To make it permanent, add the umask command to your .bashrc file.

4.4.4 Changing File Ownership

Only root can change ownership.

1
chown user:group /etc/file

Changes /etc/file to be owned by user and group. Use -R for recursive changes. To change just the owner: chown yexca /etc/file.

4.5 Moving, Copying, and Deleting Files

mv, cp, rm, rmdir

mv -i: interactive mode, prevents accidental overwrites. mv -b: creates a backup of existing files before overwriting.

cp -a: Archive mode, preserves timestamps and attributes.

rm -f: Force delete, ignores non-existent files and never prompts. (Be careful with rm -rf /).