Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 1, 2020 07:54 pm GMT

Command Line Basics: How to find your way around your computer

This post was originally published on my blog. You can also follow me on Twitter @AnnaJMcDougall.

There is no debate: using a command-line interface (CLI) is more efficient than the graphic user interface (GUI) with which were all brought up. The problem is, its not as intuitive and its not what we all grew up with. Here Ill cover the absolute basics for finding your way around your computer using the command line using Bash, which is the standard shell used by Linux. If you dont understand what all these terms mean, dont sweat it. The important thing is that youve booted up Linux, opened the Terminal (shortcut Ctrl-Alt-T) and are ready to find and make files.

However, before we begin it is crucial that you understand that you see a $ in the code examples below, that means that you enter it into the command line.

By the end of this post, youll not only be able to use the following commands, but youll know what they mean (gasp!) and the variations you can use to help you save some time:

$pwd$cd$ls

Where am I? Locating your current folder using the command line using $pwd

When you first open the Terminal, by default you will see some sort of identifying information, followed by a tilde (~) and then a dollar sign ($).

The dollar sign is an indicator that you are in the command line, and is commonly used in code snippets (like on this post) to show you that the code is to be entered into the Terminal.

The tilde is also important, as it indicates you are in your Home Directory. A quick editors note here: Im purposefully going to use capital letters when referring to the Home Directory because of a fun quirk: the Home Directory is not the same as the folder/directory named home. You will see why this becomes important later, so for now please always make this association:

Home Directory = ~$

~$ = Home Directory

So where is your home directory? Try typing this:

$pwd

Remember, you dont need to type the dollar sign ($) because it is only ever used as an indicator that you are in the Bash terminal. So just type pwd and enter.

Congratulations! You just executed your first Terminal command, and you can now see exactly where in your computer you are working from. Most likely, you see something like one of these:

  • /Users/yourUserName
  • /home/yourUserName
  • /user/yourUserName

The second example in the results above shows you why Ill always use capitals to describe the Home Directory: because home is not always Home: in this case, your Home Directory is actually a folder within the home directory! Got a headache yet? Great! Youre on your way to becoming a developer!

The command $pwd stands for present working directory, and it tells you where you are within your folders at any given time. If youre used to using the Windows GUI, youre actually already used to seeing something like this in the Explorer bar:

A screenshot of a Windows folder navigation bar

Root Directory vs Home Directory

You may have noticed that when you typed pwd above, the location of your Home Directory doesnt begin with a word but with a slash. That slash represents your Root Directory: if you picture your entire computer and all its files as one big tree, the root is the singular location from which it all stems. There is only one root location, and it is always denoted with a /.

Just as ~$ always indicates your Home Directory, /$ always indicates your Root Directory.

Now its time to learn another command, $cd, which allows us to move from one folder/directory into another. Try this

$ cd /

Dont forget the space, and remember not to type the $. Now, try going back to our trusty ol pal, $pwd, so we can find out where we are!

Hang on, its only showing a /? How can that be? Well, as I clarified above, the / indicates that we are in our root directory, and our root directory is as far back/down the tree as we can go. When we typed $cd / we were combining two ideas: $cd is the command, essentially saying Go to and then the / is our Root Directory. So what we have told the computer is Go to the Root Directory, please (side note: as far as Im aware, there is no analogous term to please in the Bash Terminal, but Im doing a polite translation).

So now were in our Root Directory, and theres nowhere deeper for us to explore in our computer. Thats good to know, but really we want to be doing the majority of our work in our Home Directory. So lets get back there! Here you have two options:

$cd ~

This is probably what youd expect, right? The ~ represents your Home Directory, so if we use it with cd then we can navigate home. AND IT WORKS! Hoorah!

Now, not to burst your bubble, but since the Home Directory is kinda a big deal, the developers of Bash have made it even easier to get back there. All you need to type is:

$cd

Yep, thats it. Just the command, running without an argument. $cd will always take you home, no matter where you are or how far youve roamed. If you need a way to remember it, just remember that if youre at a party and someone tells you to Leave!, you will probably just go home. If someone says Leave with Andrew and his friends so you can make it to the next party! then youll go there instead. So the $cd command with arguments takes you somewhere in your files and folders, and the $cd command without arguments just takes you to your Home Directory.

Does that mean $cd can take me to any of my folders, anywhere on my computer?

Why yes, dear reader, yes it does! There are two ways of navigating to another folder: an absolute path or a relative path. Lets look at both using the following folder structure as an example:

Root Directory|__user    |__Anna (~)       |__Music       |__Downloads       |__Documents          |__CameraUploads          |__ImportantForWork          |__InternetFiles

As you can see, my Root Directory is the beginning of all my folders, and from there branches off the user directory, then the folder with my name (which is my Home Directory), then all the files I use day-to-day. Now lets say I really want to find an HTML file which I know is important for work. As you may have guessed, its in my ImportantForWork folder.

Absolute Paths: navigating to a folder based on its relationship to the Root Directory

An Absolute Path describes where a folder or file is, relative to the Root directory.

We want to get to ImportantForWork which is in Documents, which is in Anna (my Home Directory), which is in user, which is in my Root Directory. In Bash, this looks like the following:

/user/Anna/Documents/ImportantForWork

Please note that these commands are all case-sensitive.

Relative Paths: navigating to a folder based on where you are now

The fun thing about developers is that theyve learned by now that laziness can often mean efficiency: similarly, although the above will always work (assuming the folder exists and you have the location correct), it is a bit of a bother to type that every time you want to move from one place to another, especially when the folders are quite close by. For example, if youve just opened the Terminal, youre already half way to the ImportantForWork folder, because youre in Anna folder (which is our Home Directory or ~). So why would you bother instructing the computer to go back to where you already are? Aint nobody got time for that!

Instead, we can tell the computer to take us to a location nestled within our current one.

~$cd Documents

As you can see, the tilde tells us were in our Home Directory (Anna), and from there we can simply jump into the Documents folder. Now we just need to go one step further, into our final destination folder, and then well use $pwd to confirm that we made it.

$ cd ImportantForWork$ pwd

The Terminal should now show us /user/Anna/Documents/ImportantForWork. Huzzah! We made it!

Hold on one second, Anna, I hear you say indignantly. That wasnt particularly efficient!

Why shucks, dear reader, no it wasnt. Lets make it better. First well use $cd alone, to bring us back to our Home Directory (Anna).

$ cd Documents/ImportantForWork

BOOM! Easy. Now were where we needed to be.

Want to make it even easier? Use your tab button to act as an auto-complete tool.

As soon as you have enough characters typed to indicate a unique directory name, hit <tab> and youll see it pop up. In this case, something like

$cd Doc <tab> / Im <tab>

Navigating this way, weve managed to get to our folder using just 11 keystrokes rather than the 40(!) we used when navigating using the absolute path.

Simply put: if you know where youre going relative to your current location, youre better off using the relative path in combination with the tab key.

Show me more ways to get around!

OK since you insist. Lets have a look at our file structure again:

Root Directory|__user    |__Anna (~)       |__Music       |__Downloads       |__Documents          |__CameraUploads          |__ImportantForWork          |__InternetFiles

As you may have noticed, you can use $cd with a folder name to access what we call a child directory, that is, a directory which stems directly off the one youre in now. In the above example, CameraUploads, ImportantForWork, and InternetFiles are all child directories of Documents. Logically it therefore follows that we can refer to Documents as their parent directory.

Moving to a Parent Directory

Any time youre in a folder, you can move to the parent directory using:

$cd ..

The .. tells the computer to move up a step. These can also be stacked to access a grandparent directory:

$cd ../..

Moving to a directory within the parent directory

Lets say we are already in the ImportantForWork folder, and we now want to check out a photo of our nephew in the CameraUploads folder. How do we get there? If you logic it out, its quite simple: first we move to the parent directory, then we $cd into the CameraUploads folder.

$cd ..$cd CameraUploads

This will work, but as usual we can save time by stacking the two commands:

$cd ../CameraUploads

Tada! Youve navigated up a level and down a level in one command.

Last but not least Seeing whats inside using $ls!

The last command is pretty straightforward, but next to $cd it is one of the commands you will type the most. Navigate to your Home Directory using$cd or $cd ~ and then type the following:

$ ls

The command stands for list and when used without an argument (as above) it shows you what files and folder are within the present working directory (i.e. where you are).

If your system is relatively new, you might not have any hidden files, but later on especially if you plan on using Git you will definitely have some! `$ls wont show those to you automatically, so to see them, type:


$ls -a

This -a is called a flag and it tells the computer to something special with the command its given. In this case, it stands for all, so were telling the computer List the files yes, all of them please, even the hidden ones.

As you may have guessed from my wording above, you can also use $ls with an argument, meaning you tell it which folder to list without actually having to go into it.

For example, if we want to see the files in the Documents folder, we can either do this:


~$cd Documents
$ls

or we can simply type:


~$ls Documents

This will show us everything in the Documents folder, without actually taking us outside of our Home Directory.

Thats it! Youre now a genius!

OK maybe not, but at least you can now move between folders using $cd, find out where you are using $pwd, and see whats inside a folder using $ls, plus you know how to cut some corners by using relative paths, parent directories, and the <tab> button, saving you some time in an industry where every keystroke can be valuable.


Original Link: https://dev.to/annajmcdougall/command-line-basics-how-to-find-your-way-around-your-computer-30h4

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To