Cozy Up with the Command Line on a PC


This week we are diving back into Skillcrush 105 and getting to know the command line a little bit more. Your workspace is ready to go so it is time to start learning the basics of the command line. Keep in mind some of these commands will differ depending on what computer you are using and that’s ok.

I’ll be talking about the PC commands in this post, but I will mention some of the commands Mac users can use. This post introduces directories and files. The most important thing Skillcrush students learned about in this lesson is how they work, how to create them, and how to switch between two different directories.



A Note on Folders and Directories

Folders are great tools for both in real life and on the web. Developers love folders because they provide a good structure for their content. For example, when you build a portfolio site you have a folder for your portfolio project to live in and have everything you need for your portfolio site inside from files to even more folders.

You might hear developers talk a lot about directories. Skillcrush says that is a fancy way of saying a folder, but they are where developers store files and other directories. You can think of them just like a filing cabinet that has multiple file folders inside.

You don’t need to memorize the commands featured in this lesson. Skillcrush does provide cheat sheets that their students can use to help them when working on the command line. You can use the Git resources I mentioned during my first post in this series.



Pwd and ls

The first two commands you will learn about are pwd and ls. Developers use these a lot to double-check what directory they are in and what is in that directory. One of the worst things you can do is create files and folders in the wrong directory.

Many developers remember these commands first since they use them often in the command line and even use them before they even get started.

pwd
Enter fullscreen mode

Exit fullscreen mode

The pwd command means print working directory. This will show you the current directory that you are in. Skillcrush points out that you could use your computer’s GUI to do this, but it takes more time, making the command line much more efficient.

When you use the pwd command, you will see where you a located to the directory you are in. Think of it as a map inside a big office building or hospital that had those red circle saying “You are here”. That’s what the pwd command does for developers in the command line.



The ls command

You’ll also be using the ls command. The ls command tells the computer you want all the files inside the current directory you are in.

ls
Enter fullscreen mode

Exit fullscreen mode

You can also type “ls-la” in the command line, and it will list every file, including any that are hiding. This will come in handy if you want to be very cautious about where you are. However, the ls command alone will tell you all the files you need to know.

ls -la
ls -a
Enter fullscreen mode

Exit fullscreen mode



How to create a directory

You are ready to create your first directory. To create a new directory or folder, you will type the mkdir command into the command line followed by a space then the name of the directory you want.

mkdir seasons
Enter fullscreen mode

Exit fullscreen mode

Make sure the name is lowercase to keep with best practices. Need to use more than one word? Use a dash or an underscore between words like new_score. After you run the mkdir command, Skillcrush recommends students run the ls command to see if your directory has been created.



The cd command

You will need to switch directories as you work in the command line, so you will need the cd command. The cd means change directory. This command moves you from directory to directory.

Just type cd then put a space before the name of the directory you want to switch to. When you press enter, the computer will take you straight to the directory you put in. If you aren’t sure, look at your command line.

cd seasons
Enter fullscreen mode

Exit fullscreen mode

The next line will show you what directory you are in. Be careful! Skillcrush warns students that computers follow file paths, so make sure you double-check what paths you specify.

Skillcrush tells students to be specific about the path from where they are to the one they want to go. Want to go back to a previous director? You just need to type cd then two dots before you press enter. This will take you back to the previous directory. Just make sure you use the ls command to check.



The touch command

A brand-new directory needs a file to go inside it. To add a file inside a directory on a PC computer, you will use the touch command. The touch command creates files.

Type touch, then the file name you want then end with the extension. A file extension is key to creating a file because it tells the computer what type of file to create.

touch index.html
Enter fullscreen mode

Exit fullscreen mode

When you press Enter, a blank file will be created.

Don’t forget to use the other commands we’ve already discussed, such as ls, pwd, and cd to help you check if the file is in the right directory. For example, a developer trying to create an HTML file can use touch index.html to create this file in the command line. When the developer presses Enter, the index.html file will be created inside the directory that the developer is in.



The explorer/finder command

The explorer (Finder for Mac users) command allows developers to double-check their directory and files. Regardless of what command you use here, Skillcrush says that these commands use GUIs to find and access certain file systems. PC users just need to type explorer and then put a dot (“explorer .”) in the command line.

explorer .
Enter fullscreen mode

Exit fullscreen mode

This will let you see the current directory and files. PC users can also use the start dot (“start .”) to open their text editor and begin coding. Mac users need to type open dot (“open .”) to use Finder to see their current directory and files.

start . 
Enter fullscreen mode

Exit fullscreen mode



How to see what is inside a file?

Developers will often check to see what is inside of a file. You can do this on the command line using the cat command. Just type cat then the file name. After you press enter, the next line will show you everything inside that file.

cat flowers.html
Enter fullscreen mode

Exit fullscreen mode



How to remove directories

Directories change over time so you will be removing some of them from your computer while you are working on the command line. You can remove directories using the rm command. Type “rm -r” with the directory name.

rm -r img
Enter fullscreen mode

Exit fullscreen mode

This will permanently delete the directory and all the files inside of it. Be careful using this command. When you delete a directory, it is gone for good and you can’t redo it on the command line.



What should I do if I get a “command not found” message?

Always start by double-checking your command for any typos. Most of the time, a command not found is the result of a misspelling. You will also want to make sure you are using the right command for the right type of computer.

Mac users will get an error message if they use a PC command for a certain action. Next, retype your command. When it runs, it should be fixed.



Conclusion

You can use Git Bash to play around with some of the commands you learned. The next lesson is all about repos. You will learn how to create a repo and get even more commands to use to help you navigate the command line and make a repo.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *