Bash scripting

Published: December 23, 2025

Rolling Post
Last updated: January 19, 2026

Bash (Bourne-Again SHell) - Used to write scripts and run commands in unix based systems.

Shell - Text based interface to talk to the computer. Its's both a command interpreter and a programming language. As a command interpreter, it gives access to the GNU utilities and as programming language it combines these utilities.

Files can be created containing these commands and the files become commands.

Common Bash commands

- ls - List directories

Options

  • -l - Long list formatting - Gives detailed information about files and folders such as file permission, number of links, owner name, owner group, file size, time of last modification, file/directory name. Sample output -rw-r--r-- 1 kavete kavete 873 Nov 18 10:00 test.txt
  • -a - Include hidden files
  • -h - Human readable sizes - Uses kilobytes, Megabytes etc instead of bytes. Use with long listing format -lh
  • -t - Sort by modification date / time
  • -r - Reverse order while sorting
  • -R - List sub-directories recursively
  • -S - Sort by file size - Largest file size first.
  • -1 - List one file per line
  • -d -List directories themselves, not their contents.
  • -F - Append indicators (/=@|*) to entries
Indicators

/ Directories

@ Symbolic links

* Executables

- cd - Change directory

Options

  • cd .. - Move up one directory

  • cd ~ - Move to the home directory

  • cd - - Move to the previous directory

  • cd / - Move to the root directory

- mkdir - Make a new directory

Options

  • -p -Create parent directories as needed Example: mkdir -p parent/child

  • -v -Show a message for each created directory (Verbose output)

  • -m - Set file mode (permissions)
    Example: mkdir -m 755 some_dir

- pwd - Print working directory

Options

  • -L -Show the logical path, including any symbolic links (default).
    Sample output: /sbin

  • -P - Show physical path, resolving any symbolic links to their actual locations.
    Sample output (If /sbin actually pints to /usr/bin): /usr/bin

- mv - Move or rename files

Basic Usage

mv source_file destination_directory
Example: mv file.txt /some_path/in/computer/

Renaming Files

mv old_file.txt new_file.txt

Options

  • -i - Ask before replacing files

  • -u - Move only if the source is newer

  • -v - Verbose mode (show files being moved)

Use with wildcards

Wildcards can be used to move multiple files at once. Example mv *.rs /rust will move all files with .rs extension to the rust directory.

- cp - Copy files and directories

Basic usage

cp source destination - Makes a duplicate of the file/directory.

Options

  • -r - (Recursive) Copy all files and folders inside a directory
  • -i -(interactive mode) Ask before replacing files
  • -u - Copy only if the source is newer
  • - - (Verbose mode) Show files being copied

- rm - Delete/ Remove files and directories

Options

  • -r - Delete a folder and everything inside it
  • -f - Force delete without asking
  • -i and -v

- echo - Display a line of text.

Options

  • -E -Don't allow special characters (default).
  • e - Allow special characters like \n for new lines.
  • -n - Don't add a new line at the end. (It's added by default)

- cat - Concatenate and display files

Used to show content of files. Can also be used to combine multiple files into one.

Options

  • -n - Add numbers to each line.
  • -b - Add numbers to the lines with text. (Ignore empty ones).
  • -s -Remove extra empty lines. It leaves only a single blank line where multiple ones exist.
  • -v - Show non-printing characters, except for tabs and end of line.

Concatenate two files

cat main.rs build.rs > lib.rs - Takes the contents of build.rs and main.rs and writes them into lib.rs

Using cat with piping

the cat command is often used in conjunction with piping to send the content of files to other commands. Example: cat main.rs | ripgrep "panic"

- touch <filename> - Create a new file or update it's time

Options

  • -a - (Access) Update only when the file was last read.
  • -m - (Modification) Update only when the file was last changed.
  • -t - Set the timestamp to a specific time.
    Example: touch -t 202601010000 memory.x -c - Do not create any files if they do not exist.

- man [command] -Display user manual of any command

Example: man ls

Related Posts

Vim basics

Programming

Modes

Normal Mode

Used for :
1. Moving the cursor
2. Deleting characters, words, lines etc
3. Performing …

Dec 23, 2025

Git basics

Programming

Git - Version control system

Uses

  • Tracking code changes
  • Collaborating

Key Git Concepts

Dec 23, 2025