In this article, I explain how to use the Terminal commands to provide information about some commands and what they do.
I provide examples to illustrate the process as well.
The command-line tool is powerful. Tools exist that take a tedious job in a graphical User interface (UI) and turn it into a task that takes a few seconds. Removing the last four lines in every row of a large file would be a lengthy process in a user interface application. However, it can become automated on the command line.
The most common ways of opening a Terminal window in Ubuntu Linux are:
There are many commands that are associated with the Terminal Shell in Ubuntu Linux. The article below lists the most commonly used, under the groups they affect.
There are three main commands that we use to provide all the information required to use the various terminal commands.
The man command displays the manual for any command that is used in the Terminal.
Most shell commands accept various options. To get some information about a command and a list of the available options, use the man
- short for manual command. Give the name of the command that you want to find more about as its argument. That is which command the man
command works on.
Example: If you enter the command man who
, the following output is displayed.
WHO(1) User Commands WHO(1)
NAME
who - show who is logged on
SYNOPSIS
who [OPTION}... [ FILE | ARG1 ARG2 ]
DESCRIPTION
-a, --all
same as -b -d --login -p -r -t -T -u
-b, --boot
time of last system boot
-d, --dead
print dead processes
-H, --heading
print line of column headings
-i, --idle
add idle time as HOURS:MINUTES, . or old (deprecated, use -u)
-l, --login
print system login processes
--lookup
attempt to canonicalize hostnames via DNS
-m, only hostname and user associated with stdin
Manual page who(1) line 1
Many of the manuals are several screens long. Use the cursor or ARROW
keys or the Page Up and Page Down keys to navigate through the text. The spacebar key shows the next line of text. Pressing the q key quits the manual and returns to the shell.
We can see from the man page that we can use the -a
option to display more information about the active user sessions.
user@3[user]$ who -a
|
|
|
|
Nov 14 13:47 |
|
10 |
id=si |
term=0 |
exit=0 |
|
|
|
system boot |
Nov 14 13:47 |
|
|
|
|
|
|
|
|
run-level 5 |
Nov 14 13:47 |
|
|
Last=S |
|
|
|
|
|
|
Nov 14 13:47 |
|
835 |
id=15 |
term=0 |
exit=0 |
LOGIN |
|
|
tty1 |
Nov 14 13:47 |
|
958 |
id=1 |
|
|
root |
|
- |
tty2 |
Nov 14 19:15 |
00:19 |
959 |
|
|
|
user |
|
- |
tty3 |
Nov 14 19:28 |
00:06 |
960 |
|
|
|
LOGIN |
|
|
tty4 |
Nov 14 13:47 |
|
961 |
id=4 |
|
|
LOGIN |
|
|
tty5 |
Nov 14 13:47 |
|
962 |
id=5 |
|
|
LOGIN |
|
|
tty6 |
Nov 14 13:47 |
|
963 |
id=6 |
|
|
user |
|
? |
:0 |
Nov 14 13:47 |
? |
965 |
|
|
|
|
|
|
pts/3 |
Nov 14 16:29 |
|
1746 |
id=/3 |
term=0 |
exit=0 |
user@3[user]$
It also reads help files for programs, the man
command can also give information about shells, functions, and libraries.
If you are not sure how to use a specific command, run the command with the -h
or –help
switches. You see usage information and a list of options that you can use with the command.
Example: If you want to know how to use the wget
command, type wget –help
or wget -h
.
User@Ubuntu@#$ wget --help
GNU Wget 1.12, a non-interactive network retriever.
Usage: wget [OPTION]... [URL]...
Mandatory arguments to long options are mandatory for short options t
oo.
Startup:
-V, --version display the version of Wget and exit.
-h, --help print this help.
-b, --background go to background after startup.
-e, --execute=COMMAND execute a '.wgetrc' -style command.
Logging and input file:
This often prints a lot of information to the terminal. It can be unwieldy to scroll through. To read the output more easily, you can pipe
it through the less command. Which allows you to scroll through it with the cursor or ARROW keys on your keyboard.
Example: wget -help | less
GNU Wget 1.12, a non-interactive network retriever.
Usage: wget [OPTION]... [URL]...
Mandatory arguments to long options are mandatory for short options t
oo.
Startup:
-V, --version display the version of Wget and exit.
-h, --help print this help.
-b, --background go to background after startup.
-e, --execute=COMMAND execute a '.wgetrc' -style command.
Logging and input file:
-o, --output-file=FILE log messages to FILE.
:
Press q to close the less utility when you are done.
To find a specific option, you can pipe
the output through the grep
command. (For example: Use the following command to search for options that contain the word proxy.
)
wget -help | grep proxy
User@ubuntu:~$ wget --help | grep proxy
--no-proxy explicitly turn off proxy.
--proxy-user=USER set USER as proxy username.
--proxy-password=PASS set PASS as proxy password.
User@Ubuntu:~$
Some commands are also documented inside the info
system. The information here complements what the man
command displays.
Example: Enter the command info ls,
the following output is displayed.
[support@support]$" info ls
You can now view a manual with important information about the ls
command.
File: coreutils.info, Node: ls invocation, Next: dir invocation, Up: Directory listing
'ls': List directory contents
=============================
The 'ls' program lists information about files (of any type,
including directories). Options and file arguments can be intermixed
arbitrarily, as usual.
For non-option command-line arguments that are directories, by
default 'ls' lists the contents of directories, not recursively, and
omitting files with name beginning with '.'. For other non-option
arguments, be default 'ls' lists just the file name. If no non-option
argument is specified, 'ls' operates on the current directory, acting
as if it had been invoked with a single argument of '.'.
By default, the output is sorted alphabetically, according to the
locale settings in effect. (1) If standard output is a terminal, the
output is in columns (sorted vertically) and control characters are
output as question marks; otherwise, the output is listed one per line
and control characters are output as-is.
--zz-Info: (coreutils.info.gz)ls invocation, 48 lines --Top------------------
Welcome to info version 4.3 Type C-h for help, m for menu item.
You can scroll down the page by pressing the spacebar key or by using the cursor/ARROW
keys.
help
, press Hquit
, press Q
You can do almost anything in a terminal, that you would also do from a user interface.
As said before, many commands were designed first to work in the terminal, and then a user interface was put on top. That is why some GUIs may feel clunky - at first they were an afterthought as most Linux Server builds do not bother with them.
The default location for your terminal to open from the menu is in your home folder. Known as ~
You can find your current directory by the .
operator. Most commands are case-sensitive when they act on the current folder selection operate on commands, locations, and files. /home
is not the same as /HOME
or /Home
.
Use the TAB key to complete file names if you have a long driver title. driver-128947232jaseu.sh
for example, type dri
and it enters the rest. Provided you do not have two names starting with dri
and if you do, add another character to make it driv
and try again. Ensure that the auto completely matches what you want to see.
Almost any command can be read about in full using the man
page or by typing -h
or --help
after writing the initial command. This syntax is either man command_name
, command_name -h
or command_name --help
.
To get even more information, you can use info. A command can be searched for by using info command_name
. For most of these commands which are part of the coreutils package. You can find info
as well using info coreutils command_name
invocation where the command searched for replaces command_name
.
Almost any command can show exactly what is happening, step by step. This is usually the -v
or --verbose
.
You can specify multiple command flags to a command at a time to get more information. See the ls -al
example below.
Command names are not always obvious - due to space limitations in the old days of UNIX that they were shortened and these conventions stuck.
cd
- Moves you back to your home, same as cd ~
cd..
- Takes you back one directory. Starting in /home/user/Desktop
, cd..
puts you into /home/user
. This can be expanded to cd ../../
which moves you back two directories to /home
.
cd foldername/
- Move you forward to the given folder in your current folder.
/
, it is an important omission. If I am in /home/user
and I want to get to Desktop
. I must type cd Desktop/
without the /
before Desktop
. Typing /
before it places us in the root of the file system which would not work.
cd /some/other/path
- Takes you to the specified folder path. If you got the path right. Do not forget you can use the TAB
key to autocomplete.
ls
- Lists all your files in the current folder.
ls -l
- Provides a longer list including owners, permissions, size, and date modified.
ls -a
- Displays the hidden files and folders and the normal list.
ls -al
- Combines two options to display both the hidden files and folders and do it in the long format.
ls -h
- Shows file sizes in human readable format KB, MB, GB,
file sizes instead of bytes. It is most often used with the -l
flag.
You can view files in directories you are not even in. If I am in /home/user/Desktop
and I want to view a file in /home/user
, I can do ls ../
which lists the files one directory back.
cp file /path/to/folder
- Copies the specified file to the given path.
cp -r folder /path/to/folder
- Copies repeatedly the contents of the folder to another folder.
cp *.extension /path/to/folder
- Copies the files matching the given extension to the new folder. To copy all .doc files, it becomes cp *.doc /path/to/folder
and the folder must exist.
cp name* /path/to/folder
- Copies all the files starting with name
to the given folder. To copy all files starting with example
, it becomes cp example* /path/to/folder
and the folder must already exist.
The syntax of mv
is similar to the example above with cp
exempt for example #2. mv
does not take the -r
flag since moving a folder also moves its contents. The syntax is not exact in all instances, but works with the above examples. Consult your manpages for more details.
Removing files using rm
is permanent. It does not use the Trash bin. Use this with caution and ensure that you are deleting exactly what you want, before you press the Enter
key. If you overcomplicate your delete commands, it never ends well.
rm file
- Remove that specific file from the computer.
rm -r folder
- Remove that specific folder from the computer.
rm -rf folder
- Removes that specific folder forcefully from the computer. This command can mess up your configuration if it is used incorrectly.
You can edit files using nano
in a terminal to do quick and rough files all the way up to full configurations. It can be useful, but it handles plain text files and programming files, so things like Microsoft Word documents do not open properly.
If the root owns a file, a regular user cannot edit it. nano
must be prefixed with sudo
; in order to save changes. It opens in read-only mode otherwise.
nano newfile.whatever
of the specified name and opens it for editing.
nano existing_file
- opens the existing file for editing.
From inside nano
Save the file by pressing the CTRL+O
keys together and either change the name or press the Enter
key to keep the same name. This saves the file.
Exit nano
by using the CTRL+X
keys together. If you have unsaved changes, then it asks if you want to save them.
mkdir folder_name
- Creates the folder with the specified name
mkdir -p /path/to/folder/name
- Creates each folder as necessary. To create a folder /home/user/1stfolder/2ndfolder
, and only /home/user
exists, using mkdir -p
makes both directories 1stfolder,
and 2ndfolder
.
ps aux
- Lists all the processes in detail running on the computer. This includes user, Process ID PID,
and name of the process. Using this you can view the process list and if necessary you can kill unnecessary or stalled processes.
kill PID
- PID is a number referencing the offending process. You should obtain the PID from a command like ps aux
. If a process refuses to die, you can also specify kill -9 PID
which should terminate the process by any means.
killall program
- Killall
kills by name
all instances of the listed program
. If there are for example 3 Firefox Internet browser sessions open, killall Firefox
does exactly that, kill all Firefox sessions. kill
would take the specified PID of the offending Firefox process that you want to kill and kill that one only.
xkill
is a user interface way to click and kill windows. Typing in xkill
should bring up a skull-and-crossbones icon and the next window clicked is killed.
Pipes
are shown as a straight bar,
otherwise known as the |
key.
It is a rarely used key in Windows, but it is often found on the backslash key.
These are used to link commands together. Pipes
take the output of one command and route it to be used as input for a second command when they are chained together.
Consult online resources for more information about pipes
and their use as there are volumes that are written on them.
>
is used to overwrite
the existing files contents by replacing them with the output from the new command.
>>
is used to append
information to the existing files. This is useful for logging actions.
Example:
ps aux > processes.log
sends the output of ps aux
to the file processes.log
for viewing the command output in a text editor and overwrites the current contents of the file.
tee
is used along with a |
in order to take the command output and send it somewhere else. This is useful if there are errors which you miss. This way whatever goes on the screen is also captured to a file.
Example: dmesg
| tee boot.txt
would run the command dmesg
which shows the initial boot info. The |
sends the output of dmesg
to tee
, which then does its job by sending it to the terminal and to the log file boot.txt
.
Do you want to run a file in the current directory after it is marked executable? The ./
operator can run the file as a normal user provided you do not need root rights. ./
literally means in the current directory
so it does not work on files outside of the present directory.
If you want to run a file not in the current directory, then you must pass the path to the proper running program. If it is a python program, it is python /path/to/file
and if it is a shell file, it is sh /path/to/file
as an example. There are other programs, but these are the most common.
If you want to run a file with root rights because you received operation not permitted?
. You must prefix the command with sudo
. As with the above example, sudo python /path/to/file
runs the script with root rights.
If you want to run a user interface program from the terminal, then type the program name - case sensitive!
and it launches. This renders the current terminal unusable. Closing the terminal while the program is open kills the program as well. A better way is to background the program, using program_name
and then typing the word exit
at the terminal to close it and keep the process running.
If you want to run a user interface program with root rights from the terminal, then prefix it with gksudo
or gksu
and not sudo
. Using sudo
to launch user interface applications is a bad habit and should be avoided.
Do not
use sudo
only because something brings up Operation not permitted.
Keep in mind that you can destroy
computers by running commands in the wrong place with root rights. Ensure that your files come from reputable sources.
Lost yourself in a directory? Type pwd
to print working directory.
Want to calculate your disk space? df -h
can give you a quick figure.
Want to calculate the size of a folder or file? du -cksh target_name
does exactly that. Want to calculate the size of the current folder? du -cksh
.
Want to mark a file executable? chmod +x filename
does that.
You have to mount an iso? Linux has this functionality built in. Create a directory somewhere, say /home/user/isomount
and issue the command mount -o loop /path/to/myisofile.iso /home/user/isomount
and the contents are mounted inside that folder.
Having run a command, you must rerun it, but you cannot remember exactly how it went? Type history
into the terminal and it prints your command history. Want to clear your history? history -c
wipes the information.