Skip to content

10-ish Unix CLI File Commands Every WordPress Developer Should Know

Two weeks ago, we published a conceptual primer on the command line for WordPress developers who’ve never used it and never thought of using it. That article didn’t really touch on any of the nitty-gritty of using the command line, though. And the heart of deftly using the command line is really down to having sufficient knowledge of a bunch of different commands that you can use.

As we mentioned in the original CLI article, the command line varies a lot from traditional Windows and the more-Unix-y command lines of Macs and Linux distributions. This means that while some of these commands may roughly work on a Microsoft operating system, they will work differently enough that this tutorial is probably not useful for someone working in an exclusively or primarily Windows environment.

Also as mentioned in the first article, there are alternative shells for working on the Unix command line. As BASH (the Bourne-Again Shell) is the most common, all commands in this article will be in that format.

With those provisos out of the way, I think there is a lot of mileage that comes form having basic familiarity with a large number of Unix commands and having a sense of when and how they’re useful. So let’s get started. At the end of this tutorial you should have a solid understanding of all the most essential file-and-folder-manipulations commands.

Commands for Filesystem Navigation and Creation

Our last Quick Guide was about the very basic commands that you need to understand to move around the filesystem on the command line. Folders or directories are one of the most foundational concepts of modern computing, and being able to confidently and fluently move around there is the first step to take to be good at using the command line for your WordPress (or any other) work.

pwd tells you where you are

The first command that I think you must know to understand the command line is pwd. Because it tells you where you are already. Knowing where you are is always (in technology and in life ;p) the first step to getting to where you want to be.

pwd stands for “present working directory”, and that’s just what it tells you: where you are right now. Here’s the basics of it working:

MacBook-Pro:~ david$ pwd
/Users/david

If you’re not used to seeing the Unix/BASH command line, there’s a lot of unimportant gibberish to the left of the “$” on the first line there. You can really ignore it. For the curious, it lists where we are, and who we are. Then to the right of the dollar sign is the area where we type out command. In this case, we’re typing pwd and it’s responding with a location of /Users/david.

That directory is actually really special. If we look just to the left of the dollar sign ($), we see that to the terminal we’re currently david. And the /Users/david directory is thus our “home” directory for our user. This directory, in various commands and references, is used a lot. Because of its frequent use, it has a nice little shortcut: the tilde (~). That is on the key to the left of your number 1 in the top row of your keyboard.

cd moves your command line location for you

Changing where you are is accomplished with the cd command. What does it do? It “changes” your current “directory”. So if you’re in /Random/Folder/on/your/computer and you want to get back to your “home”, you’d type

cd ~

And if you wanted to go to an arbitrary relative path (starting from where you are), you’d type:

cd arbitrary/relative/path

If you wanted to deterministically and with certainty know where you were going, you’d start your cd command with a file path that has a forward slash (/) at it’s start.

And if you wanted to deterministically and with certainty know where you were going, you’d start your cd command with a file path that has a forward slash (/) at it’s start. /Users/david is an absolute path. I could go from there to the /Users/david/Dropbox/WebDev directory by using any of the following commands:

cd Dropbox/WebDev #relative to where I am now
cd ~/Dropbox/WebDev #relative to my user root
cd /Users/david/Dropbox/WebDev #relative to the system root

Assuming I started in /Users/david, all three of those commands take me to the identical location. Knowing that, and being able to see how all of those yield the same location is crucial.

The final common need when cding is to go up a level. If I’m in /Users/david/Dropbox/WebDev I can get back to my home directory in a couple ways:

cd ../..
# or
cd ..
cd ..
# or
cd ~

The primary piece of new knowledge here is that .. means “up a level” in the Unix command line. (Less necessary to know but still really useful—a single dot, ., is where you currently are.) So typing cd .. twice is the same as typing cd ../.., which will both move me to a location two folders above (in the hierarchical structure of Unix directories) than I currently am.

ls tells you what’s in your current directory

screen-shot-2016-11-16-at-11-04-08-amSo we’ve learned how to tell where we are. And we’ve covered how to change where we are. The last crucial thing to know (and the last bit of knowledge that we covered in the Quick Guide about starting to move around the filesystem from the command line), is ls. ls “lists” what is in the current directory that our terminal is resting. As if we’re in /Users/david, typing ls will show us all the folders and files that are sitting in that folder.

If we need a little more detail, we’ll want to add the -l flag. ls -l will tell us not just the files and folders, but a wealth of other information like the user who owns each file, its size, and more. (Again, for a little more detail on the ls flag(s), look over the Quick Guide about the using the CLI to navigate the filesystem.)

mkdir makes a new directory

mkdir is the first command that changes something other than your current terminal. And as you may have been able to guess, the mkdir command is how you “make” a “directory”. That is, if I want a new folder, I’d just simply type the command:

mkdir new-directory

If that worked—no typos—then we’ll now have a folder wherever we are called “new-directory”.

touch creates a file

touch is one of the less-obviously-named commands

Finally, the last command that I think you should know about for basic navigation around the command line is touch. touch is one of the less-obviously-named commands, in my opinion. What touch does is create a file for you. If I wanted a text file called “touched.txt” I could easily create an empty file with the command:

touch touched.txt

Once that file exists, I can add things to it using other commands. Or I can remove it. Or move it. And that’s the focus of our next section: the essential file-manipulation commands that are crucial for effective use of the CLI in a Unix-y system.

Manipulating Files on the Command Line

Once you’ve moved around and have created some files and folders, the next thing you’ll need to do is make changes to them. Maybe that’s taking away a file or folder, or moving it, or changing its contents. The possibilities are big, but only a few commands are really essential to getting comfortable with manipulating files and folders from the command line. The core ones are listed below: rm, mv, cp, editing commands, and version control.

rm deletes files

rm has a pretty understandable name, it “removes” files. So lets assume we have the touched.txt file we made with the touch command, but we decide we no longer want it. What do we do?

rm touched.txt

And it’s gone. What about removing a folder though? There are a couple options. The first is that there does exist an rmdir command which is made to remove directories. The hitch is that if the directory has anything in it, it won’t work.

So that’s why most people use rm -rf to remove a directory and all the files inside of it. This is just the same rm command we used to remove touched.txt, but we’ve given it two flags. The -r flag signifies that we want rm to act “recursively”. “Recursively” means “keep doing the same thing all the way down”. So when we rm recursively we’ll remove all files and folders inside the file and folder we’ve told the CLI to remove. The -f flag stands for “force”, which is your way of telling the CLI that you really mean it and you don’t want it questioning you or double-checking with you or quitting part-way on you.

mv moves files and folders for you

The “move” command works about how you’d expect: it moves files or folders for you. It’s typed as mv and you start with where it is, and then say where it’s going. So it basically looks a little like this on your command line:

mv touched.txt moved-touched.txt

You can use it the exact same way for folders. And you can also use mv to put a file in a folder. That’s done with a command like:

mv touched.txt moved-folder/touched.txt

Make sense?

cp is the CLI way to copy a file

You use move when you want to change the place a file is. You use cp when you want to replicate file or folder. Like mv you first say where it (the file or folder you want to copy) is, and then where you want to new duplicate to go. So to duplicate touched.txt you’d do something like:

cp touched.txt touched-copy.txt

It’s more idly interesting than useful, but judicious use of cp and rm can get around you ever typing the mv command. Why you’d try to do that is a bit of stumper for me though…

nano, vi, or emacs are text editors that work from the command line

My newbie-CLI-editor of choice is nano.

rm, cp and mv can do for you most everything with your files as black boxes of data. But they don’t change what’s inside of them. You can see what’s inside of your files with commands like cat, head, tail (that’ll be my only mention of them, as I don’t think they’re crucial to know, just cool).

But to really see your files and be able to make changes to them, you need a text editor. If you’re already comfortable with Sublime Text or Atom or whatever, getting to know a command line text editor is not completely required when you’re on a local machine. But as soon as you start using the command line on a remote server, having at least some familiarity with a CLI text editor is essential. vi and emacs—about which whole books could and have been written—are heavy-duty and may replace your current GUI editor for you if you learn them really well.

My newbie-CLI-editor of choice is nano. And you can get inside of touched.txt and start to actually add some content to it with a simple:

