In this video I am going to share with you the commands I use most frequently when working with Symfony projects on the Linux command line.

One of the biggest concerns I hear from developers new to frameworks like Symfony is that there is a need to use the command line. I completely understand this point of view. Lord knows there's enough to learn with the framework itself, let alone having to learn the command line of a new operating system too.

Well, much like with Symfony - where you only need to learn a few core concepts to get going - I can assure you that learning to use your server's command line is a similar process. There are a few commands you will use frequently, and for everything else there is Google and StackOverflow :)

One thing worth noting is that unless you are absolutely sure you will be deploying to a Windows server environment then I would highly advise you to start using some variant of Linux in your development environment.

Tools like VMWare (paid) and VirtualBox (free) make getting started with Linux a much easier process than you might think. Back when I got started with Linux I had to download and burn a CD, then format my only desktop pc and install over the top. Then things inevitably went wrong and I had no way of 'googling' for solutions. Actually Google wasn't even the search engine of choice back then, but I digress.

Nowadays, these virtualisation tools allow you to install a copy of Linux (or any other operating system) in a virtual machine which runs a full / real version of Linux right alongside your existing desktop. You can happily be on your Macbook or Windows pc, but be running Ubuntu or CentOS or FreeBSD just like you might run sublime text or phpstorm. Pretty awesome. And of course that means you have your browser to Google for solutions or tips if you get stuck.

Hopefully this gives some reassurance that it's not all that bad, and like anything the hardest part is getting started.

I've covered installing Ubuntu using VirtualBox before so be sure to check out that tutorial if you would like to know more.

There are additional benefits to learning Linux. Firstly it gives you greater freedom - you won't need to rely exclusively on your host to do all your admin tasks.

But more importantly - in my opinion - is that it gives you confidence. You can learn on your local virtualised development environment, taking advantage of snapshots before doing risky operations and you have an immediate quick save tool.

And as you grow in your career, especially moving into more senior development roles, it is almost inevitable that you will need to work with the command line. Whether this is setting up, troubleshooting, or anything else in between, command line chops are a very useful (and financially worthwhile) skill to have.

What's on the box?

Once you have your shiny new Linux server up and running it's time to start exploring.

I'm going to assume you have a Symfony site up and running for the purposes of this tutorial, as covering how to set up an apache or nginx web server is out of the scope of this tutorial. However if this does interest you then leave a comment as I'd happily cover this if there is enough interest in doing so.

Viewing Directory Contents

If you're used to having a GUI then things you consider basic on your desktop can become a real head scratcher when all you have is a blinking white cursor to work from.

Fortunately, the command ls(short for list) is nice and easy to remember - after all, you want to list the directory contents. If only Windows followed suit - the equivalent Windows command: dir won't work for you here.

Being Linux there are many options (called switches) that can be passed in to the ls command but you don't need to know any of them to get started.

Depending on your variety of Linux, ll is a shortcut (or in Unix parlance, an alias) to ls -lh and is lazier to type than ls so I tend to use it most.

This will run the list command with long output (-l) and human readable file sizes (-h). You can learn about the myriad options by either typing the command man ls, or using a site such as this.

Relative and Absolute Paths

When working with commands such as ls, you can use relative or absolute paths.

I must say that when I first began working with Unix-y operating systems, I found this terminology confusing. It needn't be.

An absolute path is simply the full path to a file. Let's say we have a Symfony site living at:

/var/www/mysite.dev

We could say the absolute path to the parameters.yml file would be:

/var/www/mysite.dev/app/config/parameters.yml

This is the full path to the file.

Likewise, the relative path would be the location of the file relative to our current position in the file system. If we were already in the /var/www/mysite.dev directory, then the relative path to the parameters.yml file would be app/config/parameters.yml.

There's some interesting things happening here which are not immediately obvious to beginners.

Firstly, we don't use the starting forward slash:

app/config/parameters.yml

