a compass resting on top of a world map

Dark Theme | Category: Code, Educational

Bash for Beginners: Navigating in the Terminal

A note before we begin: If you’re brand new to shell scripting and afraid to make a mistake you can’t resolve on your (likely very expensive) everyday machine, you can always learn on a Raspberry Pi – they’re comparatively cheap, and if you find yourself in a mess you can’t untangle, you can simply wipe the SD card and start fresh.

Actual Bash commands that you can copy and paste into your terminal will appear throughout this post like this.

What is Bash?

In this case, it’s a programming language used for writing shell scripts. It can also refer to the Bourne shell (or Bourne Again SHell, if you’re into acronyms). The language was written in C by Brian Fox and released in 1989 (like yours truly). In terms that actually matter to you in the moment, though, Bash is a language you can use to communicate with your computer – and other devices on the network! – to accomplish tasks. What kind of tasks, you may ask? Why, anything you can imagine.

Navigating in the Terminal

When you first open the Terminal you’ll automatically be in your Home folder. Wherever you are in the depths of your file structure, you can always type cd and press enter to return to your home folder.

To navigate into a folder, simply type cd foldername, where “foldername” is the name of the folder you want to dive into, and press enter. To move up a folder level, type cd .. and press enter.

terminal window demonstrating cd command with Desktop directory

But what if you don’t know off the top of your head which subdirectories are inside of a folder? Simply type ls and press enter to output a list of the folders and files that are inside the current directory you’re in.

terminal window demonstrating ls command showing a list of subdirectories in the Home folder

When you try this for yourself, you may notice that you have hidden files or folders that aren’t showing up in this list. If you want to see those as well, use the command ls -a. The “-a” part of the command is called a flag. Many Bash commands have flags that let you get more specific in the way you use them, the data you get back, etc., and you can even combine flags! For example, ls -l will give you lots of great information about the files and folders in your current directory, such as the permissions associated with them, their owner, etc. However, it doesn’t take hidden files and folders into consideration by default. Using ls -la will give you all that good data including hidden files and folders.

Finally, if you get lost and forget which directory you’re in, you can type pwd and press enter to output the file path to your present working directory.

Terminal window showing example of the pwd command output: /Users/Mainframe/code/practice/javascript

When you first start navigating in your Terminal, you’ll be doing a lot of cd-ing and ls-ing to get your bearings. Eventually, when you have a good sense of your folder structure and you’re comfortable bopping around from place to place in your terminal, you can enter longer paths to jump multiple levels at a time. Take the file path above, for example. I have a folder in my Home directory called “code”, and inside of that folder is another folder called “practice”, and in there I have a folder called “javascript” where I keep all my practice JS files. I can type cd code/practice/javascript to pop right in there, rather than cd-ing one folder level at a time.

Bash Commands in this Post:

cd – return to your Home directory
cd foldername – enter a directory
cd .. – go up a directory level
ls – list all of the files and folders inside your current directory, not including hidden files and folders
ls -a – list all of the files and folders inside your current directory, including hidden files and folders
ls -l – list detailed information about the files and folders inside your current directory, not including hidden files and folders
ls -la – list detailed information about the files and folders inside your current directory, including hidden files and folders
pwd – output the file path to your present working directory, aka the folder you’re in when you run the command

</ XOXO>

[Photo credit: Denise Jans via Unsplash]

Back to the Blog