Embrace the commandline

If you want it or not, the commandline is part of your daily toolset as a web developer, so let's embrace it !

6 minutes

Embrace the commandline

Introduction

If you want it or not, the commandline is part of your daily toolset as developer. As a Laravel developer you will use artisan, as a javascript developer you will use npm. As a web developer you will use the commandline on a daily basis. We have to embrace the commandline, and once you do so, it will give you extra powers.

What is the power in it ?

If you embrace the commandline then you will see that a lot can be done faster. By creating aliases to commands you can even make the commands shorter. An alias can be created by typing alias [youralias]='[your command]') , for example alias pam='php artisan migrate' will run the php artisan migrate command when you type pam.

I've created aliases to directories on my filesystem, so I can jump to them very fast. Typing open . opens finder on the location I jumped into. pstorm . opens phpstorm for the given directory and gittower . opens git tower for the directory. If you now combine this in an oneliner and create an alias for all this together you can get this ...

alias work='open . && pstorm . && gitower .'

If you now type work in a directory your productivity tools will open. How cool is that!

Some commands I use almost daily

The commands like ls , mkdir , cd are known by almost everyone, but there are a lot of handy commands that are less well-known but are nevertheless very useful. I'll try to give you some commands that I use A LOT and that might help you to speed up your workflow. I've already spoiled a couple commands in the previous chapter.

alias

It can be very handy to create an alias for common used commands. These aliases are shortcuts to a given command. Aliases are saved in your user profile and will be remembered by your system.

An alias can be created by typing alias [youralias]='[your command]') , for example alias pam='php artisan migrate' will run the php artisan migrate command when you type pam.

&&

With && you can combine multiple commands in one line, this is very handy in combination with an alias. You can create aliases that do multiple things when calling the alias.

tail

Do you want to see new entries written into some log file, maybe the laravel.log file of your application. Then tail is your companion command. If you tail a file, the last lines of the file will be shown. By default, tail prints the last 10 lines of each file to standard output. If you specify more than one file, each set of output is prefixed with a header showing the file name.

If you want to output more than the default of 10 lines you can use the --lines=num or -n num option, specifying the number of lines you want to show.

It is also possible to let tail output any new line added to the file your are looking into. So if a new log entry is written to the file, it will immediately be shown in your output. This can be done via --follow or -f as option.

history

If you type history you will get a list of the n last commands that you did run on your computer. This gives you the possibility to copy past commands that you executed in the past.

As mentioned by Jason Judge in a reply on twitter you can also use control-R to search through your history to go back to a previous command.

|

the | , the pipe command makes it possible to chain commands. So you can use the output of a previous command to a next command. This is very handy in combination with grep. You can let a command output something to your screen, pipe it to another command, so it will not be shown on your screen but passed to the next command.
In combination with grep you can now search in the output.

grep

Grep gives you the power to search into files. grep -Ril "string to search" * Will search all files in the current directory and show you the filename of the file where the string is found in.

i stands for ignore case (optional in your case). R stands for recursive. l stands for "show the file name, not the result itself".

Grep can also be used to search in piped output, I use the combination of history and | grep a lot . For example , if you have multiple of versions of php installed, and you don't actually can't get the directory top of mind where php8 lives, but you know you ran a command with php8 last week then you can search your history with the following command.

history grep | php8

chown

chown makes it possible to change the ownership of a file. Via these commands we will change the owner of the files to 'www-data' and assign the files and folders to the 'www-data' user group. Making it possible for apache to write into this directories.

chown -R www-data:www-data *

curl

You might know the term curl-request, but did you know that curl is a terminal command. You can use it to test get requests on the commandline.

touch

The touch command allows you to create a blank new file through the Linux command line. As an example, enter touch /home/username/Documents/Web.html to create an HTML file entitled Web under the Documents directory.

pwd

The pwd command shows the path you are currently working in.

df

Use df command to get a report on the system’s disk space usage, shown in percentage and KBs. If you want to see the report in megabytes, type df -m.

du

If you want to check how much space a file or a directory takes, the du (Disk Usage) command is the answer. However, the disk usage summary will show disk block numbers instead of the usual size format. If you want to see it in bytes, kilobytes, and megabytes, add the -h argument to the command line.

Retrieving the size of all files and folders in a given directory can be done via following command du –sh.

If you want to receive the total size of a given folder including the sum of all subfolders you can do this via this command du -h --max-depth=1.

ps

ps displays information about a selection of the active processes. If you want a repetitive update of the selection and the displayed information, use top instead. Ps will show a list of processes , including a process id. I you want to terminate a process you can use kill pid to terminate the process.

Sajan

Because there are many commands that are hard to remember, or combinations of commands that we often run together I've create sajan. It's a commandline tool that gives you some shortcuts to this difficult to remember commands. You can find more information about sajan on Github.

Learn more

I've contributed to a Linux book. It explains the most popular Linux commands, it's a completely open source driven book and can be downloaded here. If you also want to contribute to the book, you can take a look into the GitHub repo


this article has been read 190 times