The Writing Confederation
Computers, anime, and writing – A confederation of topics
The Comprehensive Terminal Guide
Posted by on May 18, 2011
A few posts back–in my post Installing Cygwin–I went over some basic terminal commands. Lately I have been using Ubuntu and the terminal much more than often, and because of that I felt it would be helpful to go over, in extensive detail, terminal usage. The first thing, then, is to go over the basics I’ve covered in the past:
- ls. This is the basic command for displaying the contents of the current directory, and will print the names of the files and folders in the current directory to the screen. The syntax for this command is as follows:
ls -[options] [file]
Where [options] is replaced with parameters for the command, and [file] can be replaced with the name of a specific file. Note the hyphen before [options]; the hyphen is not to be confused with the dash, which is two hyphens: — , and is required if additional parameters are being supplied to the ls command. A hyphen is not required to precede the name of a file if it is supplied. Both additional parameters are optional and one can be included without the other or not at all. If a parameter is supplied for [file] the ls command will return the name of the file if it exists, and an error if it does not. - ll. ll is simply an alias for ls -l. Note that the -l is one of the many parameters that the ls command can be supplied with. ls -l means list the contents of the current directory in the long format, and therefore that is what ll does. In other words, the ll command prints the contents of the directory in greater detail. This greater detail includes permissions for the file or directory, the owner, group it belongs to, and the date created.
- ls -lsa. Like the ls and the ll command, the ls -lsa command prints the contents of the current directory to the screen. ls -lsa produces a similar result to that of ll, with one minor difference: ls -lsa also prints the file size allocated to a specific file or folder, whereas ll does not. In this command, note the three parameters–or options–that the ls command has been supplied with: l which lists the result in the long format; s which returns the size allocated to a specific file or folder; and a which tells the ls command not to ignore files prefixed with a period ( . ). Files prefixed with a period are considered to be hidden on Unix systems. Note that the optional parameters are supplied in the form of dash-parameter-parameter-parameter (-lsa) rather than like this:
ls -l -s -a
Each method produces the same result though, so whether you decide to write your command as ls -lsa or as ls -l -s -a will make no difference to the terminal. This rule is the same for any command that accepts optional parameters: the parameters can be supplied as a single argument or as separate arguments each with its own hyphen.
Before moving on, I would like to mention a few more useful parameters the ls command can be supplied with: - h. Adding -h to the ls command will cause the size allocated to files or folders to be displayed in a more readable format with K, MB or GB appended to the number, denoting what unit is is measured in.
- t. Adding -t to the ls command causes the files to be listed with the most recently modified file at the top and ending with the last-modified file at the bottom.
- r. Appending -r to the ls command will cause the list to be printed in reverse order; if used in conjunction with the -t parameter, for example, the files would be printed in order of last-modified to most recently modified.
- To stress the point of parameter formation, the ls command could be formatted, with the parmeters above, in any of the follow manners:
ls -hk or ls -h -k
ls -krh or ls -k -r -h - clear. A quick mention of clear is appropriate here. As its name suggests, clear clears the contents of the terminal window and shows only the prompt after running. This is a very useful command and especially so when browsing directories with the ls command.
- cd. The cd command is the next most basic and essential command that one must understand in order to work in the terminal. The syntax for cd is as follows:
cd [directory]
Where [directory] is replaced with the name of the directory one wishes to move to. For example, if I was in my home directory and wished to move from my home directory to a folder named “Documents”, I would type the following command:
cd Documents
If I wished to move to a directory within “Documents” (from my home directory), the name of the folder within “Documents” would be preceded by a slash and appended to the end of “Documents”:
cd Documents/Blog
The above command would move from my home directory to the folder within the Documents folder named “Blog”. - The .. directory. In Linux, the directory .. (dot-dot, period-period) is actually the directory “above” the current directory. So for example, if my current directory was Blog, located within the Documents folder, I could type the following command and end up in the Documents folder:
cd ..
Similarly, if I wished to move from my current directory–in this example, Blog–to my home directory two levels up, I would need to type the following command:
cd ../..
The preceding command moves up two levels since there are two .. “directories”. - pwd. Okay, this directory stuff is a bit confusing-I get that. One thing that is especially confusing is the directory hierarchy and moving around inside it. If I was in the Blog folder, as mentioned above, and did not remember that two levels up was my home directory, is there any way to find the current working directory and its path? Of course! Enter the pwd command. The pwd command actually stand for “print working directory”, and will print, if in the Blog folder, the following:
/home/Documents/Blog
Let’s analyze this: The top directory here is actually the root directory, which has a path of “/”. The root directory contains all files and folders for Linux systems. Beneath the root directory is the “home” directory, with the “Documents” folder inside it, and the folder named “Blog” inside the “Documents” folder. The folder at the end of the line is your current folder. Greater detail of the Linux file structure will be presented later in this article, or in another post sometime in the future. - touch. The touch command takes the name of a file and does one of two things: if the file does not exist, a new, empty file is created with the name supplied in place of [file]. If there is already a file with the same name as the one provided to the touch command though, the last-modified date on the file is updated to the current time of the computer. The syntax for the touch command is as follows:
touch -[options] [file]
Where [options] is replaced with optional parameters for the touch command, and [file] is replaced with the name of the file to be created or updated. - mkdir. This command creates a folder, or directory, inside the current directory. The syntax is as follows:
mkdir -[option] [folder_name]
Where [options] is replaced with parameters for the command, and [folder_name] is replaced with the desired name of the folder. - rm. rm deletes a file and functions with the following syntax:
rm -[options] [file]
Where [options] is replaced with optional parameters for the program, and [file] is replaced with the name of the file to be deleted. To delete a folder and it’s contents, use the following command:
$rm -r [foldername] - rmdir. Even though it is possible to delete a folder with the rm command, there is also another command for deleting an empty folder: rmdir. The syntax of the rmdir command is as follows:
rmdir [options] [folder_name] - ;. The semicolon is a very useful tool in the terminal: it allows for the separation of multiple commands to be run in sequence. For example, to clear the terminal window of all text and then print the path to the current directory all as a single command, one would need to input the following:
clear; pwd
Additionally, any number of commands can be run at once using this method so long as each is separated with a semicolon. However, if any command in the sequence fails, no commands after the failed command will execute. Therefore, although the use of the semicolon to separate commands is useful, it is not always used for obvious reasons.
Now that the basic review, as well as the introduction to a few new things, has been finished, it is time to move on to more advanced commands and uses for the terminal. The first subject to be covered will be editing a text file with various editors through the terminal. First though, what if you just want to preview a file? Sometimes it is beneficial to just take a glance at the contents of a text file in order to determine whether or not to open it. This can be done with the cat command. The syntax of the cat command is as follows:
cat -[options] [object]
Where [options] is replaced by parameters–in the same format as those for the ls command, although not the same exact parameters–for the command, and [object] is the file for the cat command to print to the window. Here’s an example of the cat command on a file called text.txt which has the contents “Hello, world!” (minus the quotation marks):
$cat text.txt
Hello, World!
$
Note that the dollar sign ($) is the character that precedes all terminal commands and indicates that you are running the terminal with limited privileges. Elevating your privileges will be discussed later in the article. As you can see from this example, catting the contents of the file text.txt will print its contents to the screen and then prompt you for a new command on a new line.
Now that you know how to navigate to a directory, how to make sure it is the correct directory by listing its contents, and how to display the contents of a file without opening it, what about editing? There are a few different ways to edit text files via the terminal, which is the topic that will be covered now.
- vi. vi is the most basic terminal editor and comes standard with all terminals–even Cygwin. The syntax for vi is as follows:
vi -[options] [file]
Where [options] is replaced with parameters in the same form as those for the ls command mentioned above, and [file] is replaced with a single file name. Since additional parameters are not frequently used for vi, I will not explain them in this article. However, near the end I will make known a method to find out much more about any terminal command.
It is possible to include more than one file in the vi command, but since only one file can be viewed and edited at a time there is little point in opening two files at once. Should you have some reason to do this though, simply write the command in this format:
vi program1 program2
The vi command will replace the current contents of the terminal window with the contents of the file specified by you, and will allow you to edit the file. If the specified file does not already exist, it will be created.
Upon opening, or creating, a file with vi, you will either be presented with the contents of the file on the screen or a blank window depending on whether or not you are editing a file or creating a new one. On Linux systems, you will automatically be put into insert mode. However, when opening a file with vi through Cygwin, you must change from command mode to insert mode by pressing the i key. Unless you are in insert mode, you cannot modify a file opened with the vi command.
Depending on the mode you are in you will use different keys to navigate through the open document. When not in insert mode, use the h, j, k, and l keys to move left, down, up, and right, respectively. When in insert mode though, use the arrow keys. The usage of the Home and End keys are only permitted when in command mode, but the usage of the Page Down and Page Up (PgDn and PgUp on most computers, respectively) is permitted regardless of the mode. A detailed guide on the usage of vi will be created and posted in the future.
When finished editing the file, hit Ctrl+C or the Esc key to exit insert mode (regardless of whether you are running the terminal natively or not) and enter command mode. Next, type a colon ( : ) followed by “w”, “wq”, or “q!” depending on what you wish to do with the changes you made to the file: - w. Typing “:w” while not in insert mode and then hitting Enter will cause the file to be saved–or “written”, as it may make more sense to think of it–without closing the file.
- wq. Typing “:wq” while not in insert mode and then hitting the Enter key will cause the file to first be saved, and then the editor will be exited–or “quit” as it may make more sense to think of it.
- q!. Finally, typing “:!q” while not in insert mode and then hitting the Enter key will cause the editor to quit without making any changes to the file currently open.
- A few more notes on vi: vi can be very difficult to use without some practice, and can be quite frustrating at times even for those with practice using it. vi is still something that you should be aware of if you plan to use the terminal though, so do not disregard it simply because it is difficult to use.
- nano. nano is another very basic text editor for the terminal which behaves in very much the same manner as vi. The syntax for nano is as follows:
nano -[options] [file]
Where [options] is replaced with additional parameters for the command, if any, and [file] is replaced with the name of the file you wish to edit. Since there are no modes in nano, one must simply open a file and begin typing in it. Full usage of all navigational keys and the backspace key is supported in nano. When finished editing a file in nano, simply hit Ctrl+X. You will then be prompted to answer “yes or no” to save the changes. Answering “y” will save the changes, and answering “n” will discard all changes to the file since it was opened. After answering with either “y” or “n” nano will exit and return to the terminal window from which it was called.
Another great thing about nano is all the built-in tools. These tools include the following: - Ctrl+W. Hitting this key combination allows for a user to input a string or a regular expression and then search for the next occurrence of that string in the current document.
- Ctrl+K and Ctrl+U. Ctrl+K will cut the current line of text and place it on the clipboard, and Ctrl+U will paste whatever has been placed on the clipboard by nano into the current document. Ctrl+U will not paste anything from the clipboard that has been added by anything other than nano, and the text clipped with this method is not available to any program besides nano.
- nano has quite a few more tools similar to the two outlined above though, which can be found by opening nano and hitting Ctrl+G. Ctrl+G opens the nano help file, which displays all the shortcuts that can be used in nano.
That concludes the list of editors built-in to the terminal, but is that it? Are there no more editors to choose from? No to both, actually. The next three editors I will go over are not built-in to the terminal, and with the exception of the first editor–gedit–must be installed manually on Linux systems. The process to install a program from the terminal will be covered later.
- gedit. gedit is a very good editor and is built-in to Ubuntu. gedit features a graphical user interface-aka a GUI, whereas both vi and nano do not have a GUI and are text-based editors. The syntax for gedit is as follows:
gedit [options] [file]
As with the vi and nano commands, [options] is replaced with parameters for the command’s execution, and [file] is replaced with the name of the file(s) you wish to open with gedit. Notice that I said file(s) and not file like I said in my description of vi and nano; unlike the editors previously described, gedit, since it has a GUI, has the ability to have multiple programs open at the same time in tabs. Furthermore, each tab can be moved to a new window without having to go back to the terminal and enter a new command. An example of this is as follows:
$gedit program1 program2 program3
The above command would open gedit with three tabs; each tab would contain a separate program in the order in which it was entered: program1, program2, and program3 respectively. - The syntax for each editor–gedit, nedit, and emacs–is the same as gedit‘s, so I will not explain it for each.gedit is my favorite text editor for writing programs on Linux systems because it is a bare-bones, no-frill editor that features a tabbed interface, syntax highlighting, and the ability to copy and paste text from the clipboard–very useful when programming.
- nedit. nedit is another text editor for Linux that features a GUI. Like gedit, nedit features syntax-highlighting, a tabbed interface, and the ability to copy and paste text from the clipboard. The choice between the two editors is that of personal opinion and should be made after trying each out.
- emacs. emacs is generally held up as the Holy-grail of text editors, although unless you plan to make extensive usage of it there is little chance you will see its benefits over either gedit or nedit. I personally do not find it all that amazing and would much rather use gedit; however, I have not taken the time to experiment with or learn to use emacs, so my opinion should not influence your choice in the matter, and as with choosing gedit over nedit, use it before making the choice. I will say this though: emacs does not do a very good job of handling multiple open programs at once, especially compared to gedit‘s or nedit‘s tabbed interface: emacs does not feature tabs for managing multiple open programs at once.
After trying these various commands out, you may have noticed a slight inconvenience when using the text editors featuring a GUI: once the editor has been opened, no more commands can be entered into the terminal until the editor has been closed. There are two solutions you might have tried: closing the editor each time you have finished making a change or after changing a program–very annoying when debugging a program–or opening a second terminal. Both solutions are fine, but there is an easier way: the ampersand sign ( & ). Before explaining exactly what the ampersand sign does, let me show you the syntax:
$gedit program1&
Notice the ampersand sign at the end of the command; appending it there will cause the command to be run in the background rather than in the current terminal window, which thus allows you to have an editor open and use the same terminal window it was launched from at the same time. Unfortunately, this does not work with either vi or nano, and a separate terminal window must be open in order to simultaneously run and write a program.
Now that you know how to do quite a few different things in the terminal, let’s move on to some more advanced topics. Remember when I mentioned that the dollar-sign prompt in the terminal meant that it was being run with limited privileges? It turns out that many operations will require you to have elevated privileges, so the next subjects I will cover are running a single command as an admin (with elevated privileges) and changing the current terminal to run all commands as an admin.
- To run a single command as an admin, you simply need to enter the following:
$sudo [command]
That’s it! Simple type sudo followed by the command, enter the password for the account you are using, and it will be run with elevated privileges. The syntax for the command is as it would normally be, but here is an example anyway:
$sudo gedit program1&
The command above will open program1 in gedit, and will do so with elevated privileges and in the background. - Next, to elevate the terminal’s privileges so that all commands run in it will be run as if prepended with sudo, use the following command:
$sudo su
Like running a command with sudo, you will be asked for your password. However, once you have run this command and entered the correct password, all future commands entered into the current terminal will be run with elevated privileges.
One example of a command that must be run with elevated privileges is the command to install software on Ubuntu machines: apt-get.
- The syntax for the most common uses of the apt-get command is as follows (from the Wikipedia article on apt-get):
$sudo apt-get install [program_name]
$sudo apt-get update
$sudo apt-get upgrade
$sudo apt-get dist-upgrade
In the order that they were listed, this is what the commands do:
The first command, apt-get install [program_name] installs the program, or package, with the name of [program_name]. Here’s an example that will install the emacs editor on the current machine:
$sudo apt-get install emacs
The second command, apt-get update , checks for updates to packages installed on the computer it is run on.
Third, the apt-get upgrade will update all packages on the computer it is run on.
Finally, apt-get dist-upgrade will preform the function of the apt-get upgrade command as well as attempt to handle changing dependencies.
The last commands I would like to run through are the cp and mv commands.
- cp. cp means “copy” and copies a file or a directory. The syntax is as follows:
cp [option] [/source] [destination]
To copy a directory, you must supply the -r parameter to the cp command:
cp -r Folder1 Archive_Folder1
The above command would copy the contents of the folder named Folder1 to a folder–created if it does not exist–named Archive_Folder1. - mv. The mv command has the same syntax as the cp command and accepts similar parameters as the cp command with few exceptions.
Throughout this article I have frequently mentioned some of the more useful parameters that can be supplied to a terminal command, but I have by no means mentioned all the parameters any of the commands discussed here can be run with. Therefore, I would like to introduce you to the man command. The syntax for the man command is as follows:
man [command_name]
Where [command_name] is replaced with any command. man returns information on the command it is supplied with including syntax and optional parameters, as well as a description of what the command does and how it works. It might take a bit of practice to get used to reading man pages, but once you learn to this becomes an invaluable resource when using the terminal.
man [command_name]
Where [command_name] is replaced with any command. man returns information on the command it is supplied with including syntax and optional parameters, as well as a description of what the command does and how it works. It might take a bit of practice to get used to reading man pages, but once you learn to this becomes an invaluable resource when using the terminal.
Articles or websites referenced in the creation of this post, or websites that provide additional information on topics covered here:
I have to learn how to use the terminal and this post is a perfect introduction at the perfect time. Thanks!
Thanks for commenting danoprey, I’m glad this article was helpful!
Um, a couple things re: vi…
1. “vi. vi is the most basic terminal editor and comes standard with all terminals” — this is like saying a laptop is the most basic calculator. Or that Emacs is the most basic terminal editor, for that matter. And besides, you don’t NEED vi to edit files in the terminal. Just use nano.
2. There is a GUI version of vi. It’s called gvim.
Thanks for taking the time to comment, Leila, I will update this article soon with more information, and will fix the mistakes you pointed out.
VI is also the oldest editor built for use on the UNIX platform. It’s also one of the most powerful if you can figure out how to use it correctly. Just my two cents, personally I usually just use nano but, a text editor is a text editor, they just differ in functions available.
I’m torn between agreeing with you, and disagreeing with you Shawn. On one hand, I recognize the power of VI, but on the other I like editors such as nano much better. In my opinion, it really just comes down to your personal opinion on the subject. Thanks for commenting.