Email Subscription Form

Saturday, January 11, 2020

The Command Line, Demystified- Part II

In last week's blog post, we started taking a look at the command line, and how it's possible to navigate through your computer's file system using some very simple commands.  This week we'll learn how to use the command line to create and remove folders and files.  We'll be building upon the knowledge in last week's post, so if you haven't read that, I recommend starting there.




Let's start by creating a new folder.  Open up the command window, and enter mkdir MyNewFolder.  You won't get any kind of response, just a new command prompt.  But if you type ls (on Mac) or dir (on Windows), you'll now see MyNewFolder listed in the contents of your home directory.  

To navigate to your new directory, type cd MyNewFolder.  Your command prompt will now look like MyNewFolder$ on Mac, or MyNewFolder> on Windows.  If you type ls or dir now, you'll get nothing in response, because your folder is empty.

Let's put something in that new folder.  If you are using a Mac, type nano MyNewFile.  A text editor will open up in the command window.  If you are using Windows, type notepad NewFile.txt.  Notepad will open up in a separate window.

Type This is my new file in the text editor (Mac) or in Notepad (Windows).  If you are in Windows, just save and close the Notepad file.  If you are in Mac, click Control-X,  type Y when asked if you want save the file, then click the Return key to save with the file name you had specified.

If you are in Windows, return to the command line; Mac users should already be there and the text editor should have closed.  Your working directory should still be MyNewFolder.  Now when you type ls (Mac) or dir (Windows), you should get this response:  MyNewFile (Mac) or MyNewFile.txt (Windows).  You have successfully created a new file from the command line.

We can now read the contents of this file from the command line.  If you are in Mac, type cat MyNewFile.  If you are in Windows, type type MyNewFile.txt.  You should see This is my new file as the response.

Now let's learn how to delete a file.  Simply type rm MyNewFile if you are in Mac, or del MyNewFile.txt if you are in Windows.  If you've deleted correctly, an ls or dir command will now give you an empty result.

Finally, let's delete the folder we created.  You can't have the folder you want to delete as your working directory, so we need to move one level up by typing cd ..  .  Now you should be in your home directory.  If you are in Mac, type rm -r MyNewFolder.  If you are in Windows, type rmdir MyNewFolder.  You won't get any response from the command line, but if you do ls or dir, you'll see that the folder has been deleted.

Now you know how to create and delete files and folders from the command line.  I'll close by adding two bonus commands- one for Mac and one for Windows.

For Mac users:  the term sudo (which stands for superuser do) allows you to run a command as an administrator.  There are some times where you may need to do an installation or edit which requires administrator access.  By putting sudo before the command, you can run the command as the system admin.  For example, if you typed sudo rm -r MyNewFolder, you'd be removing the folder as the system admin.  Think carefully before you use this command, and make sure you know what you are doing.  There are many commands that require a superuser to execute them because they are dangerous.  You don't want to delete your entire filesystem, for example!

For Windows users:  a handy command is explorer.  Typing this in your command window will bring up the File Explorer.  This is useful when you want to switch from navigating the folder structure in the command line to navigating in the Explorer window.  For example, if you knew that a folder called MyPictures had images in it, you might want to open up the Explorer to take a look at the thumbnails of those images.

I hope that these two blog posts have gotten you more comfortable using the command line.  Have fun using your newly-learned skills!

4 comments:

  1. Great explanation! I did not know about the "explorer" command. Thank you

    ReplyDelete
    Replies
    1. I didn't know either, until I did research for this post! I'm glad you enjoyed it. :-)

      Delete
    2. You can also use the Windows Key shortcut which is even quicker.

      Just press and hold the Windows Key + the letter E on your keyboard at the same time.

      Delete
    3. Thanks, Bo! That's even better! :-)

      Delete

Book Review: Perfect Software and Other Illusions About Testing

"Perfect Software and Other Illusions About Testing", by Gerald Weinberg, is the best book on testing I have ever read.  It is a m...