not

/app/config/parameters.yml

Because if we have the leading forward slash, this would become an absolute path - and that path likely does not exist. Remember, that first slash means the filesystem root.

Secondly, we can use the .. syntax to traverse up directories as needed.

For example, let's say we are in our terminal and we have navigated to the /var/www/mysite.dev/src/AppBundle/Controller directory.

We could still list (ls) the contents of the /var/www/mysite.dev/app/config/ directory by either:

pwd
/var/www/mysite.dev/src/AppBundle/Controller

# list using the absolute path:

ls /var/www/mysite.dev/app/config/

# or

ls ../../../app/config/

Which is definitely strange, but if you think about it, the .. syntax simply goes up one directory. We are saying:

  • From our current directory;
  • Go up a directory;
  • Go up a directory;
  • Go up a directory;
  • Go into the app/config directory from this location;
  • List those contents for me, please, thank you;

Powerful, flexible, but initially confusing until you try it and then it becomes useful quickly.

Clearing The Screen

A very quick but handy tip is to use CTRL+L to immediately 'wipe' the contents of your terminal. This is particuarly useful when working with log files which can spew out gobs and gobs of information, and often you don't care about anything other than the current, or immediately previous request.

This is a shortcut to simply typing clear into your terminal, which does almost exactly the same thing.

I say almost as typing clear will add an entry to your terminal history file, whereas CTRL+L will not.

IP Addressing

Configuring an IP address is outside the scope of this video, and should - hopefully - be taken care of by either your host, or your virtual machine software. CentOS can be a little tricky about this, so I'd suggest Googling for a guide by Digital Ocean or Linode to get you further.

Knowing your IP address is always pretty helpful - and of course, the command is slightly different to the Windows equivalent:

ifconfig

Which stands for interface configuration, versus ipconfig on Windows.

Quickly Viewing A File

Often when on a new server in particular, you may need to double check some config file - maybe the parameters.yml file for instance.

The quickest way I know of is to use the cat command, which strangely means to 'concatenate files and print on the standard output` - or to you and me, get the file contents and show it in your terminal.

One downside to cat is that you cannot edit the file in any way. It is simply for reading the file contents.

This can be combined with the absolute or relative path information we discussed a little further up:

pwd
/var/www/mysite.dev/src/AppBundle/Controller

# list using the absolute path:

cat /var/www/mysite.dev/app/config/parameters.yml

# or

cat ../../../app/config/parameters.yml

CPU, Memory, and Process Management

If you have even a modicum of experience with Windows, you surely must have had to dive in to the process manager at some point or other, killing off programs that are no longer responding, or simply admiring how much memory your operating system is chewing up for seemingly no good reason :)

Linux has the equivalent, albeit with a little more nerdy output:

top

Will display all your running processes, memory, CPU, and swap file usage in a real time, text-based output.

There's plenty of useful information to be gleaned from the top command, and whilst I don't have time to do a deep dive in this video, the very high level overview it can provide is often immediately useful.

Another variant of this, which is a little more visual is htop which needs installing separately (top is a default command), which you may prefer.

Working With Log Files

Perhaps the most common task that I perform when working with Symfony in both production and development mode is to work with the log files - dev.log, prod.log, and so on.

Symfony logs an absolute boat-load of information, and this can be both a blessing an a curse.

Right here I will mention that I have seen companies both big and small fall victim to not rotating their log files. This is trivial to do with Symfony, so if you haven't already done so, check out this Cookbook article and get rotating your logs. Honestly, I have seen this bring down production sites because the log files have completely filled the available disk space... epic fail!

Being able to watch the log files in real time is a really useful technique to have in your toolbelt. It's also really easy to do.

tail -f path/to/your/logs/env.log

Here we are using the linux tail command which shows the last part of a file, but we are also using the -f flag to follow the file as data / content is appended to it.

