Published - Sun, 14 May 2023

Common SSH Commands that you should know

Common SSH Commands that you should know

SSH CommandExplanation
lsShow directory contents (list the names of files).
cdChange Directory.
mkdirCreate a new folder (directory).
touchCreate a new file.
rmRemove a file.
catShow contents of a file.
pwdShow current directory (full path to where you are right now).
cpCopy file/folder.
mvMove file/folder.
grepSearch for a specific phrase in file/lines.
findSearch files and directories.
vi/nanoText editors.
historyShow last 50 used commands.
clearClear the terminal screen.
tarCreate & Unpack compressed archives.
wgetDownload files from the internet.
duGet file size.



1. ls Command

This SSH command is used to list all files and directories. After entering ls, you will see an output that looks like this:

The output of ls command

There are also a few useful options that you can combine with it:

  • -l —  displays the details of the files, such as size, modified date and time, the owner, and the permissions.
  • -a —  shows hidden files and directories.

2. cd Command

cd (Change Directory) is the command that we use to jump between directories. It’s a pretty simple command — just type cd followed by the name of the directory:

cd [directory]

As such, if you want to enter the home directory of your server, you can type:

cd home

You may also write the full path of a certain directory if it is a few levels deep. For instance:

cd home/TestDirectory/AnotherDirectory

You are now in the AnotherDirectory.

To go back one level, you can simply enter “..” (two dots) after cd command. What’s cool, you can go back further by adding another two-dots and separating them with a forward slash (/):

cd ../..

By entering this line, you are in the home directory again.

3. mkdir Command

You can use mkdir (Make Directory) command to create a directory. This is the syntax:

mkdir [folder name]

Let’s assume you want to create a new folder named “myfolder”. You will need to type:

mkdir myfolder

4. touch Command

This SSH command is used to create a new file. Here is the syntax:

touch [file name]

If you want to create a .txt file named “myfile”, this is what you need to write:

touch myfile.txt.

The file extension could be anything you want. You can even create a file with no extension at all.

5. rm Command

rm command removes a chosen file or directory. To delete a file, enter:

rm [file name]

For instance, if you want to remove myfile.txt, simply execute:

rm myfile.txt

To delete a folder, you need to use the -r option to remove all the files and subfolders inside it:

rm -r home/hostinger/myfolder

6. cat Command

We use cat command to display the content of a file. Below is the syntax:

cat [file name]

It also allows you to create a new file by merging multiple files. For example:

cat info.txt info2.txt > mergedinfo.text

By executing this line, the content of info.txt and info2.txt will be saved into mergedinfo.txt.

7. pwd Command

pwd is a simple command that outputs the full path of your working directory. Once entered, you should see a result like this:

home/user/public_html

pwd command can come in really handy when you are accessing your site hosting account through SSH. Oftentimes, shared servers don’t tell you the directory you are in.

8. cp Command

This SSH command will copy files and folders. The syntax is:

cp [options] [source] [destination]

[source] is the file or folder you want to copy and [destination] is the duplicate.

Let’s say you have myfile.txt in your working directory, and you want to make a copy of it. The syntax would be:

cp myfile.txt myfile2.txt

If you want to make a copy in a different folder, run the following command:

cp /home/hostinger/myfile.txt /home/etc/

Be careful when writing the name of the destination. If you provide two file names, the cp command will copy the content of the source file into the destination file. Thus, the destination file will be overwritten without any warning. However, if the destination file doesn’t exist, then the command will create a new file.

[options] is not mandatory. However, there are several options that you can use:

  • -f — if you don’t have writing permission to the destination file, it’ll be deleted and the command will create a new file
  • -u — copy the source file if it is newer than the destination file.
  • -n — will not overwrite an existing file.
  • -a — archive the files.

Unlike duplicating files, copying folders requires you to use the -R (recursive) option. The option allows all folders and files inside it to be copied.

cp -R /home/hostinger/myfolder /home/etc/

9. mv Command

This command works similarly to cp. However, mv command will move the file or folder instead of copying it. This is the syntax:

mv [source] [destination]

Let’s say we want to move myfile.txt from /home/hostinger/ftp to /home/hostinger/myfolder/. The command should be:

mv /home/hostinger/ftp/myfile.txt /home/hostinger/myfolder

Unlike cp command, you don’t need the -R option to move a folder. For instance:

mv /home/hostinger/ftp/ /home/hostinger/myfolder/

This will automatically move all files and subfolders inside ftp to myfolder.

10. grep Command

grep command looks for a given string in files. For example:

grep 'line' info.txt

The above command would search for ‘line’ in a file named “info.txt”. What’s great, the command will print the entire line that contains the matched text.

The results of grep SSH command

Keep in mind that this command is case sensitive. If you want to ignore letter cases, use -i option.

11. find Command

We enter find SSH command to search for a file or files that meet the given criteria (name, size, file type, etc). The following is the basic syntax:

find [starting directory] [options] [search term]

[starting directory] is where you would like to start your search process. There are three main choices:

  • / (slash) — search the whole system
  • . (dot) — search the working directory
  • ~ (tilde) — search the home directory

[options] is an additional argument that you can use to refine your search. Some of the most popular options are:

  • -name — look for files based on their names
  • -user — search for files that belong to a given user
  • -size — look for files based on their sizes

[search term] is the keyword or number that you use to search for files.

Take a look at this example:

find . -name “index”

This command will return any files that have the word “index” on their names. And since we use “.” (dot), the command will only search the working directory.

We also have a great tutorial that provides an in-depth explanation about this SSH command.

12. vi/nano Command

Vi and Nano are two popular text editors that you can use in the command line. To open a file using Vi or Nano, you just need to enter:

vi [file name]

or

nano [file name]

If the specified file doesn’t exist, both text editors will automatically create it for you.

Unfortunately, some Linux distributions don’t offer Nano by default. Don’t worry, you can read our guide on how to install and use Nano.

13. history Command

This one is used to display the last used commands. You need to enter a number to limit the displayed results. For example:

history 20

As you probably guess, the example will show the 20 most recently entered commands.

14. clear Command

The function of clear command is simple — it clears all text from the terminal screen.

15. tar Command

tar is an SSH command that creates or extracts .tar.gz files. It is very popular because most third-party software binaries are in the .tar.gz format.

To archive a folder in .tar.gz format, use the following command:

tar cvzf ArchiveName.tar.gz /path/to/directory

To unpack a .tar.gz file, enter this command:

tar xvzf FileName.tar.gz

Notice that both commands use different four-character options — cvzf and xvzf.  Each letter represents a specific instruction:

  • x tells tar to extract files
  • c tells tar to create an archive
  • v stands for verbose. The option tells tar to display all file names that are processed by the command.
  • z instructs tar to uncompress the archive
  • f tells tar that you are supplying the name of the archive

16. wget Command

wget is used to download files from the internet. For example, to fetch a file from a website and store it in our current directory, we’ll use:

wget http://fileurl/filename.ext

If you want to download multiple files, put all URLs into a file and use the -i option.

Let’s say the file containing the links is called downloads.txt. The command will look like this:

wget -i downloads.txt

17. du Command

You can use du (Disk Usage) command to view the size of files and folders in a specified directory:

du [directory path]

Unfortunately, the summary will show disk block numbers instead of bytes, kilobytes, and megabytes. Therefore, to show it in a human-readable format, you need to insert the -h option after du command:

du -h /home

The results will be more understandable:

The results of du command

Check out this article to read more about du command.

Created by

Edward Muss

I studied BSc. in Mechanical Engineering from the University of Nairobi. While in campus I developed the passion for software Development, Graphic Design Video Editing and Photography. I have loved working as a developer and I enjoy doing anything related to tech. I show people that they don't have to be a straight (A) student or a genius to learn to tech. I break down complex concepts by showing you how to implement them in project-based courses and tutorials.



I studied BSc. in Mechanical Engineering from the University of Nairobi. While in campus I developed the passion for software Development, Graphic Design Video Editing and Photography.

I have loved working as a developer and I enjoy doing anything related to tech.

I show people that they don't have to be a straight (A) student or a genius to learn to tech. I break down complex concepts by showing you how to implement them in project-based courses and tutorials.

View profile

Comments (0)

