The University of Massachusetts Amherst
Categories
Operating System

Navigating Mac OS X Through the Command Line (Part 2)

 

Part I is available here.
Onward!

So last time we learned a few basic commands: ls, cd, and open.  That will get us through about 75% of what we would normally use the Finder for, but now we are going to address the other 50% (no, those percentages are not a typo).  In this article, I will address the following tasks:

-Copying and Moving

-Performing actions as the Super-User

-And a few little other things you may find interesting

Without wasting too much text with witty banter, I am going to just get right into it. However, I need to address one quick thing about the names of files and directories.  In part 1, we traveled through directories one at a time but in order to make things more quick and easy, we will have to do multiple directories at once.  How do we do this?

Remember in part 1 when I said the / symbol would become an important part of the directory name?  Well this is how it works: cd /directory1/directory2/directoryn.  If you have three directories with these same names on your machine and if directory2 is within directory1 and directoryn is within directory2, then you will have changed from your current directory directly to directoryn; bypassing directory1 and directory2 in the process.  Try it out with some of the directories on your machine.  Let’s say you wish to change directory from your root directory from your desktop; simply type in your cd command followed by /users/YOUR_USER/Desktop substituting the name of your user for YOUR_USER.  You should have just changed directory from the root directory to your desktop!

Alright!  Now that we can represent directories in a more intricate way, we can explore the more complex tasks that the command line is capable of!

 

Copying and Moving

