pwd
BASH Academy
This page will cover a few useful commands that you can use in Git BASH.
You will see many generalised paths, directory names, file names, etc. on this page. For example, you will see something like cd /path_to/working_directory/you_want
where you would need to replace the /path_to/working_directory/you_want
part with a path that is applicable to your work.
Avoid using Bash commands like find
, ls
or grep
within OneDrive folders unless OneDrive syncing is switched off. OneDrive will download all files to search them, which can consume significant bandwidth and storage. It’s advisable to pause OneDrive syncing if working with Bash commands within your local OneDrive folders.
Viewing Files and Directories
The commands in this section help you list and view the contents of directories, providing detailed information about files and sub-directories.
ls
: This stands for “listing”.
To list all files or sub-directories, we would type the following in the terminal:
ls
To list all files or sub-directories, including hidden files, we would type the following in the terminal:
ls -a
You can add more options to ls
to control how to list files. We can use -ltrh
options modify the output of ls
:
l
: Use a long listing format.t
: Sort by modification time, newest first.r
: Reverse the order while sorting.h
: Display sizes in a human-readable format.
To list the contents of the specified directory in long format, sorted by modification time (newest last), in reverse order, with human-readable file sizes, we would type the following in the terminal:
ls -ltrh /path/to/directory
Creating and Removing Files and Directories
Make sure you’re in the correct directory/using the correct path before using commands in this section.
Creating directories
The commands in this section enable you to create new directories, remove files, and delete empty directories.
mkdir
: This stands for “make directory”.
To create a directory, we would amend the code below to reflect the new directory name and enter it into the terminal:
mkdir new_directory_name
This will create a new directory in your current working directory.
Creating files
echo
The echo command is used to display a line of text or a variable value. It can also be used to create files by redirecting the output to a file.
To create a file named hello.txt and writes “Hello, World!” into it, we would enter the following in the terminal:
echo "Hello, World!" > hello.txt
To append text to an existing file instead of overwriting it, you can use the >> operator:
echo "This is additional text." >> hello.txt
This command adds “This is additional text.” to the end of the hello.txt file without overwriting its existing content.
touch
The command in this section enables you to create an empty file or update the timestamp of an existing file. We do this by entering the following this command template touch <file_name.extention>
where you replace file_name with the name of the file you want to create and extention
with the extension for the type of file you want to create.
For example, if we want to create a txt
type file called example
, then we would type the following in the terminal:
touch example.txt
If you use the touch
command with a file that already exists, then the timestamp for when that file was last modified will be updated to when you executed that command.
For example, if I had a file called example.txt
that already existed in my directory and its timestamp for when it was last modified was 27/01/2025 at 09:00 AM. Then I used the command touch example.txt
on 28/01/2024 at 10:30 AM, the timestamp for that file will be modified to reflect the time I entered the command (28/01/2024 at 10:30 AM).
Removing files
Assuming we’re already in the directory we want, we can remove a file from it by amending the code below to reflect the file name we want to remove, and its extension and then enter it into the terminal:
rm file_name.extention
You need to make sure to include the extension of the file in the command, or it will not work.
Removing directories
Make sure you’re in the correct directory/using the correct path before using commands in this section.
rm
: This stands for “remove” and we can use it to remove files and directories.
To remove a directory and its contents, we would amend the code below to reflect the path and the directory we want and enter it into the terminal:
Be cautious when using the rm -r command in Git Bash, as it recursively deletes the specified directory and all its contents. This action is irreversible, so double-check the directory path to avoid accidentally deleting important files or directories.
rm -r path_to_the_directory/directory_name
Viewing File Contents
Make sure you’re in the correct directory/using the correct path before using commands in this section.
The commands in this section allow you to display the contents of files, either in full or in parts, for easy reading and inspection.
To display the first 10 lines of a file, we would amend the code below to reflect the file name we want to view and its extension and then enter it into the terminal:
head file_name.extention
You can also customise the number of lines you want to view by adding -number of lines
after head
. For example, if we want to view the first 12 lines of a file, we use:
head -12 file_name.extention
To display the bottom 10 lines of a file, we would amend the code below to reflect the file name we want to view and its extension and then enter it into the terminal:
tail file_name.extention
You can also customise the number of lines you want to view by adding -number of lines
after tail
. For example, if we want to view the last 3 lines of a file, we use:
tail -3 file_name.extention
To display the contents of a file, we would amend the code below to reflect the file name we want to view and its extension and then enter it into the terminal:
cat file_name.extention
Searching and Finding Files
Finding files by patterns
Make sure you’re in the correct directory/using the correct path before using commands in this section.
You can look for files in your repository that match a certain pattern by using git grep
. We would amend the code below by replacing "pattern"
with the term or pattern we want to search for in files.
grep "pattern" filename
You can utilise all of regex’s capabilities to locate precisely what you’re looking for because regular expressions are supported.
Finding files by name of file type
The find
command is used to search for files and directories within a directory hierarchy.
This command searches for all .txt
files within the specified directory and its subdirectories.
find . -name "*.txt"