This allows you to watch as requests come in to your site, and to see information flowing through your system in real time. It can be combined with additional logging commands using monolog to really gain some valuable insight into just what the heck is happening throughout the lifecycle of requests to responses in your application.

Once you have tail'ed a log, you may be wondering how to exit from the log file. Use CTRL+C :)

Another useful command during development is to truncate the dev.log file. This can be especially useful to absolutely ensure you are only logging the output from the next request - assuming you only 'refresh' once after truncating :)

I must stress, it's likely not a great idea to truncate your prod.log file. There can be very useful information in there that you may inadvertantly lose. Don't be that guy / girl.

To truncate the logs:

sudo truncate --size 0 dev.log

Remember, it requires sudo because with great power comes great responsibility.

Another slightly more advanced method when working with tail is to pipe the output through a grep command to only pick out certain logging levels:

tail -f /var/www/mysite.dev/app/config/parameters.yml | grep "CRITICAL"

Which will then only display any critical level log statements, effectively muting any other log output. The output won't be lost, just not displayed as it doesn't match our grep filter.

There are more advanced ways to work with log files than this, but if they aren't set up and you need to do some debugging on the fly, it can be useful to quickly use these techniques as required.

Halp, I Forgot My sudo!

A quicky - if you run a command which requires sudo, such as the truncate command - then you can either do some funky combination of up arrow, then CTRL+A to jump to the start of the line, then type in sudo...

Or you can ninja it by simply typing in !!, which will do exactly the same thing but with many fewer keystrokes :)

History

As already mentioned, the history command is going to show you every* command that's been typed in to your terminal by your currently logged in user, both in this session, and any previous sessions.

Now, I add the * as in truth, this is not going to catch every command.

Sometimes, your terminal will boot you out for some reason, and when you log back in, all the recent terminal history will have been lost. This is usually (in my case) when you've been using some mega long java --many-switches --omg --kill-me-now command which you forgot to take a copy of for some inexplicable reason.

Other times, you can temporarily disable adding a line to your history also. We can do this by adding a single space before running a command:

# will appear in your history output:

ls /var/www/mysite.dev/src/AppBundle/Controller

# will not appear in your history output:
 ls /var/www/mysite.dev/src/AppBundle/Controller

The leading space is very important.

This can be useful when using a command such as the FOSUserBundle commands to add a new user with a password, and you don't want that password to end up in the history for obvious reasons.

Another handy use of the history command is to find previously run commands - such as those long Java process runners - and then re-run them without typing them out again. To do this, simply reference the line number with a leading exclamation mark.

For example, if we had a history output like:

history
 1162  gp gitlab master
 1163  sudo apt-get update && sudo apt-get upgrade -y
 1164  alias ll
 1165  man ls

And we wanted to re-run history item 1163 without having to re-type it out, we could:

!1163

Which will re-populate the same command on to your terminal, ready for you to press enter and run again. Very handy.

Much like with the output from our tail command earlier, you may wish to only pick out bits of the history that match a pattern. Again, we can use a grep here to selectively display only a subset of the history output:

history | grep "man"
 1165  man ls
 1166  man cat
 1168  man top
 1169  man tail

Abort, Abort!

Sometimes, and I must admit, all too frequently in my case, you will find yourself in a position where you inadvertantly bork your terminal session. Oops.

This is particularly annoying if you have been working locally and then done a quick ssh onto a different box, and then maybe done a 'save state' in virtualbox, and accidentally frozen your shell.

Seemingly, the only way to escape is to close the terminal window.

This works, but is a little drastic.

A better way is to use the keyboard command sequence of:

Return then ~ then .

This will exit the current session, dumping you back to your local terminal. Phew.

Other Useful Commands

If you have any other commands that you use frequently when working with Symfony on the command line then I would really like to hear about them.

Please do leave a comment with any feedback, suggestions, and terminal tips :)


Share This Episode

If you have found this video helpful, please consider sharing. I really appreciate it.