Working with WordPress on a daily basis at Combell.com (a Team.Blue company) means I get to see, optimize and debug a ton of WordPress sites each single day. This also means I sometimes need things to be dealt with quickly. And that’s where the command-line comes in…
So why won’t you use WP-CLI?
Of course I use WP-CLI too. It’s a great addition to the toolkit of any user/developer/hoster. But there are (quite often) times when WP-CLI is just not an option or too slow. And that’s no complaint or insult towards WP-CLI by the way. Let me explain by some examples:
- When WordPress core, a certain plugin or a theme causes a fatal error, WP-CLI is rendered useless.
- I’ve seen cases where a very poorly optimized query caused such a slowdown on WordPress that any wp-cli command, for example a wp user list, took more than 5 minutes to complete. This was solely caused by a plugin, not by WP-CLI, but it did impact WP-CLI.
In these kind of cases I usually resort to the command-line or bash. Bash stands for “Bourne again shell” and it is the environment in which we run our commands.
Identifying where we are.
Once connected to your server via SSH, you’ll need to figure out where in the system you are. This can be done by checking the current working directory via the command “pwd“. It stands for “print working directory”.
user@brecht:~# pwd /root
In this case, “pwd” shows us that we’re in a directory “root”, which happens to be one level down from the root of the entire server (which is indicated by the “/”).
So know we know where we are, but we still do not know what’s inside this directory. This is where the command “ls” comes in. This command allows us to show a list of the directory content.
user@brecht:~# ls user@brecht:~#
Interestingly, the use of “ls” show us nothing, only a blank line. This indicates to us that the directory is empty… or isn’t it?
Since “ls” only shows us non-hidden files, we’ll need to extend the command with some options to see more. Here are the ones I use most and are interesting for anyone:
- “a” shows all files, including hidden ones (those start with a “.”)
- “l” shows the output in long format, adding file ownership and group owner, but also things like the file permissions and the time and date of the last edit of the file.
- “h” stands for “human-readable”. This option will convert filesizes from the number of bytes to kilobytes, megabytes and such.
Once we combine all of these options into the “ls” command, we get an entirely different result that provides us much more information:
user@brecht:~# ls -alh total 64K drwx------ 4 root root 4.0K Sep 6 15:07 . drwxr-xr-x 20 root root 4.0K Sep 3 20:05 .. -rw------- 1 root root 23K Sep 6 15:07 .bash_history -rw-r--r-- 1 root root 570 Jan 31 2010 .bashrc -rw-r--r-- 1 root root 0 Aug 27 12:55 .cloud-locale-test.skip drwx------ 3 root root 4.0K Aug 28 19:42 .config -rw-r--r-- 1 root root 148 Aug 17 2015 .profile drwx------ 2 root root 4.0K Aug 27 12:55 .ssh -rw------- 1 root root 15K Sep 6 10:45 .viminfo
While we see a lot of hidden files, we’re still not seeing our WordPress site here. So we’ll need to change to another location on the server…
Basic navigation on the server.
Basic navigation on a linux-based server is mostly done via the “cd” command. It stands for “change directory”, which in itself is rather self-explanatory.
In our current example, we’re still in the “/root” directory. As there are no folders inside the directory “root”, we’ll need to search elsewhere. So our first step would be to go to the absolute root of the server, indicated by “/”. We can do this in two ways from our current location:
“cd ..” will in this case take us up one level. As “/root” is only down one level, going up one level will take us to root. However, in the imaginary case that we would be in “/root/someotherfolder”, using “cd ..” would also just take us up one level. This means we would then end up in “/root”.
“cd /” will always take you straight to the root of the server, no matter where inside the folder structure you are. So this is also why this command makes most sense to use right now. So let’s use it right away…
user@brecht:~# cd / user@brecht:/# user@brecht:/# pwd /
We notice several thing in the commands above. First, directly after executing “cd /”, you’ll see that a “/” appears directly after “user@brecht:”. This is a first indication we’re in the root of the server. Second, when we run “pwd”, it also outputs “/” as the confirmation that we’re in the root of the server.
Now, to see what’s in the root of the server we’ll run our “ls” command once again:
user@brecht:/# ls bin initrd.img lib64 opt scripts var boot initrd.img.old libx32 proc srv vmlinuz dev latest.zip lost+found root sys vmlinuz.old etc lib media run tmp wordpress home lib32 mnt sbin usr
This shows us all files and directories present in the root. For this series, I’ve set up my demo site in the folder “wordpress”. To change into the “wordpress” folder, we’ll use the command “cd wordpress”:
user@brecht:/# cd wordpress user@brecht:/wordpress#
Once again we’ll see the directory-name appear directly after “user@brecht:” confirming us we’re in the “wordpress” folder.
And that concludes the first part in this series of blog articles. I hope you found it useful. Feel free to share this article with your friends and to leave any questions in the comments. Thanks for reading my blog.