Advanced CLI: Commands You Should Know as a Developer
Advanced commands; get more done
No, in this article we won’t go over the basic commands like
ls,rm, andcd. You shouldn’t be shocked by seeing those commands. You might even be quite familiar with them. Especially if you know your way around the command line pretty well. I assume you have at least worked (or tried to work) on the command line before. If you’ve never worked on the command line before, I suggest you start by reading about the basics.This article is for those of you who have some experience with the command line and got a taste of the basic commands. For those who feel like it’s time to dive deeper into the possibilities of working with the command line. I’ve listed six commands for you in this article and included some tips at the bottom as well.
1. wget
On Unix-like operating systems, the
wgetcommand downloads files served with HTTP, HTTPS, or FTP over a network. By default, it is included in all self-respecting Linux distributions.The most simple way to use
wgetis to provide it with the location of a file to download over HTTP. Downloading the filehttp://website.com/static/images/header.jpgwithwgetcan be done by the following command:wget http://website.com/static/images/header.jpgOne great thing about wget is that it’s non-interactive, which means that it can run in the background while the user is not logged on. This allows you to start a retrieval and disconnect from the system, letting
wgetfinish the work.
2. scp
Ever had a problem where you needed to have a file from a remote server on your local machine? Getting a file that a user uploaded that caused some trouble, for example.
You could download this file via the command line using the
scpcommand. Scp is short for secure copy. But what’s even more important is that it’s a remote secure copy. This command is similar to thecpcommand that you probably already know, but either the source or the target is on another system.The following command copies the file
foobar.txtfrom a remote server to a local directory.scp username@remotehost.com:/path/to/foobar.txt /some/local/directoryBut
scpcan also be used to copy a file from a local directory to a remote server.scp /some/local/directory/foobar.txt username@remotehost.com:/destination/path/The same can be done with directories using the
-roption, which copies entire directories recursively.
3. ssh-keygen
The
ssh-keygencommand is used to generate a new SSH key pair. The public SSH key that is generated by this command can be used in Gitlab or Bitbucket to establish a secure connection.Once you’ve added your SSH key to either Gitlab or Bitbucket, you won’t be prompted for a password every time you try to push your files to a remote branch.
To generate an SSH key pair, use the following command:
ssh-keygen -t ed25519Note that in the example above, we used the ED25519 signing algorithm. While ED25519 is considered best practice, you should always do some research on the different available signing algorithms.
Generating the SSH key pair and setting it up correctly in Gitlab or Bitbucket costs you ten minutes at a maximum (probably more like three) but will be totally worth it!
4. chmod
In Unix and Unix-like operating systems, chmod is the command and system call which is used to change the access permissions of file system objects (files and directories).
According to Wikipedia, this is the definition of the
chmodcommand. We’ve all been in a situation where a server didn’t have access to a certain file because of a misconfiguration in the file permissions.The
chmodcommand is fairly simple in itself, but giving the right permissions to files and directories is a whole different game.chmod 664 robots.txt chmod 775 public/imagesThe first example gives read and write permission to user and group for the
robots.txtfile. Read permission is granted to others for this file.The second example gives read, write and execute permission to user and group for the
public/imagesfolder. Others are granted read and execute permission for this folder.If you want to know more about setting the right permissions to files and directories, I suggest you read the Wikipedia page about
chmod.
5. tar
The Linux
tarstands for tape archive. It is used for collecting many files into one archive file.Taris the most widely used command to create compressed archive files.Let’s start with how you can create an archive file for a specific directory:
tar -cvf my-archive.tar /path/to/directoryThis command will result in an archive file, called
my-archive.tar, that contains all files of the/path/to/directorywhich is created in the current working directory.Creating the archive file is part one. Part two consists of untarring the archive file, because at some point we want to use the files in the tar file. You can untar the file into a specific directory by typing in the following command:
tar -xvf my-archive.tar -C /home/myfolder/
6. alias
Everyone uses some commands that are a little too long or complex to completely remember. Luckily, you can create an
aliasfor that command so you don’t have to remember the entire command.alias short-command="your custom and very long command here"Though creating an alias this way comes with one problem: This alias is temporary. If you create an alias this way, it will only be available for your current terminal session.
To keep aliases between sessions, you can save them in your user’s shell configuration profile file. This profile file is probably located at either ~/.bashrc or ~/.zshrc if you’re using Bash or ZSH, respectively.
Tip 1: Directing Output
The standard output device is the screen. But sometimes you don’t want to output everything on your screen. In some cases, you probably prefer to output the results of some commands to a file. For logging purposes, for example.
To redirect the output, you can use the >. In the following command, the output of
ls -alis redirected to the filemyfileinstead of the screen.ls -al > myfileI’ve used
lsin this example, but it really could be any command that has some output. To confirm that this worked, you can check themyfilefile.cat myfile
Tip 2: Combining Commands
It’s possible to run two or more commands at once. The semicolon (;) operator allows you to do this. You can execute multiple commands in succession, regardless of whether each previous command succeeds.
ls -al; pwd;If you want the second command to only run if the first command is successful, separate the commands with the logical AND operator, which is
<em class="ks">&&</em>.mkdir images && cd imagesWe only want to
cdto theimagesfolder if we managed to create that folder.And sometimes you might want to execute a second command only if the first command does not succeed. To do this, we use the logical OR operator, which is
<em class="ks">||</em>.Better Programming
Advice for programmers.
Advanced CLI: Commands You Should Know as a Developer
 1 2