{"id":155087,"date":"2016-05-07T14:00:01","date_gmt":"2016-05-07T14:00:01","guid":{"rendered":"https:\/\/premium.wpmudev.org\/blog\/?p=155087"},"modified":"2016-05-08T15:03:48","modified_gmt":"2016-05-08T15:03:48","slug":"terminal-command-line","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/terminal-command-line\/","title":{"rendered":"A Quick Guide to the Terminal and Command Line Prompt for WordPress"},"content":{"rendered":"<p>The terminal is one of those things that seems scary for the uninitiated. It&#8217;s all text based and spits out responses you don&#8217;t understand and feels like you&#8217;re hacking the system.<\/p>\n<p>The reality is, it is extremely simple and straightforward and can shave hours off each of your work days, I kid you not.<\/p>\n<p>In this article, I&#8217;ll introduce you to the terminal, show you how easy it is to use, and teach you the basics you need to know to work with great products that can automate everything from tidying your CSS to migrating your WordPress site with a single command.<\/p>\n<h3>What is the Terminal \/ Command Line Prompt?<\/h3>\n<p>The terminal is an interface in which you can type and execute text-based commands. Command prompt is the name of the terminal in Windows, otherwise, it is pretty much the same.<\/p>\n<p>OK, so that was a little bit of a white lie.<\/p>\n<p>The operating principles of command prompt (let&#8217;s use cmd from now on) and the terminal are the same, but various commands might not be. The same thing happens with graphical user interfaces. In Windows, you would close windows in the top right corner, while in OSX you close programs in the top left. The principle is the same, just the element is simply found elsewhere.<\/p>\n<p>The goal of this article is to show you how commands work with some hands-on examples. Armed with this knowledge you&#8217;ll be able to use any command line tool!<\/p>\n<p>From now on, I&#8217;ll refer to both command prompt and the terminal as &#8220;terminal&#8221; unless I am highlighting specific differences between\u00a0the two.<\/p>\n<h4>A Note for Windows Users<\/h4>\n<p>If you&#8217;re using Windows, I recommend <a href=\"https:\/\/cygwin.com\/install.html\" target=\"_blank\">Installing Cygwin<\/a>. Most tutorials that contain command line references work with an environment like Ubuntu or OSX where some command names are different. Cygwin adds many of these commands to Windows giving you a uniform experience.<\/p>\n<p>This tutorial will also mostly use Unix-style commands so following along will be easier with Cygwin. If you don&#8217;t want to install it and get a &#8220;command not found&#8221; error when trying an example simply search for the Windows alternative in Google.<\/p>\n<h3>What Can the Terminal Do?<\/h3>\n<p>No doubt you are used to basic operations like creating a folder by right-clicking and selecting &#8220;New Folder,&#8221; then typing a\u00a0name.<\/p>\n<p>In the terminal, you would issue the following command:\u00a0<code>mkdir foldername<\/code>. And that&#8217;s it.<\/p>\n<p>This is a lot faster and many commands that do the same things as your GUI can take a bunch of parameters for more flexibility, more on that later.<\/p>\n<p>Your operating system already a huge list of commands you can use for everything from creating files to optimizing images. New commands can be installed that allow you to do all sorts of cool things.<\/p>\n<p>Here are just some things I do every day:<\/p>\n<ul>\n<li>Start and manage local WordPress development environments<\/li>\n<li>Automatically convert Sass to CSS<\/li>\n<li>Concatenate and minify multiple JS\/stylesheet files<\/li>\n<li>Resize multiple images at once in seconds<\/li>\n<li>Gain access to remote computers &#8211; my live sites usually &#8211; and issue commands there<\/li>\n<li>Install multiple WordPress plugins with a single command<\/li>\n<li>Search and replace within my WordPress database<\/li>\n<li>Use version control to manage my code<\/li>\n<\/ul>\n<h3>First Steps<\/h3>\n<p>Upon opening the terminal, you&#8217;ll be faced with a screen and a cursor. Here are two examples, one from OSX and one from Windows.<\/p>\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-735x735 size-735x735\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2016\/05\/osx-terminal.png\" alt=\"Terminal in OSX\" width=\"735\" height=\"182\" \/><figcaption class=\"wp-caption-text\">Terminal in OSX<\/figcaption><\/figure>\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-735x735 size-735x735\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2016\/05\/command-prompt.jpg\" alt=\"Command Prompt Windows\" width=\"735\" height=\"170\" \/><figcaption class=\"wp-caption-text\">Command Prompt Windows<\/figcaption><\/figure>\n<h4>Current Working Directory<\/h4>\n<p>The first thing to be aware of is that you are always in a folder in the terminal. This is a little bit more apparent in Command Prompt \u2013 it is shown in the beginning of the line. On OSX, it is shown at the end of the line \u2013 \u00a0in the screenshot it says <code>in ~<\/code>. <code>~<\/code> has special meaning, which is your home directory. On your local machine, this is probably <code>\/Users\/yourusername<\/code>.<\/p>\n<p>Why is this important? Many issues commands will have an effect on the directory they are in. For example, the <code>mkdir<\/code> command creates a directory. By default, the new directory is placed in your current directory:<\/p>\n<div class=\"gist\" data-gist=\"9d95a951ff1430555d9d2db779f1ec3a\" data-gist-file=\"mkdir.sh\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/9d95a951ff1430555d9d2db779f1ec3a.js?file=mkdir.sh\">Loading gist 9d95a951ff1430555d9d2db779f1ec3a<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This would create a directory named <code>newdirectory<\/code> in your current directory.<\/p>\n<h4>Absolute and Relative Paths<\/h4>\n<p>If you are required to type a directory name \u2013 like in the <code>mkdir<\/code> example \u2013 you can use relative or absolute paths. By typing a simple name I used a relative path, telling the command that I want to create &#8220;newdirectory&#8221; within the current directory.<\/p>\n<p>If I use an absolute path I can create a directory anywhere, regardless of my current working directory.<\/p>\n<p>Let&#8217;s assume we are in our &#8220;pictures&#8221; folder but we want to quickly create a backup folder in our home folder. We could do it like so:<\/p>\n<div class=\"gist\" data-gist=\"9ce9e88772ff7ea98d885f68f0becaf5\" data-gist-file=\"mkdir-absolute.sh\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/9ce9e88772ff7ea98d885f68f0becaf5.js?file=mkdir-absolute.sh\">Loading gist 9ce9e88772ff7ea98d885f68f0becaf5<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>Sometimes you&#8217;ll see a path that begins with <em>.\/<\/em>. This is also a relative path <em>.\/<\/em> indicated the current directory.<\/p>\n<h4>The Anatomy of a Command<\/h4>\n<p>Commands generally have four parts: the command itself, operands, flags and parameters. The command itself is the first string you type.<\/p>\n<p>The command itself is the first string you type. <strong>Operands<\/strong> follow the command (and any flags if given) and usually provide information about what you want to perform the command on.<\/p>\n<div class=\"gist\" data-gist=\"fb3e4136e186be32ff87884b2e2b3415\" data-gist-file=\"copy.sh\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/fb3e4136e186be32ff87884b2e2b3415.js?file=copy.sh\">Loading gist fb3e4136e186be32ff87884b2e2b3415<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>The command above will copy the file referenced in the first operand to\u00a0the location specified with the second operand. Some commands take a single operand, some take two or more, search Google for the command you are using to find documentation for it.<\/p>\n<p><strong>Flags<\/strong> modify the behavior of the command. A quick example is the <code>-i<\/code> flag applied to the copy command. It indicates that the command should ask for confirmation if the copy operation would result in a file being overwritten.<\/p>\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-735x735 size-735x735\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2016\/05\/cp-overwrite.png\" alt=\"Preventing a copy overwrite\" width=\"735\" height=\"66\" \/><figcaption class=\"wp-caption-text\">Preventing a copy overwrite<\/figcaption><\/figure>\n<p>Flags always consist of just one letter, but take care \u2013 they are case sensitive. Multiple flags can be added if available in one of two forms. To copy the file with a confirmation and more information displayed we would use the <code>-i<\/code> and <code>-v<\/code> flags. You could write them in any of the two ways below:<\/p>\n<p><strong>Parameters<\/strong> are unique to each individual command and you&#8217;ll mostly see them on advanced commands that perform more elaborate functions.<\/p>\n<div class=\"gist\" data-gist=\"a97f110d4c5d7357fae3348feb02d196\" data-gist-file=\"parameters.sh\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/a97f110d4c5d7357fae3348feb02d196.js?file=parameters.sh\">Loading gist a97f110d4c5d7357fae3348feb02d196<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This command is from a package named <a href=\"http:\/\/wp-cli.org\/\" target=\"_blank\">WP-CLI<\/a> which automates common WordPress tasks. It fills the config file with information about our database. Technically <code>wp<\/code> is the command and <code>core config<\/code> are two operands. However, WP-CLI uses operands to group various commands. There is a <code>core download<\/code>, <code>core install<\/code> and various other commands.<\/p>\n<p>Parameters are at the end, you can input information you want to send along with the command using these.<\/p>\n<h3>That&#8217;s Really\u00a0All There is to Know!<\/h3>\n<p>There is quite a bit more you can do in the terminal but in most cases you&#8217;ll end up issuing commands. If you want to put yourself to the test I recommend reading our <a href=\"https:\/\/wpmudev.com\/blog\/gulp-for-wordpress\/\" target=\"_blank\">Using Gulp To Speed Up WordPress Development<\/a>. It contains plenty of commands to issue and the end result is well-worth the effort.<\/p>\n<p>Look out for more posts on this topic soon. In other tutorials, we&#8217;ll look at editing files within the terminal, creating bash scripts that allow you to run multiple commands and once and using SSH to run commands on remote machines.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The terminal \u2013 or command line \u2013 is one of those things that seems scary for the uninitiated. It&#8217;s all text based and spits out responses you don&#8217;t understand and feels like you&#8217;re hacking the system. The reality is, it is extremely simple and straightforward and can shave hours off your work day.<\/p>\n","protected":false},"author":344049,"featured_media":155117,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[263],"tags":[10195,10473],"tutorials_categories":[],"class_list":["post-155087","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-command-line","tag-terminal"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/155087","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/344049"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=155087"}],"version-history":[{"count":17,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/155087\/revisions"}],"predecessor-version":[{"id":209599,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/155087\/revisions\/209599"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/155117"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=155087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=155087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=155087"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=155087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}