Search
Popular categories
Latest blogs
Common SSH Commands that you should know
Common SSH Commands that you should know
SSH CommandExplanationlsShow directory contents (list the names of files).cdChange Directory.mkdirCreate a new folder (directory).touchCreate a new file.rmRemove a file.catShow contents of a file.pwdShow current directory (full path to where you are right now).cpCopy file/folder.mvMove file/folder.grepSearch for a specific phrase in file/lines.findSearch files and directories.vi/nanoText editors.historyShow last 50 used commands.clearClear the terminal screen.tarCreate & Unpack compressed archives.wgetDownload files from the internet.duGet file size.1. ls Command This SSH command is used to list all files and directories. After entering ls, you will see an output that looks like this: There are also a few useful options that you can combine with it: -l —  displays the details of the files, such as size, modified date and time, the owner, and the permissions.-a —  shows hidden files and directories. 2. cd Command cd (Change Directory) is the command that we use to jump between directories. It’s a pretty simple command — just type cd followed by the name of the directory: cd [directory] As such, if you want to enter the home directory of your server, you can type: cd home You may also write the full path of a certain directory if it is a few levels deep. For instance: cd home/TestDirectory/AnotherDirectory You are now in the AnotherDirectory. To go back one level, you can simply enter “..” (two dots) after cd command. What’s cool, you can go back further by adding another two-dots and separating them with a forward slash (/): cd ../.. By entering this line, you are in the home directory again. 3. mkdir Command You can use mkdir (Make Directory) command to create a directory. This is the syntax: mkdir [folder name] Let’s assume you want to create a new folder named “myfolder”. You will need to type: mkdir myfolder 4. touch Command This SSH command is used to create a new file. Here is the syntax: touch [file name] If you want to create a .txt file named “myfile”, this is what you need to write: touch myfile.txt. The file extension could be anything you want. You can even create a file with no extension at all. 5. rm Command rm command removes a chosen file or directory. To delete a file, enter: rm [file name] For instance, if you want to remove myfile.txt, simply execute: rm myfile.txt To delete a folder, you need to use the -r option to remove all the files and subfolders inside it: rm -r home/hostinger/myfolder 6. cat Command We use cat command to display the content of a file. Below is the syntax: cat [file name] It also allows you to create a new file by merging multiple files. For example: cat info.txt info2.txt > mergedinfo.text By executing this line, the content of info.txt and info2.txt will be saved into mergedinfo.txt. 7. pwd Command pwd is a simple command that outputs the full path of your working directory. Once entered, you should see a result like this: home/user/public_html pwd command can come in really handy when you are accessing your site hosting account through SSH. Oftentimes, shared servers don’t tell you the directory you are in. 8. cp Command This SSH command will copy files and folders. The syntax is: cp [options] [source] [destination] [source] is the file or folder you want to copy and [destination] is the duplicate. Let’s say you have myfile.txt in your working directory, and you want to make a copy of it. The syntax would be: cp myfile.txt myfile2.txt If you want to make a copy in a different folder, run the following command: cp /home/hostinger/myfile.txt /home/etc/ Be careful when writing the name of the destination. If you provide two file names, the cp command will copy the content of the source file into the destination file. Thus, the destination file will be overwritten without any warning. However, if the destination file doesn’t exist, then the command will create a new file. [options] is not mandatory. However, there are several options that you can use: -f — if you don’t have writing permission to the destination file, it’ll be deleted and the command will create a new file-u — copy the source file if it is newer than the destination file.-n — will not overwrite an existing file.-a — archive the files. Unlike duplicating files, copying folders requires you to use the -R (recursive) option. The option allows all folders and files inside it to be copied. cp -R /home/hostinger/myfolder /home/etc/ 9. mv Command This command works similarly to cp. However, mv command will move the file or folder instead of copying it. This is the syntax: mv [source] [destination] Let’s say we want to move myfile.txt from /home/hostinger/ftp to /home/hostinger/myfolder/. The command should be: mv /home/hostinger/ftp/myfile.txt /home/hostinger/myfolder Unlike cp command, you don’t need the -R option to move a folder. For instance: mv /home/hostinger/ftp/ /home/hostinger/myfolder/ This will automatically move all files and subfolders inside ftp to myfolder. 10. grep Command grep command looks for a given string in files. For example: grep 'line' info.txt The above command would search for ‘line’ in a file named “info.txt”. What’s great, the command will print the entire line that contains the matched text. Keep in mind that this command is case sensitive. If you want to ignore letter cases, use -i option. 11. find Command We enter find SSH command to search for a file or files that meet the given criteria (name, size, file type, etc). The following is the basic syntax: find [starting directory] [options] [search term] [starting directory] is where you would like to start your search process. There are three main choices: / (slash) — search the whole system. (dot) — search the working directory~ (tilde) — search the home directory [options] is an additional argument that you can use to refine your search. Some of the most popular options are: -name — look for files based on their names-user — search for files that belong to a given user-size — look for files based on their sizes [search term] is the keyword or number that you use to search for files. Take a look at this example: find . -name “index” This command will return any files that have the word “index” on their names. And since we use “.” (dot), the command will only search the working directory. We also have a great tutorial that provides an in-depth explanation about this SSH command. 12. vi/nano Command Vi and Nano are two popular text editors that you can use in the command line. To open a file using Vi or Nano, you just need to enter: vi [file name] or nano [file name] If the specified file doesn’t exist, both text editors will automatically create it for you. Unfortunately, some Linux distributions don’t offer Nano by default. Don’t worry, you can read our guide on how to install and use Nano. 13. history Command This one is used to display the last used commands. You need to enter a number to limit the displayed results. For example: history 20 As you probably guess, the example will show the 20 most recently entered commands. 14. clear Command The function of clear command is simple — it clears all text from the terminal screen. 15. tar Command tar is an SSH command that creates or extracts .tar.gz files. It is very popular because most third-party software binaries are in the .tar.gz format. To archive a folder in .tar.gz format, use the following command: tar cvzf ArchiveName.tar.gz /path/to/directory To unpack a .tar.gz file, enter this command: tar xvzf FileName.tar.gz Notice that both commands use different four-character options — cvzf and xvzf.  Each letter represents a specific instruction: x tells tar to extract filesc tells tar to create an archivev stands for verbose. The option tells tar to display all file names that are processed by the command.z instructs tar to uncompress the archivef tells tar that you are supplying the name of the archive 16. wget Command wget is used to download files from the internet. For example, to fetch a file from a website and store it in our current directory, we’ll use: wget http://fileurl/filename.ext If you want to download multiple files, put all URLs into a file and use the -i option. Let’s say the file containing the links is called downloads.txt. The command will look like this: wget -i downloads.txt 17. du Command You can use du (Disk Usage) command to view the size of files and folders in a specified directory: du [directory path] Unfortunately, the summary will show disk block numbers instead of bytes, kilobytes, and megabytes. Therefore, to show it in a human-readable format, you need to insert the -h option after du command: du -h /home The results will be more understandable: Check out this article to read more about du command.

Sun, 14 May 2023

What happens when you type 'https://www.google.com'​ in your browser and press 'Enter'
What happens when you type 'https://www.google.com'​ in your browser and press 'Enter'
We use internet all the time for all sorts of purposes, and it has become like a second nature to browse all day. So much so that we’re most of the time content with just knowing that our browser works and does what it’s asked. But it’s not magic (or is it?), and the web pages we see in that rectangle machine must come from somewhere. So how is it all happening? What is happening under the hood between the moment we enter a URL (Uniform Resource Locator) in the search bar and the moment we receive the content of the desired page? The client-server model Before diving into the details of the web infrastructure, it’s important to understand the client-server model. On the World Wide Web, the network is organized between clients, which request data, and servers, which stores the data and manages most of the processing of this data. For example, a browser is considered a client, and a server would be the computer program serving data to that client. Server is also a term describing the physical machine on which the server program is running. Each website, application, service can have multiple servers working behind the scenes to perform the processes needed by the client(s). In general, physical servers are regrouped in server farms, or data centers. Simple Diagram showing the web stack There are many other layers between the client and the server in a client-server model, let’s break is down. The DNS request When we type the URL https://www.google.com into our browser (Google, Firefox, Safari, or any browser) and press ‘Enter’, the first thing that the browser is going to do is break down the URL in pieces. The browser is going to consider the www.google.com part first, which is a domain name. If the browser doesn’t know that domain name (it’s not stored in its cache), it is going to ask the Domain Name System for the IP address corresponding to this particular domain name. This IP (Internet Protocol) address is in fact the unique address of the main server hosting the website (the data, the text files, the code, the services…) www.google.com. It’s a suite of four numbers ranging from 0 to 255, separated by dots. The reason we have domain names in the first place is because humans remember words better than numbers. Thankfully, the DNS is here for us to remember the IP of each domain. The DNS request first goes through the resolver. The resolver is usually our Internet Service Provider, and if it doesn’t find the IP in its cache, it’s going to request the root server. The root server knows where the TLD (Top-Level Domain) server is. In our case, the top-level domain is .com. Other types of TLD are .net, .fr, etc. If the TLD server doesn’t know the IP, it points the resolver to the Authoritative Name Servers for the domain name. Usually, there is more than one name server attached to one domain name. But any of those name servers can give the IP for the domain name they are attached to. Now the resolver has the IP address(for example, 54.172.4.191), and can send it back to the browser which will perform its request to the corresponding server. Protocols: TCP/IP We mentioned how domain names actually represent IP addresses, but IP is not the only type of protocol use by the Internet. The Internet Protocol Suite is often referred to as TCP/IP (TCP stand for Transmission Control Protocol), and it also contains other types of protocols. It’s a set of rules that define how servers and clients interact over the network, and how data should be transferred, broken into packets, received, etc. The Firewall To protect themselves from hackers and attacks, servers are often equipped with a firewall. A firewall is a software that sets rules about what can enter or leave a part of a network. In the case of our example, when the browser asks for the website at the address 54.172.4.191, that request has be processed by a firewall which will decide if it’s safe, or if it’s a threat to the server’s security. The browser itself can also be equipped with a firewall to detect if the IP given by the DNS request is a potential malicious agent. Security and Encryption: HTTPS/SSL Now that the browser has the IP address, it is going to take care of the other part of the URL, the https:// part. HTTPS stands for HyperText Transfer Protocol Secure, and is a secure version of the regular HTTP. This transfer protocol defines different types of requests and responses served to clients and servers over a network. In other terms, it’s the main way to transfer data between a browser and a website. HTTP and HTTPS requests include GET, POST, PUT, and others. The HTTPS requests and responses are encrypted, which ensure the users that their data can’t be stolen or used by third-parties. For example, if we put our credit card information in a website that uses HTTPS, we are guaranteed that this info is not going to be stored in plain text somewhere accessible to anybody. Another key component in securing websites is the SSL certificate. SSL stands for Secure Sockets Layer (also known as TSL, Transport Layer Security). The certificate needs be issued from a trusted Certificate Authority, like the famous Let’s Encrypt for example, which gives free SSL certificates. When a website has this certificate, we’re able to see a little lock icon next to the website name in the search bar. On some browsers and with certain types of SSL certificates, the bar turns green. Load-balancer As we mentioned earlier, websites live on servers. For most website where the traffic is consequent, it would be impossible to be hosted on a single server. Plus, it would create a Single Point of Failure (SPOF), because it would only need one attack on said server to take the whole site down. As needs for higher availability and security rises, websites started augmenting the number of servers they have, organizing them in clusters, and using load-balancers. A load-balancer is a software program that distribute network requests between several servers, following a load-balancing algorithm. HAproxy is a very famous load-balancer, and example of algorithms that we can use are the round-robin, which distributes the requests alternating between all the servers evenly and consequentially, or the least-connection, which distributes requests depending on the current server loads. The Web server Once the requests have been evenly distributed to the servers, they will be processed by one or more web servers. A web server is a software program that serves static content, like simple HTML pages, images or plain text files. Examples of web servers are Nginx or Apache. The web server is responsible for finding where the static content corresponding to the address asked for is living, and for serving it as an HTTP, or HTTPS response. The Application server Having a web server is the basis of any web page. But most sites don’t just want a static page where no interaction is happening, and most websites are dynamic. That means that it’s possible to interact with the site, save information into it, log in with a user name and a password, etc. This is made possible by the use of one or more application servers. These are software programs responsible for operating applications, communicate with databases and manage user information, among other things. they work behind web servers and will be able to serve a dynamic application using the static content from the web server. The Database The last step in our web infrastructure is the Data Base Management System (DBMS). A database is a collection of data, and the DBMS is the program that is going to interact with the database and retrieve, add, modify data in it. There are several types of database models. The two main ones are relational databases, and non-relational databases. A relational database can be seen as a collection of tables representing objects, where each column is an attribute and each row is an instance of that object. We can perform SQL (Structured Query Language) queries on those databases. MySQL and PostgreSQL are two popular relational databases. A non-relational database can have many forms, as the data inserted in it doesn’t have to follow a particular schema. They are also called NoSQL databases. A web stack has many layers, and we touched just the surface of it. When we type a URL in a browser, it takes only microseconds for all the agents we talked about to form a response and serve it to the client. Even knowing what is happening behind the curtain, it is still pretty magical to see it happening before our eyes. Conclusion I hope this articles has enriched your knowledge of understanding the behind the scenes of what happens when you visit a webpage.

Tue, 02 May 2023

All blogs