Listing Directory Contents
The ls utility is used to list directory contents and file information. By default, it displays non-hidden files in the current working directory. To view detailed metadata such as permissions, ownership, and size, use the -l flag. To include hidden files—those prefixed with a dot (.)—append the -a option.
# Detailed listing including hidden files
ls -la
# List attributes of a directory itself rather than its contents
ls -ld /var/log
# Use wildcards to match patterns
ls *.conf
Hidden files in Linux are denoted by a leading dot. Every directory contains two special entries: . (representing the current directory) and .. (representing the parent directory). These entries facilitate relative path navigation.
Navigating the Filesystem
To identify the current location within the filesystem, the pwd (print working directory) command is used. Changing directories is accomplished via cd.
# Change to a specific directory using absolute path
cd /usr/local/bin
# Navigate using relative path
cd ../etc
# Switch to the user's home directory
cd ~
# Return to the previous working directory
cd -
The root directory / serves as the hierarchy's top; attempting to go above it using cd .. will leave the user at the root.
Creating Directories
New directories are created using mkdir. If the parent directory does not exist, the -p flag allows for the recursive creation of nested directories.
# Create a single directory
mkdir project_alpha
# Create a nested directory structure
mkdir -p project_alpha/src/main/java
To visualize directory structures, the tree command is often employed. If not installed, it can be added using the system's package manager.
# Install tree (RHEL/CentOS)
yum install -y tree
# Display directory hierarchy
tree project_alpha
User Management and Identity
The whoami command outputs the username of the current effective user. Administrators can create new user accounts using utilities like adduser, which establishes a home directory (typically under /home) for the new user.
Command Execution Mechanics
Commands are fundamentally executable programs stored on the disk. These binaries are frequently located in standard directories such as /usr/bin. The installation of software generally involves copying these binaries into system paths defined in the $PATH environment variable.
Locating and Aliasing Commands
To find the absolute path of an executable, which is used. It may reveal that a command is actually an alias—a shorthand for another command or a string of options.
# Find the location of the ls command
which ls
# Output: alias ls='ls --color=auto'
# /usr/bin/ls
# Create a custom alias
alias listall='ls -la'
Aliases allow users to define custom shortcuts for frequently used complex commands.
Managing File Timestamps
The touch command updates the access and modification timestamps of a file. If the specified file does not exist, touch creates it as an empty regular file. For detailed metadata, stat provides the Access (atime), Modify (mtime), and Change (ctime) timestamps.
- Access: Time when the file contents were last read.
- Modify: Time when the file contents were last written to.
- Change: Time when the file's inode metadata (permissions, ownership) was last altered.
Removing Files and Directories
The rmdir command removes empty directories only. For general deletion, rm is used. While rm prompts for confirmation by default (especially for the root user), the -f flag forces deletion without prompts. To remove directories containing content, the -r (recursive) flag is mandatory.
# Remove a file forcefully
rm -f filename.log
# Remove a directory and its contents recursively
rm -rf old_data/
Warning: Executing rm -rf / will initiate the recursive deletion of the entire root filesystem, resulting in catastrophic data loss.
Documentation
Linux provides comprehensive manual pages via the man command. These manuals cover executable programs, system calls, and library functions.
# Read the manual page for the ls command
man ls
# Navigate using arrow keys; press 'q' to quit
Copying and Moving
The cp command copies files or directories. When copying directories, the -r flag is required. The destination can be a directory or a new filename, effectively allowing for copy-and-rename operations.
# Copy a file to a parent directory
cp config.txt ../
# Copy a directory recursively
cp -r scripts/ /usr/local/bin/custom_scripts
The mv command moves files or directories. It is also used to rename items. The operation is instantaneous when moving within the same filesystem as it only updates directory inode entries rather than copying data blocks.
# Rename a file
mv old_name.txt new_name.txt
# Move a file to a different directory
mv archive.tar.gz /tmp/
Operational Tips
1. Termination: If a command output becomes uncontrollable (e.g., an infinite scroll), press Ctrl+C to send an interrupt signal and terminate the process.
2. Conflict Resolution: Copying a file into a directory where a file with the same name exists will typically result in an overwrite prompt or an error, depending on the flags used.