Learn the Command Line!
svg files cannot be displayed: https://replanalytics.repl.co/track/1a7dc1bf-462c-47c6-8cf3-11f52de679be/badge.svg
How to use Bash
Hi, today I will be teaching you how to navigate the file system using a software called Bash
. Bash
stands for Bourne Again SHell. Bash
is the most popular Terminal
software. Terminal
is an application that comes installed on Windows, Mac, and Linux, which you can use to navigate between, create, and view files.
Note: If you see something called
CLI
, just know that is shorthand for "Command Line"
Opening Bash on MacOSX
Bash
is the default Terminal
Program in MacOSX
.
Bash
comes installed on all versions of MacOS
before MacOS Catalina
. MacOS Catalina
and after, there is a very similar application called Zsh
. It's basically the same thing. To open Bash, first navigate to Finder
. Then, go to the Applications
folder. In there, look for Terminal
. Double click the icon, and voila! You have opened Bash
on MacOSX
!
If you had a little trouble understanding all this, here's the step-by-step version
- Navigate to
Finder
. - Go to the
Applications
folder. - Search for
Terminal
. - Double click the icon that shows up.
Opening Bash on Linux and Ubuntu
Bash
is the default Terminal
program in Linux and Ubuntu
.
To open Bash
in Linux or Ubuntu
, go to the list of programs and open the program that says Terminal
or Terminal Emulator
. Open it, and you have opened Bash
on Linux/Ubuntu
!
Opening Downloading Bash
on Windows
Opening Bash
is much more complicated on Windows
. Sadly, Windows
does not use the Bash
software for Terminal/Powershell/Command Prompt
. While the Bash
and the software Windows
uses for its CLI
are different, some commands that are available in Bash
are not in Windows
' CLI
. So just take note that Bash
is much better. Since Bash
is not directly available on Windows
, we will be downloading something called Git Bash
. This is virtually the same thing as Bash
.
Okay, so now let's actually download Git Bash
for Windows
.
First, navigate to this site: Git Bash.
Then click the Download
link. If you can't see it, here is the link: Download Git Bash. You may need Administrator
control over the computer. Go through all the steps the Wizard takes you through. At some point, it may ask you for your text editor preference. I recommend selecting Visual Studio Code
. Finally, press Finish
. The application might start up automatically, or, if it doesn't, go to the Windows
search bar. Search for Git Bash
. Press Enter
. You have successfully opened Bash
for Windows
!
Do not ever 'Pin'
Git Bash
to your taskbar inWindows
. Some glitches may occur while doing this.
That was quite a lot! If you had trouble understanding the steps, here is the task-by-task version
- Open the
Git for Windows
link: Git Bash - Click the download link: Download Git Bash
- You might need
Administrator
control over the computer to downloadGit Bash
. Do what the download wizard says. When it asks you for the text editor of your choice, I recommend you selectVisual Studio Code
.
Now let's actually start using Bash
!
First, we're going to learn some very basic words. Directory
means a folder. Files
are things that contain information like words or code.
Before we start learning the commands, I just need to tell you a few very useful tricks:
- Using the
tab key
. This can be very useful when typing out long directories or files. If you want to navigate to a directory called "Something Random", you can use this:cd "Something + 'tab'".
When you hit the tab key, it will automatically fill up the directory name. It's okay if you don't understand whatcd
means. You will learn it before the end of this tutorial. - Another very useful trick is
clear
. This just cleans up theTerminal
.
Next I'll teach you some commands.
First is cd
.
cd
stands for Change Directory
cd
changes from the current directory to the specified directory. For example, say my current place is /c/users/username/
. The next directories I can go to are Desktop
, Downloads
, Documents
, and some more. I'll teach you how to see that these directories are accessible later. I want to go to the Desktop
directory. So, I type in cd Desktop
. And voila! You are now in the Desktop
directory! However, IF the next directory is two or more words, you must use quotes around the directory you want to go to. For example, let's go back to my C:\Users\username
directory. Say there is another directory called My Documents
along with the Desktop
, Downloads
, and Documents
. Then, to change from the username
directory to the My Documents
directory, you would do cd 'My Documents'
. You can use single quotes or double quotes. Next, going off of the cd
command is the cd ..
command. The cd ..
command goes up one current directory. Remember when I was at /c/users/username/desktop/
? I went back to /c/users/username
and navigated to /c/users/username/'My Documents'
. To do that, I could do cd ../'My Documents'
.
What does this do?
cd ..
goes up one directory. (From/c/users/username/Desktop/
to/c/users/username/
)/'My Documents'
is a continuation of thecd
command. It goes to theMy Documents
directory.
Second is ls
.
This is ls as in the lowercase L, not the number one
ls
means LiSt
ls
basically lists all the directories and files accessible in the current directory. Remember, earlier, how I knew that the directories available in the username
directory were Desktop
, Downloads
, Documents
, My Documents
, and some other stuff? Well, I figured that out using ls
. This can be really helpful when you don't have all the directories and files that are in a directory memorized. There are some other things you can add on to ls
such as ls -l
, ls -a
, and ls -t
. But for the sake of simplicity, I'm not going to go into detail about these commands.
Third is pwd
pwd
means Print Working Directory
pwd
is a really simple command. All it does is show the current working directory. For example, if I don't know what directory I am on right now, I can simply use pwd
to find that out.
Fourth is mkdir
mkdir
means MaKe a new DIRectory
Now is when you really start harnessing the power of the CLI
!
mkdir
is an awesome command that makes a new directory!
For example, let's say that I am in the root
directory right now. The root
directory is the base directory I see when I start up Terminal
or Bash
. For me, it is /c/users/username/
. On MacOSX
it is a little different, but don't worry. Say I want to make a new directory called tutorials
where I store all my tutorials in HTML
format. I do the following command: mkdir "Tutorials"
. Now, to see if I succeeded in creating that new directory, I can do: cd "Tutorials"
. Yay, you just created a new directory!
Fifth is rmdir
.
rmdir
means ReMove DIRectory
rmdir
removes the specified directory. Remember the directory Tutorials
we created a while ago? Well, we just realized we don't want this to be in the root
directory. We want it to be in our Desktop
. So, let's harness the power of rmdir
and do this!
- If you are still in the
Tutorials
directory, go up one directory - Remove the
Tutorials
directory. Check the hint below if you don't fully understand how to do this. - Go to the
Desktop
directory.
- In step 1, do:
cd ..
- Hint: For step two, do the following command:
rmdir "Tutorials"
- In step 2, do:
cd 'Desktop'
- Make a new directory called
"Tutorials"
inDesktop
.
Note:
rm -r
can also be used instead ofrmdir
.
Sixth is touch
touch
creates a new file
touch
creates a new file. To see this in action, first navigate to your Tutorials
directory in Terminal
. It should be in Desktop
. Next, do the following command: touch "index.html"
. Now you have a file! If you do ls
, you should be able to see that it lists index.html
. I will teach you how to put content in the file in a bit.
Seventh is rm
rm
means ReMove
rm
removes files.
So we removed directories, but we didn't remove files! We just decided that we don't want the HTML
file to be named index
. So, we are going to delete the file and make a new file with a different name! Make sure you are in the "Tutorials" directory. You can make sure of this using the pwd
command. Do ls
to see if the "index.html" file is still there. Now, do the following: rm "index.html"
. Make sure you get the name of the file exactly right, or else it won't work. Now do ls
. You shouldn't see anything. And you just learned how to delete a file in Terminal
! Now make a new HTML
file called LearningCLI.html
. Do ls
to make sure the file got created. You should only see LearningCLI.html
.
Eighth is echo
echo
basically puts words in files.
First navigate to the Tutorials
directory. Do ls
to make sure "LearningCLI.html"
echo
is one of the most handy commands in CLI
! To use echo
, do the following:echo "I'm learning the CLI!"" >> "LearningCLI.html"
Now use the command cat
(which we'll learn about later) to do this: cat "LearningCLI.html"
Ninth is cat
Used the simplest way,
cat
can show the contents inside a file
This is an on-your-own exercise. Go to Terminal, and navigate to a directory containing some .txt
file. Do: cat filename
. You should see the contents inside the file!
Tenth is grep
grep
searches for the specified file among all the available directories.grep
means “global regular expression print.” It searches files for lines that match a pattern and then returns the results. It is also case sensitive.
We do NOT need quotes when searching for a file or directory.
This is very confusing, so I will not go into detail about it. Feel free to Google it.
I have included a handy table that includes all the commands I taught and what they do.
Command | What It Does |
---|---|
cd directory | Navigates to the specified directory |
cd .. | Goes up one directory |
ls | Lists all directories and files accessible in the current directory. |
pwd | Shows the current working directory |
mkdir newdirectory | Makes a new directory |
rmdir unwanteddirectory | Removes the specified directory |
rm unwantedfile | Removes the specified file |
touch file.txt | Creates a new file in the current directory |
echo wordstoputinfile | Puts words in specified file using redirect symbol: >> |
cat file | Shows the contents inside a file |
grep | Searches for the specified lines inside a specified file. Format: grep linetosearch filetosearchin . |