Skip to content

File System Basics with Bash

Copyright: © 2022 VEXIT , Tomorow is today ® , www.vexit.com
Author: Vex Tatarevic
Date Created: 2022-02-21

Overview

This tutorial is aimed at Windows users who have no knowledge of Linux operating system or Bash scripting language.

In this tutorial we learn basics of working with file system via command line interface using Bash - how to create, read, update and delete files and folders.

We will use bash scripting language to run commands in the terminal in order to interact with the operating system.

Bash (Bourne Again Shell) - is a command line shell program that is used in Linux and macOS operating systems. It is similar to Command Prompt in Windows operating system.

We will install Git Bash command line program which allows us to run bash commands on Windows.

Git Bash is a command line program that provides a Linux-like environment on Windows operating system.

In summary we are learning how to use bash commands to interact with the operating system because: 1. It is one language that allows you to speak to many different operating systems 2. It is the most common way to interact with Linux servers and we are preparing to learn how to manage Linux servers 3. Bash allows you to automate tasks on the server by writing bash scripts

Install Software

User Home Directory

User Home Directory - is a directory created automatically by the operating system at the time of user account creation. This directory is intended for storing only user related documents and programs.

The following is the location of user's home directory on the main 3 operating systems:

OS (Operating System) User's Home location
Linux /home/{{USERNAME}}
macOS /Users/{{USERNAME}}
Windows C:\Users\{{USERNAME}}

NOTE: In Windows operating system we use back slash symbol "\" to separate folders in the file path. In most other operating systems we use forward slash "/".

~ (tilde symbol) - is a shorthand notation for User Home directory path. In Linux command line, User Home directory path can be replaced with tilde symbol ~

You can navigate into User Home directory by using short notation "~" instead of the whole User Home path

cd ~

When inside User Home directory, the command line prompt will display ~ instead of the full directory path. It will look like this:

john@my-home-server:~ $

Follow these steps in your terminal to practise the basics of working with files and folders in Bash.

1. Go to your home directory

cd ~

Explanation: cd ~ changes into your user home directory (the ~ symbol stands for your home path).

2. See where you are

pwd

Explanation: pwd (print working directory) shows the full path of the folder you are currently in.

3. Create a project folder

mkdir -p ~/development/my-project

Explanation: -p means "parents": it creates any missing parent directories in the path. Without -p, mkdir development/my-project would fail if development does not exist yet; with -p, both development and my-project are created in one go.

4. Rename directory from development to dev

mv development dev

Explanation: mv moves or renames directories just like it does with files. Here we're renaming the development directory to dev in the current location.

5. Go inside the project folder

cd ~/dev/my-project

Explanation: cd changes the current directory to the path you give (here, into my-project).

6. Create a file

touch Test.txt

Explanation: touch creates an empty file with the name you give, or updates its “last modified” time if it already exists.

7. Remove the file

rm Test.txt

Explanation: rm deletes the specified file (remove).

8. Create a script file

touch test.sh

Explanation: touch creates an empty file; here we use it to create a new shell script file test.sh.

9. Rename the file

mv test.sh HelloWorld.sh

Explanation: mv moves or renames a file; with two names in the same folder it renames test.sh to HelloWorld.sh.

10. Edit the script and run it

Open the script in the nano editor:

nano HelloWorld.sh

Explanation: nano opens the file in a simple text editor inside the terminal.

Inside nano:

  1. Paste this code :

#!/bin/bash
echo "Hello world!"
2. Save: press Ctrl+S 3. Exit: press Ctrl+X

Explanation: The first line tells the system to run the file with Bash; echo prints text to the terminal.

Make the script executable and run it:

chmod +x HelloWorld.sh
./HelloWorld.sh

Explanation: chmod +x makes the file executable; ./HelloWorld.sh runs the script in the current directory.

You should see: Hello world!

11. Add a name prompt to the script

Open the script again:

nano HelloWorld.sh

Explanation: nano opens the file so you can edit it.

Add these lines after the existing ones (for example after echo "Hello world!"):

read -p "What's your name? " name
echo "Hello, $name !"

Save (Ctrl+S) and exit (Ctrl+X).

Explanation: read -p "..." name shows the message and stores what the user types in the variable name. In Bash, the dollar sign $ means "the value of"; so $name is replaced by whatever was stored in name, and echo "Hello, $name" prints that value.

Run the script again:

./HelloWorld.sh

Type your name when asked and press Enter; the script will print Hello, YourName !.