nano touched.txt

What I really like about Nano—as opposed to VI(M) or Emacs—is that all your basic manipulations are shown on the screen. It tells you that you write/save the file with CTRL+o, and so on right on the screen. If you’ve never found yourself (as many newbies do) in VI not being able to quit. That seems pretty trivial, but if you’ve been there, you’ll understand why I think the using screen space for those explanations is great for beginners (or anyone who just occasionally finds a need for a CLI text editor).

git, svn, etc let you version control

Knowing that the git command exists is useful, even if you stick to the GUI app you prefer.

Finally, as with the text editors, the scope of these commands are too big to cover in depth. But version control is a core part of file manipulation for developers in WordPress and beyond. And git is the most common version control application in use today.

You can do version control with a GUI app, or even in your text editor itself, but the real heart of Git is in the command line interface. And so knowing that the git command exists is useful, even if you stick to the GUI app you prefer. A commit is done with the git commit command, and a push is done via git push. I could go on, but as I said knowing the Git CLI well is far beyond this article’s scope.

If you ever doing any work with WordPress.org, it’s also important to know that svn, which is the command line interface for the Subversion version control system, is also available on most Unix computers. That’s what WordPress.org uses, so as a WordPress developer it’s good to have at least passing familiarity with.

What We’ve Learned About Commands for File Manipulation

We’ve only scratched the surface of the entirety of the command line. It is a vast and long-established kingdom. But I hope now you feel confident in all the basic functions that you might need to do as a developer trying to make your first go at the command line.

I think there are other non-file commands that are essential to really know how to use the command line to it’s full potential, but it’s impossible to really get those under control without having an ability to do all the basic file manipulation things. So hopefully how you use pwd, cd, and ls are all kind of clear to you. And you know that touch and mkdir make new things for you. That rm gets rid of them, cp duplicates them, and mv will change where they are.

Knowing that version control and text editing can be done from the command line is crucial too, though many WordPress developers never really have a need for either of those. But some prefer it for one or the other operation. Try out the Unix command line interface for one or both at least a few times and you’ll find it much easier to do in the future when you absolutely must. Happy hacking!

Yay! 🎉 You made it to the end of the article!
David Hayes
Share:

5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Working With The Command Line and WP-CLI – WPShout
June 6, 2017 12:14 pm

[…] 10-ish Unix CLI File Commands Every WordPress Developer Should Know […]

Anatomy of a WP-CLI Command: wp database import | WPShout
May 9, 2017 10:56 am

[…] Article: 10-ish Unix CLI File Commands Every WordPress Developer Should Know […]

The What, How and Why of WP-CLI: WordPress in Your Terminal | WPShout
January 31, 2017 10:26 am

[…] don’t think or worry about WP-CLI. (But if you’re interested, here are the basics of file and non-file Unix CLI commands.) WP-CLI isn’t essential and it’s not something you […]

Mastering the Unix Terminal: Working With Permissions, Networking, and Other Key Concepts | WPShout
January 17, 2017 11:22 am

[…] Last time we covered Unix commands, we talked about how you can move around the filesystem from the command line. These ideas are crucial; if you don’t understand the basics of navigating files and folders from the terminal, there’s not a lot you can do on the command line. If you’ve not mastered that, start with this on file-manipulation from the command line. […]

Gary Hall
November 29, 2016 2:09 pm

In filenames you can use wildcards and ‘*” is a very common one. So ls *.txt will show you all files with the extension txt. and ls twenty*.pdf will list all pdf files that start with twenty. And cp *.log ~/oldlogs will copy all files ending in .log.

Which brings up this: rm *.log will wipe out all files ending in .log in one stroke. When using wildcards with rm it is best to use rm -i, i stands for interactive and prompts you y or n to delete a file. So, if you were to type rm -i *.log then you will be prompted for each file that matches *.log. So, if one of the logs was named superimportant_DONOTDELETE.log then rm -i *.log would give you the opportunity to keep that file.

Or start the conversation in our Facebook group for WordPress professionals. Find answers, share tips, and get help from other WordPress experts. Join now (it’s free)!

5
0
Would love your thoughts, please comment.x