Command Line Basics: A Short & Sweet Introduction
*Note: The tutorial assumes you are on a Mac or Unix operating system. The purpose of the command line is the same across all operating systems – navigating and manipulating files and folders that live on your computer. The only difference is the syntax for the commands will differ on Windows.
There is a workaround though 😉. If you are on Windows and want to follow this exact tutorial (which uses commands heavily similar to Linux), I recommend enabling Windows Subsystem for Linux.
My aim with this post is to (hopefully) help anyone overcome a fear of the command line by proving a short, practical introduction to navigating the command line by creating folders and files.
This is not an in-depth, comprehensive guide to all things command line. This does not contain an exhaustive list of every command ever created nor do I intend to. Even the most experienced professionals in tech do not seek to memorize everything, that’s what Google is for😛 . Will you unlock the power level of a Command Line legend? No, not quite. However, this tutorial will get you up and running so you can start navigating the command line with ease.
I also made a cheatsheet summarizing the basic commands and a visual of how the tree-like file structure so you can download it here! Bookmark and refer to it as you continue your journey in tech <3
- Understand what the terms
- How to navigate the filesystem
- Create a Project folder, file, write in the file and view it…all in the command line!
What is the Command Line (CLI)?
The command prompt is an interface that enables you to interact with your operating system and gives you the power to navigate and manipulate the files and folders (aka
directories) that live on your computer. You’re probably familiar with your
Finder (on Mac) or where you are provided with a
GUI (graphical user interface), which is a visual representation of all of the folders and files on your operating system. Check out the visual guide below to see how the filesystem looks in a GUI vs the terminal. They’re all the same.
In the example I am listing all of the content (directories and files) that are located in the parent directory
Desktop. The terminal to the right is listing the exact directories and files you see, it’s just all displayed in text format.
The command line is a text interface for the computer’s OS. You can use it to traverse and edit your computer’s filesystem. Throught the command line you can create new files, edit the contents of those files, delete and more. You will learn how to navigate through a computer’s filesystem solely through command line (view contents of your filesystem, move dir, make files and dir);
A filesystem organizes a computer’s files and directories into a tree structure; it starts with the root directory; each parent directory can contain more child directories and files
How to Access The Command Line
- Click the 🔍 finder icon in the top right corner
terminaland hit enter
CTRL + T on many distros will launch the terminal (Shoutout to @rolandixor for the tip!)
Let’s Start Practicing!
The Objectives of Our Terminal Exercise:
- Create a directory (folder) inside of the
- Create a file and write text
- Read the contents of our file
There are 2 essential things you must do to get comfortable with anything you are learning – practice and be consistent.
- Open your terminal
cd ~Navigate to the root directory
pwdPrint the working directory, aka where you are currently located in the filesystem in the terminal. On Mac It should say something like
/Users/Username, in my case, username is Taelur. On Linux, the root directory is in
cd DesktopChange directories into Desktop folder.
mkdir hello_worldCreate a directory in Desktop called,
hello_worldor whatever you wish to name our project folder!
cd hello_worldChange into the hello_world directory. It’s kinda empty right now. Let’s add some friends!
touch goals.txtCreate a file called goals.txt. The
.txtis a file extension indicating we are creating a text file.
lsList all files in the
hello_worlddirectory. You should see the
goals.txtfile we just created.
echo "Say Anything" > goals.txtUse the
echocommand to write anything you want to the file within the quotation marks. For example, write a summary of your learning goals for 2021
cat goals.txtOutput the text that is written in the
goals.txtfile and read it in the terminal!
- (Optional) Run
historyto see a list of the commands you just ran.
If you were able to successfully print text to the terminal, congratulations, you just learned all of the commands I (and countless others) use on a daily basis! You are well on your way to unlocking more powers! I’m very proud of you ^_^
More Commands To Learn Right Now
$pwd – outputs the name of the current working directory
$ls – Lists all the contents of a
directory (folder); can contain files and folders
$cd – Switches you into a different directory
$mkdir – Create a directory
$touch – Creates a new file
$mv – Moves or renames a file
tab – Autocompletes a file or folder name; only if you are in the correct directory that contains that file or folder
$ls -t orders files and directories by the time they were last modified
$ls -a – lists all contents of a directory, including hidden files and directories
ls -l displays a list vertically with permissions, date modified and file name
clear – Clears the terminal; does not delete any folders or files, don’t worry!
history – Lists all previous commands you’ve made
help <command> Provides documentation on the command
$cd .. – Goes up one directory!
If you want to learn more about the command line, check out these free resources below:
A Few Words of Advice To Beginners
The key to becoming more comfortable with the command line is practice and repetition. If you end up not feeling the command line and preferring the GUI method of navigating, editing and creating files, that’s fine. The more you start practicing using the terminal, the more confident you’ll become.
The way I’ve been able to use the command line comfortably on a daily basis is because of practice. I learned on my first job. I didn’t even know how to use Git, I ended up learning that. The
history command was essential in me remembering.
I was reminded during one of my Twitch streams that everything is hard when you’re first learning it. I have been a programmer for a few years and I still struggle. The key is repetition, practice and googling your way to comfortability with these sorts of things. You’re not alone in the struggle of learning all things tech.
Thank You For Reading!
I hope you enjoyed this introduction to the command line. Feel free to leave a comment below with topics you’d like to see me discuss next and don’t forget to bookmark my cheatsheet! Happy Command-lining ^_^
If you enjoy my content, please consider buying me a coffee on Ko-Fi. For daily updates, check out my Twitter! I stream every Tuesday, Thursday, and Saturday on Twitch where I livestream myself learning programming languages like Python and building projects. You’re more than welcome to join if you’d like to interact and chill with me! Have a great day.