If I could take a guess at the number one action people perform in the Finder, I would guess copying and moving files and directories.  Unless you know the position of every particle in the universe and can predict every event in the future, you’re probably going to need to move things on your computer.  You accidentally save a file from MATLAB to your Downloads directory and want to move it before you forget it’s there.  You just 100% legally downloaded the latest high-flying action flick and you want to move it from your Downloads directory to your Movies directory.  Additionally, you may want to create a new copy of your Econ paper (which you may or may not have left until the last minute) and save it to a thumb drive so you can work on it from another machine (#LearningCommonsBestCommons).

These tasks all involve moving files (or entire directories) from one directory to another.  The last task involves both duplicating a file and moving the newly created duplicate to a new directory.  How do we do this in the Finder?  We drag the file from one directory to another.  How do we do this in the Terminal?

  1. To move files we use mv
  2. To copy files we use cp 

Here is the basic implementation of these two functions: mv file location and cp file location .  In practice, however, things look just a bit different.  I will give you an example to help show the syntax in action and I will try to clearly explain the presence of all text in the command.  Let’s say we have a file in our root directory call GoUMass.txt and we can to move it to our documents folder so we can open it later in TextEdit or Vim and write about how awesome UMass is.  To move it in terminal we would type:

mv /GoUMass.txt /users/myuser/Documents/

After typing this in, if we ls /users/myuser/Documents, we would see GoUMass.txt in the contents of the Documents directory.  Need another example?  Let’s say we get cold feet and want to move it back to the root?  Here’s what we would type:

my /users/mysuser/Documents/GoUMass.txt /

So now that we know how to move, how do we copy?  Well, luckily, the syntax is exactly the same for cp as it is for mv.  Let’s say instead of moving GoUMass.txt from the root directory to documents, we want to copy it. Here is what we would type:

cp /GoUMass.txt /users/myuser/Documents/

Nice and simple for these ones; the syntax is the same.

One issue though: if you try to move an entire directory, then you will get a nasty error message.  To make this not the case, we employ the recursive option of mv and cp.  How do we do this?  After the command (same as it is written above) we provide one more space after the destination name and write -r.  This -r tells the computer to go through the mv or cp process as many times as it needs to get your directory from point A to point B.  Anyone who has taken a data structures course may recognize the word “recursion” and be able to see why it is implemented here.

You may also get a different nasty message here about permissions, the we will deal with in this next section:

…oh, and one last thing: if you want to delete a file, the command is rm.

 

Performing Actions as the Super-User

Permissions are a nasty thing in the computer world and can really hold you back.  The Finder will often deal with it by prompting you to enter in you administrator password.  Mac users who have configured a connection to Eduroam on their own (which I hope most of you have) will have had to do this an annoying number of times.

The Terminal will not pop up and ask you for your password, you have to tell it when you are about to do something which requires special permissions.  How do you do this?  You use a command called sudo.  sudo stands for super-user do and will allow you to do nearly anything that can be done (provided you are acting as the right super-user).  This means that those clever things that the folks at Apple Computers put in the Finder to prevent you from deleting things, like your entire hard drive, are not there.  For this reason, you can mess up some really important things if you use sudo, so I caution you.

So how does sudo work syntactically?  There are two things you can do with it: you can preface a command with sudo, or you can use sudo su to enter the super-user mode.

Prefacing a command is simple, you type sudo before whatever it is that you wanted to do.  For instance, let’s say our GoUMass.txt needed administrator privileges to move (unlikely but possible).  We would type in our move command the same as before but with one extra bit:

sudo mv /GoUMass.txt /users/myuser/Documents/

After you type this is, your computer will prompt you for your password and you will enter it and press return.  Do not be alarmed that nothing shows up in the command line when you press a key, that’s normal.  If you enter the correct password, then your computer will do the thing you asked it to after the sudo command; in this case, it’s mv.

You can also invoke actions as the super user using sudo su.  The su command will lock in the sudo privileges.  The syntax for this is as follow:

sudo su

That’s it!  After this, you will be prompted for your administrator password and then you are good to go.  The collection of cryptic text prefacing your command will change after you enter sudo su; this is normal and means you have done things correctly.  In this mode you can do anything you would have needed the sudo command for without the sudo command; sudo mv becomes just mv.

 

And a few little other things you may find interesting

The command line can be used for a wealth of other tasks.  One task I find myself using the command line for is uploading and downloading.  For this, I use two different apps called ftp and sftp.  Both do the work of allowing the user to view the contents of a remote server and upload and download to and from the server.  Sftp offers an encrypted channel when accessing the server (the ‘s’ stands for secure) and has the following syntactical structure to its command:

sftp username@server.address

If you server requires as a password, then you will be prompted for one.  Once you’re logged in to your server then you can use commands like get, mget, put, and mput to download and upload respectively.  It will look something like:

mget /PathToFileRemote/filename.file

mput /PathToFileLocal/filename.file

 

Wondering if your internet is working?  Try using the ‘ping’ command!  Pick a website or server you would link to ping and ping it!  I often use a reliable site like Google.com.  Your command should look something like:

ping www.google.com

You should start getting a bunch of cryptic-looking information about ping time and packets.  This can be useful if you are playing an intense game of League of Legends and want to know you ping time (because you are totally lagging).  The main use I find for the ping command is to see if the wireless network I’m connected to is connected to the internet.  Though rare, it is possible to have full wifi reception and still not be connected to the internet; ping can test for this.

 

Feel like you can do anything in the Terminal that you could do in the Finder?  Want to add the ability to quit the Finder?  Here’s what you type:

defaults write com.apple.finder QuitMenuItem -boolean true

Follow this command with the following:

killall Finder

Your machine will glitch-out for a second but when things come back online, you will have a cool new ability in the Finder: command-Q will actually quit the Finder.  Here’s what you loose: Finder windows and your desktop.  This is the fabled way to improve your computer’s performance through your knowledge of the command line.  Finder uses more of your computer’s resources than the Terminal does so substituting one for the other can help if your computer gets hot often or runs slow.

Remark: if your computer is running outrageously slow, try running an antivirus scan, like Malwarebytes, or checking to make sure your drive isn’t failing.

 

In Conclusion

The command line, once you get a grip on some of the less-than-intuitive syntax, is an invaluable tool for using any computer system.  For everyday tasks, the command line can be faster and for slightly beefier tasks, the command line can be the only option.

And for those still in disbelief, I implore you to try installing a package manager, like Homebrew, and installing some applications to your command line.  If you can think of it, it’s probably out there.  My personal favorite is an application called ‘Links’ which is a text-based internet browser for the Terminal.

The command line, on any system, is one of the most important tools for navigation and operation.  Even for those who do not want to become one with the compute, the command line can really come in handy sometimes.