The command line is really powerful and there are a quite a few things made easier by using it.
When I started learning things, I did a lot of copy-and-paste. It got the job done (usually…), but I rarely understood what I was doing or why it worked. So I want to outline a handful of basic commands, and some tips and tricks.
In the code snippets below,
$ indicates a command I’m entering into the terminal and the rest is output (known as standard output, standard out, or stdout) from the terminal. For example
$ echo "Hello" Hello
Here are a few basic commands. There are certainly others that can be called “basic.” Perhaps I’ll do a Part II if it seems called for.
cd – Change Directory
This command will change the current directory, either an absolute or relative path will work.
$ cd somefolder
$ cd ~/Desktop/
$ cd /var/www/html/mysite.com/
And go up a directory
$ cd ..
pwd – Present Working Directory
Simply tells you where you are currently. If you’re working with symlinked directories, you may find the
-P flag useful,
$ pwd /Users/KLampert/Desktop
ls – List
List files in the current directory. By default, you’ll get a list, in columns, of all ‘regular’ files and directories. You won’t see hidden file/folders, or see any info about them.
$ ls index.php wp-config-sample.php wp-login.php license.txt wp-config.php wp-mail.php readme.html wp-content wp-settings.php wp-activate.php wp-cron.php wp-signup.php wp-admin wp-includes wp-trackback.php wp-blog-header.php wp-links-opml.php xmlrpc.php wp-comments-post.php wp-load.php
ls -a list all. Includes hidden files
ls -l list in long format. Shows filesize, ownership, permissions and more.
ls -lh used with -l. Makes filesizes human-readable
mv – Move a File
Move a file or directory from one place to another. Of course you can move a file from one directory to another, but it’s also how you’d rename a file – a lot of people don’t think of renaming as function of moving, but that’s all it is.
$ mv source-file.txt destination.txt
$ mv source-file.txt destination/source-file.txt
cp – Copy a File
Copy a file or directory. Like
mv, the source/original file is first, and the new file second.
$ cp wp-config.php wp-config.php.backup
For directories, you’ll need to use the
-r flag to recursively copy the contents.
$ cp -r wp-content wp-content-backup
cat – Concatenate and Print Files
You can pass multiple files into this command, and it will concatenate and print them out. But you can still pass just one file, which makes it really handy to look at a file without opening an editor.
man – Manual
Get the manual page for a command. This really handy for remembering the order of parameters and what flags are available. Also, sometimes it looks funny.
$ man logger
Once you’ve opened a man page, there are a few commands you should know.
q to leave,
f to move forward one page,
b to move backward, and
h to learn more about those shortcuts. And yes, you can also look up the manual page for itself.
$ man man
There’s a lot of value in being able to do two things at once. But even if combining commands is more than what you need now, it’s nice to know what it looks like, so you can better understand some of those lengthy one-liners in tutorials.
; The Simplest Way
Run one command, then another.
$ pwd; ls -lah
There’s probably a fancy name for this, I don’t know it. Similarly to the above, but the second part only runs if the first part evaluates to true.
$ true && echo "Hello" Hello
Since true is true, “Hello” will be printed
$ false && echo "Hello" $
But since false is not true (go figure), nothing will be printed. Of course, in the real world you wouldn’t use
false you’d have something useful.
| Pipe, Redirect Standard Output to Command
Send the output of the first part to the command in the second part.
$ ls | grep wp
Here I list the files, but before that list is printed out, it is passed to
grep which filters that list down to only items containing “wp”
> Redirect Standard Output to File
Similar to the Pipe, but instead of passing the output to a command, we’ll send it directly to a file. This works well with the
cat command mentioned earlier.
$ cat onefile.txt twofile.txt > onetwofile.txt
$ pwd > current-directory.txt
This will create the file if it does not exist, and will overwrite any contents that may exist.
>> Redirect Standard Output to File (Append)
> but will add to the end.
$ ls >> current-directory.txt
So if you run the last two commands together, you’ll get a file called
current-directory.txt that has the path at top followed by a list of files.
< Redirect Standard Input
This works especially well with commands like
sort. Suppose you have a big unsorted list of things that you need to, well, sort.
$ sort < things.txt
That will sort it, and print it back out. But probably you want to save that list, so we could use what we just learned and redirect standard output.
$ sort < things.txt > things-sorted.txt
Or, we can use what we learned earlier and read the man pages.
$ sort < things.txt -o things-sorted.txt
The advantage of the latter is that if we wanted, we could update the original file, rather than creating a second file.
$ sort < things.txt -o things.txt
Really, as with many things, the best way to get better and more comfortable with it is to get out there and use it. That’s why I didn’t put the remove command in here 🙂^