Advanced CLI Commands You Should Know As A Developer
Advanced CLI Commands You Should Know As A Developer
Developer
Advanced commands; get more done
Daan Follow
Jan 5 · 5 min read
No, in this article we won’t go over the basic commands like ls , rm , and cd . 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 wget command 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 wget is to provide it with the location of a file to download
over HTTP. Downloading the file http://website.com/static/images/header.jpg with
wget can be done by the following command:
wget http://website.com/static/images/header.jpg
One 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 wget finish 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 scp command. 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 the cp command that you probably already know, but either the
source or the target is on another system.
The following command copies the file foobar.txt from a remote server to a local
directory.
scp [email protected]:/path/to/foobar.txt /some/local/directory
But scp can also be used to copy a file from a local directory to a remote server.
scp /some/local/directory/foobar.txt
[email protected]:/destination/path/
The same can be done with directories using the -r option, which copies entire
directories recursively.
. . .
3. ssh-keygen
The ssh-keygen command 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.
ssh-keygen -t ed25519
Note 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 chmod command. 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 chmod command is fairly simple in itself, but giving the right permissions to files and
directories is a whole different game.
The first example gives read and write permission to user and group for the robots.txt
The second example gives read, write and execute permission to user and group for the
public/images folder. 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 tar stands for tape archive. It is used for collecting many files into one archive
file. Tar is 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:
This command will result in an archive file, called my-archive.tar , that contains all files
. . .
6. alias
Everyone uses some commands that are a little too long or complex to completely
remember. Luckily, you can create an alias for that command so you don’t have to
remember the entire command.
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.
. . .
To redirect the output, you can use the >. In the following command, the output of ls -
cat myfile
. . .
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 && .
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 || .