-
Notifications
You must be signed in to change notification settings - Fork 1
How to Use the Terminal
This tutorial is a brief introduction to the terminal in Linux. The terminal is also known in Linux as the shell, command line, or console. It is a tool by which users can manipulate data and files and execute commands. The terminal is also available in MacOS and in a different form in Windows called the Command Prompt.
Terminals act as interfaces between users and the underlying system. They are included in presumably all distributions of Linux, and over the years, many variants of them came in to existence. All are stripped-down graphical interfaces that are much more functionally adept than window managers and more resource-intensive guided user interfaces (GUIs). Today, three of the most commonly encountered terminals are the C shell (csh), the Bourne shell (sh), and the Bourne-again shell (bash).
Bash is a revamped version of the Bourne shell, hence the name, and it is the default terminal in Ubuntu. Below we provide a quick tutorial on some basic commands that you'll need to know to operate RTXI and use the shell.
- Introduction
- Open the Terminal
- Basic Commands
- List Contents
- Change Directories
- Make a Directory
- Open a File
- Moving Files
- [Copying Files] (#cp)
- Execute a Script
- Deleting Files
- Less Basic Commands
- sudo
- External Sites
- Summary
###Open the Terminal Back to top
In Ubuntu, the terminal can be opened by pressing CTL + ALT + T. Alternatively, it can be opened through the system menu in the GNOME 2 desktop environment. Go to the top menubar and navigate through Applications->Accessories->Terminal.
Once the terminal is open, you'll see something like this:
![New terminal][image2]Note that I have the menubar turned off in my terminal, so if you see a bar at the top that has 'File', 'Edit', etc., that is to not an issue at all.
The text on the screen is the username followed by the host, or system, name. In this case, the username is birdie, and the hostname is Stork. The $ denotes the end of the terminal's prefix and the beginning of the space where users can input commands. The blinking cursor is where text typed in will appear.
Quick Note: Some manuals or online guides prefix commands to enter into the terminal with
$, such as$ ls. DO NOT enter$into the terminal. The$is simply intended to point out to readers that the command is intended to executed in the terminal instead of somewhere else.
###Basic Commands Back to top
Now that the terminal is open, we'll start by using some basic commands.
####List Contents Back to top
Enter:
ls
This command lists the contents of the current directory.
![ls directory][image3]A directory is a folder the system uses to catalog where information is stored. In Linux, the file system (i.e. the layout for organizing all the files) is set so that each user has their own directory but share the underlying Linux architecture. When someone logs in, they log into their own directory. Within their directory, they are free to create whatever files they want and whatever directories they want.
Note that if the results of entering your 'ls' command are different from the one here, that is simply because of changes implemented in the course of using the system.
As said before, ls lists the contents of the directory. There are several options for how they can be listed, such as:
ls -l
Quick Note: To clear the terminal, enter the
clearcommand. It does not delete the input and output of the terminal. You can simply scroll up or down to view the history.
All the options can be viewed by executing (i.e. typing in and hitting ENTER) man ls. man is a command that opens up the manual for a command and follows the syntax man COMMAND.
To scroll down, hit ENTER repeatedly. To exit, simply type q to quit.
####Change Directories Back to top
Now that we've seen the directory contents, let's move to another directory. Enter:
cd Documents
Quick Note: Most terminals have an autocomplete functionality. If you type the beginning of the name of a file or a command, hit
TAB. If there are no redundancies where several names start with the keys entered, Bash will complete it. If there are redundancies, you can hitTABagain to have the terminal list what they are.
Notice that the prefix that begins each line you type has changed from username@hostname:~$ to username@hostname:~/modules$. It is simply displaying the current directory.
To return to the original directory, enter:
cd ../
This command moves the user to the parent directory. To move up two directories, you can enter cd ../../, and for three, follow the pattern. Similarly, when changing to a subdirectory, it is possible to go as many subdirectories deep as possible. Simply enter cd subdir1/subdir2/subdir3 and continue the pattern until you reach the directory you want.
Quick Note: If you find yourself in somewhere unintended, simply enter
cd, and it will take you back to the root of your home directory, the location available when the terminal was originally opened.
####Make a Directory Back to top
Return to the Documents/ directory. If you are not there right now enter:
cd ~/Documents/
The ~ is a symbol Bash uses to denote the root of the user's home directory.
Now, let's make a directory with the mkdir command. Enter:
mkdir bash_intro
This command creates a new directory within Documents called "bash_intro". You can now move inside this directory with `cd`.
Quick Note:
mkdirtakes as input the name of the directory to be created. To include white spaces in the name, prefix them with\. Otherwise, Bash will not realize that the space-separated words are part of a name, and an error results. For example, to make a directory called "New Directory", runmkdir New\ Directory.
Quick Note: You can combine commands with directory specifiers. For example, you can use
mkdir ../to make a directory in the parent directory. It is best to avoid doing this so that you avoid careless errors. The commands listed so far are innocuous enough, but mistakenly deleting files in the wrong directory is extremely undesirable and entirely avoidable.
####Open a File Back to top
Move inside bash_intro. In case you forgot it, use the command cd ~/Documents/bash_intro. You'll notice that there are no files in the directory. A simple ls confirms this. Let's add a file to it called "myfile.txt"
touch myfile.txt
This command creates an empty file called "myfile.txt".
Open the file in an editor in order to modify it's contents. There are many command line editors available, such as nano, vim, and emacs, and there are also more feature-filled one available, such as gedit and LibreOffice Writer. Users are encouraged to try several of them to see which is most comfortable to use, but for this tutorial, we will use gedit due to its similarity with Notepad and other editors more often used in Windows and Mac environments. Run:
gedit myfile.txt
gedit will now open and allow you to modify myfile.txt. In gedit, type the following:
echo "This is easy"
Save the file and close gedit. You have now modified myfile.txt. The text entered is a command that will be explained in the Execute a Script.
Quick Note: If you look at the terminal while gedit is open, you'll notice that it is not possible to execute a command. Hitting
ENTERwill simply creates a new blank line. This is because the command gedit is still running. The terminal will be unavailable for use as long as gedit is open, so either close gedit itself or enterCTL+Cin the terminal. Be careful if you enterCTL+C, as it will force close gedit whether the data is saved or not.
#### Moving Files
Back to top
Once files are created within a directory, it is possible to move them from one directory to another. It is also possible to move entire directories from within one to another. Create a new directory within bash_intro and call it "sub_directory". Move into the bash_intro directory and run:
mkdir sub_directory
Check with ls to see the new directory. The blue color tells the user that it is a directory and not a text-containing file. Now, lets move myfile.txt to sub_directory. Do:
mv myfile.txt sub_directory
This moves myfile.txt into sub_directory. In this case, we are moving just one file, but it is possible to move multiple files as well. Simply check the man pages for mv (i.e. run man mv) to learn how.
Change directory to sub_directory. Now we can use mv to change a filename. Run:
mv myfile.txt myfile
This changes the name of myfile.txt to myfile. Strictly speaking, we are moving my_file.txt into another file in the same directory called myfile, but the effect is the same. You can check for yourself that the contents are the same via gedit or some other text editor. Note that though this example uses a file, the syntax applies to entire directories as well.
####Copying Files Back to top
In addition to moving files, it is possible to copy them as well. First, copy one file and store in in the same directory under a new filename. Run:
cp myfile myfile2
This copies myfile into another file called myfile2 and stores it in the same directory.
####Execute a Script Back to top
Now we will execute a script. There are several ways to do it. We will focus on two. First, create an executable script, which in this case is going to be myfile. Run:
chmod +x myfile
This allow the user to execute the contents of the file by giving it executable permission. Notice that if the commands saved in the file are incorrect or are not commands at all, attempts to execute myfile will produce errors.
To see the results, type ls. Note how the name of the file has turned green. This signifies to the user that the file contains an executable script. If one wishes to de-specify an script as executable, run chmod -x filename. The name of the file will change back to white, also.
Now, execute the file. Run:
bash myfile
-OR-
```` ./myfile ```` ![./ execute][image19]
Both commands above are equivalent. The output should be a message saying, "This is easy!"
The output is produced by the echo function. When given an argument, echo outputs that argument as a string to the terminal. The text written previously, echo "This is easy!", is then easily understood.
Executables are used to run scripts and applications. When previously opening myfile.txt in gedit, the gedit command is an executable that runs gedit and opens the file that follows the command. You may wonder how this is possible given that the gedit source code is not in the current directory. In Linux and other OSes, the PATH variable specifies where to look for the source code for a command to be executed. PATH contains a list of locations in the filesystem where source code is kept. When executing gedit for instance, the terminal locates the gedit files and then runs the application. The same thing occurs when the users starts RTXI by executing rtxi. It is possible for the user to modify the PATH variable, but that is beyond the scope of this tutorial.
####Deleting Files Back to top
To delete files, use the rm command. Remove myfile2 from the directory.
rm myfile2
Use ls to confirm that the file has been deleted. Now, let's remove a directory. First, copy myfile into the parent directory (cp myfile ../) and move to the parent directory. To delete sub_directory, run
rm -r sub_directory
The -r option for rm means that the rm command will be executed recursively. That means that starting from all the subdirectories of sub_directory, each file will be deleted. The recursive element is necessary because not deleting files within a directory leaves allocated memory that is inaccessible to the system because the directory point to the files is no longer available.
###Less Basic Commands Back to top
These commands are more involved than the ones listed above. The basic commands are all that are really needed to run RTXI and move around the filesystem, but occasionally more complex commands are needed.
####sudo sudo, which stands for substitute user do, is a tool used to execute commands with root privileges. In Linux, there are user accounts and a root account. User accounts can do things within their permissions, but root accounts require no permissions. They can execute and access anything. From a security standpoint this is problematic. People using root accounts have no safeguards against accidental deletion of vital files, and malicious code can access and damage the entire filesystem. The solution to this is to have a root account that is not accessible to ordinary users. Rather, users, when they need root privileges to run things, can emulate root (substitute user) to run commands (do). This method follows the principle of only allowing access to precisely what is needed and nothing more.
When compiling and installing modules and RTXI itself, root privileges are needed. To execute a command as root, prefix it with sudo. For example, one can run sudo make install to compile and install a program in the current directory. The user is then prompted for his/her password. After entering it, the command is run. If a command has been run via sudo recently, the system may or may not prompt the user for their password.
In general, do not indiscriminately run commands with root privileges. Within RTXI, the times in which that is appropriate are delineated within our tutorials and manuals.
###External Sites Back to top
Here are some external sites with good information about the commands explained here and additional ones as well. We welcome feedback regarding the helpfulness of this tutorial and will alter it as needed as quickly as possible, but if explanations here are insufficient, it is likely quicker to peruse additional tutorials or simply use a search engine to find anything needed.
- The Bash reference manual, which is not for the faint-hearted.
- This tutorial on the Ubuntu Community Forum is more condensed but covers more commands and material.
###Summary Back to top
Contact us at info@rtxi.org if you have suggestions for this tutorial.