The Linux Command Line
The Linux Command Line
Table of Contents
Contents............................................................................................................................... 2
1: The Terminal.................................................................................................................... 3
#1 Gnome Terminal........................................................................................................ 3
#2 Konsole...................................................................................................................... 4
#3 A dedicated terminal session.....................................................................................4
We have now seen three different ways in which we can access a terminal..................4
Questions?...................................................................................................................... 4
2: A closer look at a command............................................................................................. 5
3: File operations................................................................................................................. 6
List all the files in a directory........................................................................................... 6
Open a file...................................................................................................................... 6
Copy a file....................................................................................................................... 7
Move or rename a file..................................................................................................... 7
Delete a file..................................................................................................................... 8
Edit a file......................................................................................................................... 8
4: Gaining Superpowers...................................................................................................... 9
Sudo............................................................................................................................... 9
Su................................................................................................................................... 9
5: Pipes................................................................................................................................ 9
Using the output of one command as the input of another ...........................................10
Linking Commands....................................................................................................... 10
6: Package Management................................................................................................... 10
7: The Linux File System.................................................................................................... 11
1: The Terminal
What is a terminal?
A terminal is a command line environment, where a user can issue commands by
typing in the command using the keyboard.
A terminal is also referred to as a shell, in the context of a nut, the shell surrounds the
kernel.
Information
#1 Gnome Terminal
The Gnome Terminal opens a windowed terminal inside your desktop session.
From here you can enter commands directly, and the output will be displayed in the terminal
window
Information
In Ubuntu you can find Gnome Terminal by going to Applications >> Accessories >>
Terminal
In Kubuntu you can find Konsole by going to Applications >> System >> Konsole
#2 Konsole
Konsole is the default terminal in the KDE desktop environment, and functions in a similar
way to the Gnome Terminal
Other terminals are available, and offer various improvements over the standard terminal.
If you are interested then take a look in your software repositories for more information.
Unix / Linux operating systems have access to multiple terminal sessions that can run in
parallel to your desktop.
You can access a terminal session by pressing CTRL+ALT+F1
Information
Questions?
2: A closer look at a command
Information
If you want to know more about a command you can refer to its manual.
For example if you type man ls you will see the manual for the ls command.
Give it a try
ls
This is a command that lists the contents of a directory.
This basic command can use additional options to provide more useful information.
ls –a
This command will now list all of the files in the current working directory, including
any hidden files.
ls –h
This command will now list the size of files in kilobytes and megabytes.
ls –R
This command will list all the files in the current working directory, and it will also
show the contents of any sub directories in your current working directory.
You can also link these options together to use one command to complete multiple tasks in
one command.
ls-ah
This command will list all the files in the current working directory, including any
hidden files, and it will display the size of the files in kilobytes / megabytes.
What we have learnt is that a command can have many options, that can add more value to
the output of the command.
3: File operations
Command Description
ls Lists all the files in the current directory
Usage
ls - <option> *.
Options Description
-a Show all files, including hidden files.
-h Show the size (in kilobytes and megabytes) of files
-R Show files in subdirectories.
-t Sort by modification time (or when the file was created)
*. Search for files by extension type (I.e *.jpg)
Open a file
There isn't a specific command to do this, but the following should help
Command Description
more More is a filter for paging through text one screen of text at a time.
Usage
more <filename>
Options Description
-d This will prompt the user with the message "[Press space to continue,
'q' to quit.]" and will display "[Press 'h' for instructions.]" instead of
ringing the bell when an illegal key is pressed.
Command Description
less Less is a program similar to more (1), but which allows backward
movement in the file as well as forward movement. Also, less does not
have to read the entire input file before starting, so with large input
files it starts up faster.
Usage
less <filename>
Options Description
There are options available, but they would detract from the flow of this
guide. Please type “man less” for more information.
Command Description
N/a You can call an application and open a file at the sametime, for
example.
gimp photo1.jpg
Would open GIMP and open photo1.jpg ready for viewing.
Usage
Application filename
Copy a file
Command Description
cp Copies a file from one location to another.
Usage
cp - <option> SOURCE DESTINATION
Options Description
-a Archive the files (preserve the current structure of the directories).
-i Interactive, it will ask before overwriting a file.
-R Copy files in subdirectories.
-u Only update the changes to files and directories.
Command Description
mv Moves files or directories to another location
It can also rename a file
Usage
mv - <option> SOURCE DESTINATION
Options Description
-v Verbose, it tells you what it is doing.
Delete a file
Command Description
rm Deletes a file
Usage
rm - <option> FILENAME
Options Description
-r Recursive, deletes all files and directories
-i Interactive, ask before deleting a file
-v Verbose, it tells you what it is doing
-f Forces the deletion of a file
Warning!!
Never use the command rm -rf as this will remove every file on your computer.
Edit a file
Command Description
Nano, gedit Nano is a comand line text editor.
Gedit is a GUI text editor
Usage
nano FILENAME
gedit FILENAME
Options Description
N/A There are options for these commands, you are best using man nano,
or man gedit to discover more information.
4: Gaining Superpowers
Sudo
Command Description
sudo Sudo, is a command that gives a limited user the power of a superuser
(root) for a short time.
The user who uses this command must be on the list of sudoers, if not
then they will be unable to use the sudo command.
Usage
sudo -<option> COMMAND
Options Description
-h Displays a help file.
Su
Command Description
su Changes your user ID or lets you become the superuser (root)
Usage
sudo USERNAME
If used with no username, then the command will default to superuser (root)
Options Description
N/A See man su for more details
5: Pipes
Pipes are fantastic tools that allow data from one command to flow into another command.
Any input made via the keyboard is called “standard input” or stdin for short, any output on the
screen or to other output devices is called “standard output” or stdout for short.
Using pipes we can direct the standard output of one command, and use it as the standard
input of another command.
For example
ls | less
Using the command ls to list the contents of a folder, we can direct the output of the
command into the less command, so that we can pause the output on the screen.
Try it out!
Using the output of one command as the input of another
You can also redirect the standard output of commands to other files, using an “>” after the
command and specify the name of a file to create for examples
ls -l > list.txt
This will list the files in the current directory and direct the output of the command into a text
file called list.txt
Linking Commands
This is a particular favourite of mine, linking two commands together into one line.
I use it when updating my system remotely.
For example, when updating a Debian based system you would use these commands
These commands, update the list of packages for my computer, then upgrade the packages.
To do both in one line you can do this
Notice the “&&”? This is the “chain” between the two commands.
The second command is only run, if the first command is successful.
6: Package Management
You can manage the software installed on your machine from the terminal, for this example
we are using a Debian based system.
Debian uses the Advanced Package Tool (APT) to control the packages of software installed
on your computer.
To use apt, you need to be root, or prefix any commands with sudo. See Section 4, for more
details.
Ok, let's use apt to update our list of packages, I am going to run all of these commands as
root.
apt-get update
Once found, you can install the software using this command
So far we have learnt to update our repositories, search for software, install and then remove
software.
/bin this folder contains all the user-essential binaries (programs) that are needed to
administer and run your linux system… delete this folder and your system is broken.
/boot as the name suggests, this folder contains configuration files and other necessary files
that are needed by the bootloader
/dev this folder contains device files (remember, these files represent physical devices, so be
careful when working with them)
/etc this folder contains all the configuration files used by the system, you can also start and
stop services (daemons ) from here
/home this folder contains the home folders of all the normal (non – root ) users on the
system .. think of it as my documents in windows
/media this is a mount point for removable devices… this is where you would usually mount
your thumbdrives … etc
/sbin this folder contains binaries that can only be run as the root user (”superuser”)
/tmp this folder contains temporary files that are erased upon reboot
/usr this folder and its subfolders contains user installed programs and utilities and libraries
/var this folder contains files that change alot (”Variable files”)
/proc this is a psuedo folder, that contains information about the linux kernel and hardware
that is updated in realtime.