This quick tutorial is for developers or system administrators new to Linux or FreeBSD. This is a quick run through of basic commands that you will need to know to get going setting up and administrating a Linux or FreeBSD server. We will review 9 of the most frequently used commands in the shell: ls, cp, mkdir, rm, cd, cat, echo, exit, and man. This tutorial is for beginners and someone who has never used a shell before.
You will need a server with Linux or FreeBSD installed. If you don’t have one, you can get an affordable server from Atlantic.Net. In addition, you will need to be logged into the server and at the shell prompt. Typically, this involves using a ssh client to connect and login to the server. We will start assuming you are logged into the server. We will be using Ubuntu Linux for this demonstration, but any Linux variant will do.
To begin, you should be at a prompt that looks like this:
Let’s understand what is going on here. In the example above, the username I chose for the account is “demo” and the name of my server is “demoserver”. So its showing [email protected] at the command prompt. Depending on your version of Linux, it may show something different. We are now at the command prompt.
How to use the built-in manual for Linux or FreeBSD
The first command we learn is the “man” command, short for manual. This is the first point of reference when trying to learn a new command in Linux. The manual pages are in-depth and show all the different features and “flags” (or options) available with each command. Let’s look at the man page (one of the pages in the built-in manual) for the next command we are going to learn called “echo” by typing “man echo” into the command prompt:
You will see a manual page that describes the echo command in great detail display on your screen. You can press spacebar to continue reading the whole article, or press q to quit and return to the shell (the command options are written at the bottom of the page).
Tip: You can use the “-k” flag with the man command to search for keywords if you don’t know the actual command. For example, typing “man -k echo” shows several other commands that have echo in them. It is useful to try and find the command with the -k flag, then actually type in “man” to learn how to use it.
Using echo to display text and create a file.
Next, we will learn the echo command. Echo simply repeats whatever you send to it. For example, type the following into the shell prompt:
and you should see Hello repeated back in your terminal, as seen below:
Using echo, creating a file using echo, and using cat to view it
Great, you’ve learned how to use the echo command! In Linux, you can use the > and >> operators to forward output somewhere else. In our case, we will use them to create a file. We use the > command to create a file, and the >> command to append to an existing file.
We will now use the > operator to create a file called test:
echo "Hello" > test
Now, we will use the >> operator to append some text to the file named test:
echo "World" >> test
We will now use the cat command. Cat allows us to view what is in a file.
Your shell should have returned your Hello World text as seen below:
How to see the contents of your directory using ls
Great! We’ve now learned how to use echo to create a file using > and >>, and we’ve learned how to view it using cat. In Linux, everyone gets their own directory that houses their private files. We created a file in our directory called test. Now, we will learn how to see what files are in our directory using the ls command. Type:
and you will see your file named test. We can get a more descriptive view of the directory using the “-l” flag as follows:
Now, you will see the test file with many other fields. Since this is a basic tutorial, we won’t cover those here, but you can always use “man ls” to see the options and what they mean.
Here’s what you should have seen using the ls command:
Using cp to copy a file
We’re making progress! You’ve now learned how to create a file, and list what is in your directory. Now, let us learn how to use the cp command to copy files. Type:
cp test newtest
This will make a duplicate file called “newtest” that has the same contents as “test”. You can verify this by using the cat command to see both files. In addition, if you use the ls command, you will now see two files one called “test” and one called “newtest”. If you follow this example, your output should look similar to what we have below:
Learning how to remove a file using rm
Now, let’s learn how to delete a file. We will delete our newtest file by using the rm command as follows:
The newtest file is now gone, we can verify using the ls command. Give it a try, and your shell should look
something like this:
How to create a directory (or subdirectory) and use cd (change directory)
In order to organize files, we sometimes create directories, or subdirectory (directories within directories). We’ll make a directory by using the mkdir command. Let’s make a directory by typing:
You have now created a directory. You can use the “ls -l” command to see it. Note the “d” in the file permissions at the beginning of the listing, this denotes “directory”. Note that our “test” file doesn’t have that because it is not a directory. Now, let’s create a file called groceries in that directory that contains the word apple by typing the following:
echo "apple" > shopping/groceries
We have now created a file in the shopping directory. In order to see the file, we need to change directories so we are in the shopping directory. Let’s do that with the “cd” command by typing:
You can now use the ls command to see the groceries file, and using cat to view it. To move back up to the previous directory, you can use the command “cd ..”. Use ls to see we are back to the original directory.
Give it a try and you should see the following:
That’s it! You have learned some basic functions and navigation for the Linux (or FreeBSD) shell. These are the most popular commands you will use on a day-to-day basis as you use the shell to administer your server. We hope you found this tutorial useful, and enjoy learning how to use a Linux or FreeBSD server.
BONUS: clear, reset and exit
The clear command allows you to clear the screen quickly and reset it. Sometimes you will want a clean screen and you can use the clear command:
It will simply clear to screen. If, by chance your screen starts displaying strange characters, you can use the reset command to reset the display with the proper font:
Finally, when you are finished and want to exit the shell, simply type exit to tell the Linux server to shutdown your session: