Welcome to Part 2 of the Linux Bash course. In this part, we transition from simple shell commands to manipulating files, reading manual pages to understand complex options, and utilizing input/output redirection to combine simple utilities into powerful command chains.
If you are planning to deploy services configured with these methods, you might find our guide on Installing Docker on Ubuntu very helpful. For more details, consult the official GNU Bash Manual.
Chapter 4: Essential File Operations
4.1 Copying Files: The cp Command
The cp command copies files from a source location to a destination.
Syntax:
cp [options] source_file destination_file
Example Command:
cp -vi config.txt config.txt.bak
Expected Output:
'config.txt' -> 'config.txt.bak'
Flag & Command Breakdown:
cp: Copy files utility.-v: Verbose. Displays the names of files as they are copied.-i: Interactive. Prompts for confirmation before overwriting an existing destination file.config.txt: The source file path.config.txt.bak: The destination file path.
4.2 Copying Directories Recursively: cp -r and cp -a
To copy whole directory trees, you must copy recursively. The -a (archive) flag is highly recommended as it preserves file permissions, ownership, timestamps, and symbolic links.
Syntax:
cp -r source_dir destination_dir
cp -a source_dir destination_dir
Example Command:
cp -va assets/backup_assets/
Expected Output:
'assets/' -> 'backup_assets/'
'assets/images' -> 'backup_assets/images'
'assets/images/icons' -> 'backup_assets/images/icons'
Flag & Command Breakdown:
cp: Copy utility.-v: Verbose output.-a: Archive mode. Combines recursive copying (-R) and preserves ownership, permissions, and creation times.assets: Source directory.backup_assets/: Destination directory.
4.3 Moving and Renaming Files: The mv Command
The mv command moves or renames files and directories. Unlike copying, moving files on the same physical disk partition does not duplicate any data; it simply updates the filesystem metadata pointers.
Syntax:
mv [options] source destination
Example Command:
mv -vi temp_notes.txt documents/notes.txt
Expected Output:
renamed 'temp_notes.txt' -> 'documents/notes.txt'
Flag & Command Breakdown:
mv: Move or rename files.-v: Verbose. Confirms renaming actions.-i: Interactive. Prompts before overwriting any destination file.temp_notes.txt: Source file.documents/notes.txt: Destination path.
4.4 Deleting Files: The rm Command
The rm (Remove) command deletes files.
[!WARNING] There is no trash bin or recycle bin in the command line. Once a file is deleted with
rm, it is immediately deleted from the filesystem index and cannot be easily recovered.
Syntax:
rm [options] file...
Example Command:
rm -vi old_temp.log
Expected Output:
rm: remove regular empty file 'old_temp.log'? y
removed 'old_temp.log'
Flag & Command Breakdown:
rm: Remove files.-v: Verbose output.-i: Interactive. Prompts the user for confirmation before removing.old_temp.log: The name of the file to remove.
4.5 Deleting Directories Recursively: rm -r and rm -rf
By default, rm cannot delete directories. You must use recursive flags to traverse and delete directories.
Syntax:
rm -r directory_name
rm -rf directory_name
Example Command:
rm -rfv tmp_cache/
Expected Output:
removed directory 'tmp_cache/images'
removed 'tmp_cache/settings.json'
removed directory 'tmp_cache/'
Flag & Command Breakdown:
rm: Remove utility.-ror-R: Recursive. Deletes all contents and the directory itself.-f: Force. Ignores nonexistent files and arguments, and never prompts for confirmation.-v: Verbose output.tmp_cache/: Directory to delete.
4.6 Creating Hard Links: The ln Command
A hard link is another directory entry pointing to the same underlying inode (data block) on the disk. Modifying either file modifies the data. Deleting one leaves the other intact. Hard links cannot span across different filesystems or partitions and cannot be created for directories.
Syntax:
ln source_file link_name
Example Command:
ln original.txt hardlink.txt
Expected Output:
(No console output, but running ls -i reveals both files share the same inode number)
Flag & Command Breakdown:
ln: Link creator command.original.txt: The source file.hardlink.txt: The name of the new hard link.
4.7 Creating Symbolic Links: ln -s
A symbolic link (symlink or soft link) is a special file containing a text path pointing to another file or directory (similar to a desktop shortcut). Unlike hard links, symlinks can point to directories and can span multiple filesystems. If the original file is deleted, the symlink becomes “broken.”
Syntax:
ln -s target_path link_name
Example Command:
ln -sv /var/log/nginx/access.log web_logs
Expected Output:
'web_logs' -> '/var/log/nginx/access.log'
Flag & Command Breakdown:
ln: Link creator command.-s: Symbolic. Creates a soft link instead of a hard link.-v: Verbose output./var/log/nginx/access.log: The absolute target path.web_logs: The name of the soft link file.
4.8 Viewing File Types: The file Command
Linux does not rely on file extensions (like .txt or .exe) to determine what a file is. The file command inspects the file contents, tests magic numbers, and displays the exact file type (e.g. ASCII text, JPEG image, ELF executable).
Syntax:
file [options] file_name...
Example Command:
file image.png script.sh binary_app
Expected Output:
image.png: PNG image data, 800 x 600, 8-bit/color RGBA
script.sh: Bourne-Again shell script, ASCII text executable
binary_app: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV)
Flag & Command Breakdown:
file: Analyzes and reports file types.image.png,script.sh,binary_app: File paths to evaluate.
4.9 Comparing Files: The diff Command
The diff command compares two files line-by-line and outputs the differences.
Syntax:
diff [options] file1 file2
Example Command:
diff -u config_old.conf config_new.conf
Expected Output:
--- config_old.conf 2026-06-12 13:00:00.000000000 +0530
+++ config_new.conf 2026-06-12 13:05:00.000000000 +0530
@@ -2,4 +2,4 @@
port=8080
-debug=false
+debug=true
timeout=30
Flag & Command Breakdown:
diff: Computes differences between files.-u: Unified output format, which is the industry standard for code patches and git diff outputs.
4.10 Viewing File Statistics: The stat Command
The stat command displays detailed status info for a file or filesystem, including sizes, inodes, permissions, links, and three timestamps: Access (atime), Modify (mtime), and Change (ctime).
Syntax:
stat [options] file_name...
Example Command:
stat config.txt
Expected Output:
File: config.txt
Size: 42 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 144321 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ suresh) Gid: ( 1000/ suresh)
Access: 2026-06-12 13:00:00.000000000 +0530
Modify: 2026-06-12 13:10:00.000000000 +0530
Change: 2026-06-12 13:10:00.000000000 +0530
Birth: -
Flag & Command Breakdown:
stat: Returns detailed inode information.config.txt: Target file name.
Chapter 5: Understanding Manual Pages
5.1 The Structure of Manual Pages
Linux manual pages (man pages) are organized into structured numbered sections to avoid naming collisions:
- Section 1: User commands (e.g.
ls,grep,mkdir). - Section 2: System calls (kernel functions like
fork,open). - Section 3: Library calls (programming language functions like
printfin C). - Section 4: Special files (device files in
/dev). - Section 5: File formats and configuration files (e.g.
/etc/passwd,hosts). - Section 6: Games and screensavers.
- Section 7: Miscellaneous overviews and standards (e.g.
man 7 ip). - Section 8: System administration commands (root commands like
iptables,reboot).
5.2 Searching for Manual Pages: The man -k and apropos Commands
If you do not know the exact command name, you can search the manual short descriptions for a keyword using man -k or apropos.
Syntax:
apropos keyword
man -k keyword
Example Command:
apropos "directory size"
Expected Output:
du (1) - estimate file space usage
ncdu (1) - NCurses Disk Usage
Flag & Command Breakdown:
apropos: Searches man page summaries for the string “directory size”.
5.3 Navigating Man Pages
When you run a man page, it loads the default text pager, which is typically less. Key shortcuts to navigate include:
Spacebar/Page Down: Scroll down by one page.Page Up/b: Scroll up by one page.Down Arrow/Up Arrow: Scroll line-by-line./pattern: Search for a pattern forward. Pressnto go to the next match, orNto go to the previous.q: Quit the manual reader and return to the shell prompt.
5.4 Reading a Specific Man Page Section
To view a manual entry from a specific section (such as the configuration file instead of the user command), pass the section number as an argument.
Syntax:
man section command
Example Command:
man 5 passwd
Expected Output:
(Opens a manual page explaining /etc/passwd configuration syntax rather than the passwd command)
Flag & Command Breakdown:
man: Manual pager utility.5: Restricts search specifically to Section 5 (File formats).passwd: The topic query.
5.5 Displaying Command Brief Summaries: The whatis Command
The whatis command displays single-line manual page descriptions.
Syntax:
whatis command...
Example Command:
whatis cd ls rm
Expected Output:
cd (1posix) - change the working directory
ls (1) - list directory contents
rm (1) - remove files or directories
Flag & Command Breakdown:
whatis: Queries man database for a command’s short synopsis.
5.6 The info Documentation System
The info system is another help documentation viewer designed by the GNU Project. Unlike flat man pages, info pages are hierarchical and hyperlinked, reading like digital books with chapters and node links.
Syntax:
info command_name
Example Command:
info coreutils
Expected Output: (Opens an interactive info node document with clickable menu items)
Flag & Command Breakdown:
info: GNU hypertext documentation viewer.coreutils: The package node to document.
5.7 Location of Executables: The which Command
The which command checks the directories in the user’s $PATH environment variable and returns the path of the executable file that would be executed.
Syntax:
which command_name
Example Command:
which bash
Expected Output:
/usr/bin/bash
Flag & Command Breakdown:
which: Locates the command path matching the$PATHsearch.
5.8 Locating Binary, Source, and Man Pages: The whereis Command
Unlike which, the whereis command searches a hardcoded list of standard system directories to find all occurrences of binary files, source code, and manual page files matching the query.
Syntax:
whereis command_name
Example Command:
whereis python3
Expected Output:
python3: /usr/bin/python3 /usr/lib/python3 /usr/share/man/man1/python3.1.gz
Flag & Command Breakdown:
whereis: Locates binary, source and manual locations.
5.9 Shell Builtin Help: The help Command
External manuals do not document shell builtins well. Bash provides help specifically to query builtin details.
Syntax:
help [builtin_command]
Example Command:
help -s cd
Expected Output:
cd: cd [-L|[-P [-e]] [-@]] [dir]
Flag & Command Breakdown:
help: Utility to document Bash builtins.-s: Outputs only a brief syntax synopsis of the command.cd: The shell builtin target.
5.10 Online and Local Documentation Resources
While terminal commands are powerful, excellent resources also exist online:
- GNU Bash Reference Manual: Complete reference.
tldrpage project: An online collaborative community providing simplified, practical example sheets for commands (tldr tar).
Chapter 6: Redirection and Pipes
6.1 Understanding Standard Streams
When a program executes in Linux, it opens three standard data streams:
- Standard Input (stdin): Stream ID
0. Receives input data (typically from keyboard entry). - Standard Output (stdout): Stream ID
1. Sends normal program output text. - Standard Error (stderr): Stream ID
2. Sends diagnostic and error messages separately from standard outputs.
6.2 Redirecting Output to a File: >
The > operator redirects the standard output (stdout) stream to a file.
[!CAUTION] If the destination file already exists, it is immediately overwritten (truncated) without warning.
Syntax:
command > destination_file
Example Command:
echo "Server initialization completed" > install.log
Expected Output:
(No terminal output; file install.log is created or overwritten with the string contents)
Flag & Command Breakdown:
echo: Generates the text stdout.>: Intercepts stdout (stream 1) and writes it into the target file path.
6.3 Appending Output to a File: >>
To append stdout without deleting existing contents, use the >> operator.
Syntax:
command >> destination_file
Example Command:
echo "Backup execution finished" >> install.log
Expected Output:
(The string is appended to the bottom of install.log)
6.4 Redirecting Standard Input: <
The < operator redirects the standard input (stdin) stream from a file.
Syntax:
command < source_file
Example Command:
wc -l < install.log
Expected Output:
2
Flag & Command Breakdown:
wc: Word count utility.-l: Count lines.<: Opensinstall.logand feeds its content intowcas standard input.
6.5 Redirecting Standard Error: 2> and 2>&1
To redirect standard error (stderr) separately from stdout, prefix the redirect with its file descriptor number 2.
Syntax:
command 2> error.log
command > output.log 2>&1
Example Command:
ls /root 2> error.log
Expected Output:
(No terminal output, but error.log is created containing the error message)
Flag & Command Breakdown:
ls /root: Tries to list directory, which fails if run as standard user.2>: Catches stderr (stream 2) and saves it toerror.log.
6.6 Discarding Output: Redirecting to /dev/null
/dev/null is a special virtual device file (sometimes called the “bit bucket”) that discards all data written to it and returns end-of-file reading from it.
Syntax:
command > /dev/null 2>&1
command &> /dev/null
Example Command:
ping -c 2 8.8.8.8 > /dev/null
Expected Output: (No terminal output; the ping outputs are swallowed)
Flag & Command Breakdown:
ping -c 2 8.8.8.8: Sends two ping packets.> /dev/null: Silences standard output completely.
6.7 Combining Output Streams: The modern &> redirect
Bash 4.0 introduced &> to redirect both stdout and stderr to a file simultaneously.
Syntax:
command &> destination_file
Example Command:
ls -ld /root /home &> combined.log
Expected Output:
(No screen output; combined.log contains both success listing and error logging)
6.8 Connecting Commands: The Pipe Operator |
The pipe operator | connects the standard output of the command on the left directly to the standard input of the command on the right. This allows chaining multiple commands.
Syntax:
command1 | command2 | command3
Example Command:
ls /usr/bin | wc -l
Expected Output:
1452
Flag & Command Breakdown:
ls /usr/bin: Generates a list of all binary filenames in stdout.|: Passes stdout oflsto standard input ofwc.wc -l: Reads the stream of filenames and outputs the total line count.
6.9 Teeing Off: The tee Command
The tee command reads standard input and writes it to both standard output and one or more files. It acts like a “T” splitter in a physical pipe.
Syntax:
command | tee [options] file...
Example Command:
echo "Metric logged" | tee -a metrics.txt
Expected Output:
Metric logged
Flag & Command Breakdown:
tee: Copies stdin to stdout and tometrics.txt.-a: Appends to the file instead of overwriting.
6.10 Here Documents (<<) and Here Strings (<<<)
- Here Document (EOF): Feeds a multi-line string block into a command.
- Here String: Feeds a single string variable directly into standard input.
Syntax:
command << LIMIT
line 1
line 2
LIMIT
command <<< "single line string"
Example Command:
cat << 'EOF' > info.txt
Project Name: TechBlog
Author: Suresh
EOF
Expected Output:
(No screen output; file info.txt is created with the multi-line content block)