12. Go up two levels to your home directory

cd ../../

Explanation: .. means “the parent directory” (one level up). So ../../ means “go up two levels”: from my-project you go to development, then to your home directory. The last slash is optional but often used when writing directory paths.

12. List files: ls, ls -l, ls -la

List the contents of the current directory:

ls

Explanation: ls lists the names of files and folders in the current directory.

List with more details:

ls -l

Explanation: ls -l (long format) shows permissions, owner, size, date and name for each file and folder.

List including hidden files:

ls -la

Explanation: ls -la is long format (-l) plus all files (-a), so hidden files (names starting with .) are shown as well.

14. Copy the project folder

cp -r dev/my-project dev/my-project2

Explanation: cp -r copies a directory recursively: -r means “include all files and subfolders”; here it creates a copy named my-project2.

15. Remove the original project folder

rm -r dev/my-project

Explanation: rm -r removes a directory and everything inside it; -r means recursive (use with care).


You now have dev/my-project2 with your HelloWorld.sh script. You have used: creating and changing directories (mkdir, cd), creating and deleting files (touch, rm), renaming (mv), editing (nano), running a script (chmod +x, ./script), listing contents (ls, ls -l, ls -la), copying a folder (cp -r), and going up with ...

Commands Reference

NOTE: The text in double curly braces below, are the placeholders for your actual folder or file names e.g. {{your_folder}}

Command Description
pwd Print working directory
cd {{dir_path}} Change directory
cd ../ Go outside of current directory one level up
cd ~/ Go inside current user's directory
ls -la List files and directories. -l - long file description. -a - show hidden files
mkdir Make directory
mv {{dir_to_move_path}} {{dir_destination_path}} move one directory to another
nano {{file_path}} Open a file in nano editor

PWD - Print working directory

If want to know where we are in the file system we can use pwd command.

  • Inside your terminal type pwd and press enter

LS - List files and directories

If we want to see what files and directories are inside our current directory we can use ls command.

  • Inside your terminal type ls and press enter

Sort by file size

ls -lah [PATH] | sort -rh -k5
1. ls - Lists directory contents - -l - Long format (shows details like size, date, permissions) - -a - Show all files (including hidden ones) - -h - Human readable sizes (shows sizes in KB, MB, GB instead of bytes)

  1. | - Pipe operator, takes output from left command and feeds it to right command

  2. sort - Sorts the input

  3. -r - Reverse order (largest to smallest)
  4. -h - Human readable numbers (understands KB, MB, GB)
  5. -k5 - Sort by field 5 (in ls -l output, field 5 is the file size)

So together, this command: - Lists all files in the directory at the [PATH] with their details - Sorts them by size in descending order (biggest files first) - Shows sizes in human-readable format (MB, KB)

We should see files listed from largest to smallest, which helps us identify the biggest files / folders

WHICH - Find location of a program

If we want to know where a program is located on the system we can use which command.

  • Inside your terminal type which nano and press enter
  • You should get: /usr/bin/nano

SED - Stream Editor - Find & Replace Text

If we want to replace some text in a file we can use sed command.

  • Replace first occurrence of {{OLD_TERM}} with {{NEW_TERM}} in {{FILE_NAME}} file

    sed -i 's/{{OLD_TERM}}/{{NEW_TERM}}/' {{FILE_NAME}}

  • Replace all occurrences of {{OLD_TERM}} with {{NEW_TERM}} in {{FILE_NAME}} file

    sed -i 's/{{OLD_TERM}}/{{NEW_TERM}}/g' {{FILE_NAME}}

For example: if we want to replace all ocurrences of word hello with word world in a file test.txt we can use following command:

sed -i 's/hello/world/g' test.txt

PBCOPY / PBASTE - Copy and Paste from/to Clipboard

  • Copy text from file to clipboard

    pbcopy < {{FILE_PATH}}

OR

  cat {{FILE_PATH}} | pbcopy
  • Copy the text "Hello world" to clipboard

    pbcopy echo "Hello world"

  • Paste text from clipboard to file

    pbpaste > {{FILE_PATH}}

DU - Disk Usage

  • Find out how much space a directory or file is using

du -sh [Path]
| Flag | Meaning | Description | | ---- | -------------- | ------------------------------------------------------------------------------- | | -s | Summary | This will show the total size of the directory or file | | -h | Human readable | This will show the size in a human readable format like: 1.2M, 1.2G, 1.2T, etc. |