The command line is one of those things that you either love or hate.
It can be really scary/confusing at first, but it is an essential tool for any developer. It’s a great feeling to be able to install a plugin through the command line, or run a node server on your local machine.
If you’re unsure of the command line, let me tell you that it’s worth your time to learn. When I successfully use the command line on one of my projects and it saves me a ton of time, I feel like this guy:
In this article we’ll look at some easy ways to get started with the command line, and some useful commands that we use.
To get your feet wet, let’s just run some simple commands. First, open up your command line tool. On a Mac, that would be in Applications->Utilities->Terminal. On Windows, it’s All Programs->Accessories->Command Prompt.
Command Line Basics
When you open the command line, you are basically sitting inside a directory somewhere on your file system, and your computer is waiting for you to tell it to do something. The first thing to understand is how to figure out where you are, and how to navigate to other places.
Where am I?
Type this command and press enter to see where in the filesystem you are.
pwd
You should see something like this: `/Users/scott/whatever` That is the path in the filesystem where you are. If you do something like create a file, that’s where it will be located.
List contents
Now let’s see what other files are in this directory.
ls
Depending on where you are, you will see a list of folders and files. If you see a name like “Applications,” that means it’s a folder. Files will have extensions, like .jpg.
Creating a folder
Let’s create a folder with the `mkdir` command.
mkdir test
That is going to create a folder called test. Next, let’s change directories so we are in that folder.
Changing Directories
cd test
Typing `cd` and then a file path will relocate you anywhere. In our case, we just type `test` since the folder is in the same place we are.
Create a file
Now that we are in our test folder, let’s create a file.
cat > sample.txt
That command creates a simple text file. You’ll notice that the command prompt takes you to a blank line, that’s where we can enter some text. Type some text and press ctrl D to finish creating the file.
Type some text into your file!
Now you can run `ls` to see the contents of the test folder, you should see sample.txt. To see the contents of our text file, you can use the `cat` command again:
cat sample.txt
That should print out the text you entered above.
Removing files and folders
The `rm` command removes files and folders. To remove our text file:
rm sample.txt
To remove our test folder, first let’s move outside of it. This command will take us up one directory, regardless of where we are.
cd ../
Now we can remove the test folder like this:
rm -rf test
(Removing a folder requires adding the -rf flag)
Diving into SSH
Now that you have a basic understanding of how the command line works, let’s explore a really useful tool, Secure Shell (SSH).
SSH could be described as FTP through the command line. You can upload/download files from a server, or enter the server and do anything from installing WordPress to deleting a file.
One of the biggest advantages of SSH is that it’s secure, and it’s really fast. For example, file transfers are much faster, as well as deleting folders. If you use a tool like WP-CLI, you can even install WordPress core or plugins in a single command.
Setup SSH
Setting up SSH is the hard part, after that it’s easy.
You need to generate a private and public ssh key on your computer, and then upload the public key to your web server. This allows you to login without entering a password every time.
To check if you have ssh keys already, you can run this command:
ls -al ~/.ssh
If you see something like id_rsa and id_rsa.pub, you don’t need to generate a new key. The first is your private key, the second is your public key. In my case, I logged into my Cpanel, and copied the contents of the public key in the SSH area following this tutorial.
You can see the contents of each file by using the `cat` command we learned earlier:
cat ~/.ssh/id_rsa.pub
You can then copy/paste that into the Cpanel SSH public key field.
If you need to generate a key, you can do so with the following command:
ssh-keygen -t rsa -b 4096 -C "Comment about your key"
You will be prompted to choose a directory, you can just press enter. You are then asked for a password, which you can add if desired.
Next you’ll want to add your key to your agent. First, ensure ssh-agent is enabled.
eval "$(ssh-agent -s)"
You should get a response like “Agent pid 59566.” Next add your key to your agent.
ssh-add ~/.ssh/id_rsa
Finally, you need to upload your key to your web server. Each host has different instructions on how to do this. You may have an SSH area in your Cpanel, or there may be somewhere else to add it. Please contact your host for instructions.
Tips: I ran into trouble because I didn’t do the last step of adding my key to my ssh-agent. You also want to make sure you have a private and public key, such as id_rsa and id_rsa.pub. You’ll also want to make sure your permissions are set correctly, which you can do by running `chmod 700 ~/.ssh && chmod 600 ~/.ssh/*`
The Fun Stuff: Using SSH on your Web Server!
If you’ve setup ssh correctly, you can login to your web server like this:
ssh user@mywebserver.com
(Obviously change to your username and your server address, which is usually your url)
If you’ve logged in successfully, you are essentially using your terminal on another machine. That means you can run all the same commands we did above (such as creating a file or folder) and more. To see where you are, type
pwd
You can navigate around using `cd path/to/folder/` like we did above.
The fun really starts when you install WP-CLI. While logged into your server, you can install WP-CLI there following their instructions. With it successfully installed, we can download WordPress core to the current directory:
wp core download
If you already have WordPress installed, navigate to the core directory (cd path/to/wp). To install a plugin, simply run:
wp plugin install apppresser
wp plugin activate apppresser
Much faster than clicking around in the admin, eh?
I would highly recommend checking out more of the fun on the wp-cli website.
To end your ssh session:
exit
Transferring Files
One of the most useful command line tools is uploading and downloading files quickly. To do that, we can use the `scp` (secure copy) command.
For example, here’s how to upload a plugin that you have locally to your web server.
scp /path/to/plugin.zip username@hostname.com:/path/wp/wp-content/plugins/plugin.zip
You can use the same login you setup for SSH above. If you don’t know the remote path, you can omit the remote path like this:
scp /path/to/plugin.zip username@hostname.com:plugin.zip
Now we need to unzip the plugin and move it to the plugins folder. To do that, let’s login with ssh.
ssh username@hostname.com
If you omitted the remote path, we move the zip file to the plugins folder.
mv plugin.zip whatever/wp-content/plugins/
Now we need to move ourselves to the plugins folder.
cd whatever/wp-content/plugins/
Unzip the plugin.
unzip plugin.zip
To confirm everything is as it should be, let’s list the directory contents.
ls
If you see plugin and plugin.zip, all is well.
That’s it, now delete the zip file.
rm plugin.zip
Now you can login to your admin and activate the plugin, or use wp-cli to activate it. Finally, let’s kill our ssh session.
exit
Pro tip: Start typing a path in the terminal and press tab to auto-complete it. Very handy!
It’s not as scary as it looks!
The command line is not that scary once you get used to it. The best thing to do is just dip your toe in by running some simple commands, and getting those integrated into your workflow. You can save lots of time, I highly recommend taking the time to learn it.
What are your favorite commands? Let me know in the comments.