6 CLI Teams to Learn for a Newbie Developer

This article is for those who had little experience with the command line and appreciated the capabilities of basic commands; for those who decide that the time has come to explore the possibilities of this tool. At the end of the article there will be 2 useful tips for beginners.

1. wget

On Unix-like operating systems, the wget command is used to download files over the network via HTTP, HTTPS, or FTP. This command is present in all self-respecting Linux distributions.

The easiest way to use wget is to pass it a single argument containing the path to the file we want to load. For example, to download a file

website.com/static/images/header.jpg

we should enter at the command prompt

wget http://website.com/static/images/header.jpg

One of the great features of wget is that it does not need interactive mode to work: it can run in the background, even if the user has not logged in. This will allow you to start retrieving data and then logging out, giving wget the ability to shut down on its own.

2. scp

Have you ever encountered the need to store a copy of a file from a remote server on your local machine? For example, this happens when the user has updated or uploaded his copy of the file to the server and this caused problems.

You can download a copy of the file from the server using the command scp (abbreviated, secure copy – safe copying). But more importantly, this copying is done remotely. Command scp similar to cp, which you may already know, but the source address (from where we copy) and the destination address (where we copy) may belong to different systems.

This command copies the foobar.txt file from the remote server to a local directory:

scp username@remotehost.com:/path/to/foobar.txt /some/local/directory

And vice versa: you can copy data from a local directory to a remote server.

scp /some/local/directory/foobar.txt username@remotehost.com:/destination/path/

You can copy the whole directory using the flag -r for recursively copying the contents of all its files and subdirectories.

3. ssh-keygen

The command is used to generate a pair of SSH keys. The public key generated by this command can be used in Gitlab or Bitbucket to provide a secure connection.
By adding your SSH key there once, you will get rid of the need to enter a password each time you try to push to a remote branch.

To generate a pair of SSH keys, at a command prompt, enter, for example, this:

ssh-keygen -t ed25519

Please note that in the example above we use the ED25519 algorithm for signing. Although this algorithm is considered one of the best, I recommend always considering different options.

Generating a key pair and setting up SSH in Gitlab or Bitbucket will take you a maximum of 10 minutes (and most likely 3 minutes), but it is 100% worth it.

4. chmod

On Unix and Unix-like operating systems, chmod is a command for changing permissions on files and directories.

This is the definition of the chmod command from Wikipedia. Most of us had a situation where the server did not give access to the file due to the lack of certain file system permissions.
On the one hand, the chmod command itself is quite simple, but the correct setting of access rights to files and directories is a completely different matter.

chmod 664 robots.txt
chmod 775 public/images

In the first example, we set the read and write permissions of the file robots.txt for user and group. To all the rest, we give only reading rights.

In the second example, we set the rights to read, write and execute the directory public / images for users and groups. To all the rest we give only read and execute rights.

If you want to know more about file system permissions and setting permissions for files and directories, I advise you to read about chmod on Wikipedia.

5. tar

In Linux, the command tar (Tape ARchive bitstream format) collects multiple files into one.

Let’s start by creating an archive for an arbitrary directory:

tar -cvf my-archive.tar /path/to/directory

The result of this command will be a file named my-archive.tarcontaining all directory files / path / to / directory.

But archiving is only half the battle. Often we need to unzip an archive because we want to use its contents. We can unzip the tar file into an arbitrary directory by typing

tar -xvf my-archive.tar -C /home/myfolder/

6. alias

Everyone, most likely, was faced with the need to reuse too long or complex commands that are difficult to remember. Fortunately, for each such command, you can create an alias and not remember the commands completely.

alias short-command="your custom and very long command here"

However, when using the alias command, one problem arises: the alias exists temporarily. If you create it as shown above, alias will only be available until the end of the current session.

To prevent the alias from dying at the end of the session, you can save it in the shell profile configuration file for the current user. The configuration file is usually stored at ~ / .bashrc or ~ / .zshrcif you use Bash or ZSH respectively.

And now for the tips.

Tip 1: Redirect Output

The default default output device is the screen. But sometimes you don’t want to display all the information on the screen. In some cases, you might want to output the results of some commands to a file, for example, for logging.

To redirect the output, you can use the “>” sign. In the following example, we output the result of the ls -al command not to the screen, but to the myfile file.

ls -al > myfile

In the example above, I used ls, but actually could use any command that displays any information as the result of its work. To verify that this works, you can now output the contents of the file myfile to the screen.

cat myfile

Tip 2: Combine Teams

You can run two or more commands at a time. To do this, you can use the “;” operator as a separator. So you can execute all the commands, regardless of the results of each previous team.

ls -al; pwd;

If you want the second command to be launched only if the first one was successful, then use the logical operation “AND” as the separator, that is, &&

mkdir images && cd images

In the example above, we want to apply the command cd to the catalog images only if it is successfully created.

But sometimes a different task may arise: to launch the second command only if the first did not work successfully. Then use the logical operation “exclusive OR”, that is ||.


Subscribe to our Instagram developer

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *