This post will teach the beginner how to work on linux terminal.
Everything that I introduce to you here is not only the very basics of the terminal but it will also help you pass the Linux Professional Institute 1 Certification!
The LPIC1 topic in question is: “Work On The Command Line” (103.1)
If you’re an absolute beginner who knows nothing about the linux command line then why not check out my other post ‘A Dummies Guide To The Linux Command Line‘ where I will cover the basic principles.
There’s a lot of terminology in linux and I will explain things where needed as much as possible.
I believe that the best way to learn anything (especially when it comes to computers) is by doing.
You can take in as much knowledge as possible but you won’t acquire and retain the skills that you want if you don’t actually do the thing.
So in this post I would encourage you to fire up a linux terminal and follow along with me as I take you through the steps needed.
Also be sure that you’re working in a regular user account so that everything I take you through here will match your setup.
I’m assuming you are in a bash environment, and on a Debian-based system (If you’re using a system other than a Debian-based system then it may not matter too much here).
This post (and the exam objective) is kinda like a crash course in using the linux shell and there’s A LOT to cover here, but stick with it!
It’s probably the most important, yet most complex part of the whole LPIC1 certification.
Get through this post and the rest will be easy 😉
To check what shell environment you are currently working in, execute the following command on the terminal:
Hopefully you get the response ‘bash‘. If the answer is yes, then we’re good to go! If not, then execute the following command:
Now try the following command again:
Are you in bash yet? If so then we’re ready to move on! Next I just want to make sure you’re not running as the ‘root’ user. If you execute the following command:
And you don’t get the response ‘root‘ but your regular user name, We are now good to go.
We don’t want to be playing around in the root account, If you didn’t know this already!
Some of this post is in chronological order so it would make sense to follow along from top to bottom.
FREE Download: Linux Mint Tool Collection!
Install tons of great software from the Linux Mint repository with my personal tool list that I’ve built up over the years. I also keep this list updated from time-to-time.
For more information Click Here
The 'bash' Shell Environment
What Is A Shell?
A shell is simply a program.
This program is used by you and me to interact with the system.
We can run programs in the shell and some programs can be used to input data.
We sometimes call the shell, an ‘interpreter’ because it interprets our commands.
As long as we give our commands in an expected format then the shell can understand them.
Finding The Default Shell
When we first open a linux terminal we’re usually already in bash, as bash is usually the default shell in linux.
to find out what our default shell is, we can take a look into the passwords file. Note that our actual passwords do not appear in this file as plain text.
The passwords file is not part of this topic and so I won’t cover it here but I wanted to make you aware of it.
So, to see what our default shell is, we can execute the following command:
If you take a look at the user accounts in the output of the command, you will notice ‘/bin/bash’. Some accounts may show ‘/bin/false’.
These accounts do not have a shell assigned to them and indicates that the user cannot login.
‘bash’ GLOBAL Settings
To create system-wide (global) profile settings for bash, we need to edit the following file:
This file holds global settings and sets things such as what the prompt looks like.
The screenshot below shows my default ‘/etc/profile’ file.
Notice how I used the ‘cat’ command to display the contents of the file? Also notice that in linux, we don’t usually need to give our filenames an extension.
For example, the file does not need to be called ‘profile.txt’.
This file is a shell script. It has a list of commands to be ran by the system.
We don’t need to know exactly how the commands work in this post, as it will be covered in a later post on ‘Shell Scripting’.
‘bash’ LOCAL Settings
If we only want to create or change user-specific bash settings then we would modify the two files: .bashrc and .profile which are hidden files stored in the users home directory.
As an example then, for me, these files would be located in:
Note that these file names begin with a dot.
These are hidden files and I will explain this in the ‘Hidden Files’ section, further down.
‘bash’ Colored Output
In linux we usually refer to folders as directories, although both names can be used as they’re inter-changable.
Some people from a Windows background for example usually prefer to call them folders.
If we issue the following command on the linux terminal:
We can see that our files & folders are color coded.
The settings for these colors are stored in the ‘profile’ settings.
Hidden Files & Folders (Directories)
If we want to see files in our current working directory then we can simply use the ls (list) command.
But linux has hidden files AND folders that do now show up in the command output of ‘ls’.
To see if any hidden files or folders exist in the current working directory then we can execute the ls -a command.
If you’re in a directory that has any hidden files or folders in it, then you will notice the new files/folders that now appear in the command output that begin with a dot.
Notice how the ‘hidden’ files & folders which begin with a dot can now be seen.
This is because we used the ‘ls’ (list) command with the switch ‘-a’ (show all). The color output helps us to distinguish between a file and a folder.
Dot (.) and Dot Dot (..)
The dot (.) and the double dot (..) is a way to describe a location in the linux file system.
The dot means “The current working directory”. That is, the directory that you are currently in.
The double dot means “Your current working directories parent directory”.
For example: If I am currently in the path “/home/installtekz/Desktop” and I want to run an executable program file that is in this Desktop folder and it’s filename is ‘runme.sh’, then I can use the following command to run this shell script:
This tells the system that the file I want to run is right here in this directory.
The exact same command is:
But if the ‘runme.sh‘ file was located in the ‘installtekz’ folder, (the parent folder to the ‘Desktop’ folder) then I can use the following command to run this same executable file:
The exact same command is:
The slash ‘/‘ is the directory at the very top of the file system.
If our ‘runme.sh‘ executable file was located here, and we were located in ‘/home/installtekz/Desktop‘ then we could run the following command:
In the last example then, the first 2 dots and slash will take us to ‘/home/installtekz‘, the second 2 dots and slash will take us to ‘/home‘ and the 3rd 2 dots and slash will take us to ‘/‘.
Change And Show Your Current Working Directory
Try this for yourself in the terminal:
The above command will print your current working directory.
then run the following command: (There’s a space after the ‘cd’)
Now run the following command again:
Notice how you are now in the parent directory of the first directory you were in?
; (semi-colon) For Multiple Commands
Notice how we ran a command, waited for it to finish, then ran our second command?
Instead we can write both commands together on the same line.
For this, we use the semi-colon to separate the commands.
Try the following example:
pwd ; cd .. ; pwd
Here we have 3 commands all on the same line.
bash will interpret the command up to the first semi-colon and then execute it.
Then it will interpret the rest of the line, up until it reaches the second semi-colon and execute the second command. Then finally it will run the third command.
Notice how we do NOT need a semi-colon at the end. bash will interpret the end of the line as the end of the command, just like it does when it reaches a semi-colon.
# This Is A Comment
We don’t usually need to write a comment directly on the command line.
But when we create shell scripts (which is not part of this topic) we simply create a text file with a list of commands in it.
When we execute the text file (shell script) linux will process each command from the top down (in a basic script).
We would then write comments near our lines of commands so that we can understand what it means when we look at the script in the future, or whenever someone else looks at it.
Anyone who is anyone that uses linux will know that you are not a true linux user unless you use the text editor that is vi.
vi is also important to learn for the LPIC1 exam too and I would encourage you to use it as much as possible when editing text files.
vi is very powerful. vim is another text editor and is similar to vi.
If you’re just starting out with the linux command line then rather than overwhelming yourself with vi as well as the command line, I would certainly recommend the nano text editor.
nano is not as powerful as vi and vim but it gets most simple text editing tasks done. To start the nano text editor we can issue the following command:
If the ‘textfile.txt’ file doesn’t exist, it will be created and opened, ready to be edited.
There’s a few ways to exit from the nano text editor, but I usually use ‘Ctrl’ + x, press ‘y’ to save, then ‘return/enter’ to confirm.
This will save the file and exit the editor, returning you back to the prompt.
nano, vi and vim are not the only text editors in linux. There’s WAY more than I could possibly mention here but they’re not needed for this topic.
Clear The Screen
Sometimes when we are working in the terminal we want to clear the screen. We could use the following command:
This works fine, but after a while it gets boring to type this out all of the time.
Try the following instead to save you time:
Hold ‘Ctrl’, press L
It does the same thing. But you look ninja 😉
Spawn A Child Shell From A Parent Shell
When we’re working in a bash shell, we can also open another bash shell from within the shell we are working in.
If we execute the following command in a bash shell:
we don’t see much happening, but what has happened is that another, child bash process has been created.
If we want to exit out of this child bash process we simply execute the command:
Doing this will exit the child bash process and return us back to the original, parent bash process.
How To Create Variables
If you’ve ever had a go at a programming language then one of the most common and basic features that you will come across first is variables.
With a variable, what we are doing is storing some data in to memory for later processing. The linux bash shell does this too.
To create a variable from the command line we simply give the name of the variable, followed by an equals sign (=) then followed by a value that we want to store.
Just like programming languages, we can call our variable names anything we want, although there ARE rules to what characters we can use.
Usually I’ve found that a good general rule to linux or any programming language is not to start the variable name with a number, don’t use any special characters what-so-ever except for an underscore character (_) which we can use to separate words to make our variable names descriptive.
So in this example then, I’m going to call my variable ‘var’ and give it a value of ’25’.
From the linux command line I would issue the following:
In linux it is said that ‘no news is good news’.
When we issue a command and we don’t see any obvious output, and we are returned back to another prompt, then it is usually assumed that the command was carried out successfully.
However, to check that our new value has been stored into our new variable, we can ask the system to give us the value by giving it the name of the variable.
To do this we can execute the following command:
The ‘echo’ command will simply display the result to the screen.
The dollar sign ($) is asking the system for the value that is stored.
The best way for me to describe a variable is to think of it like this:-
We have a box. We write a descriptive word on the box so we can have a good idea what is kept in it.
In this case, we are writing the word ‘var’ on our box, then placing the number 25 in it. We then ask the system what is in the box called ‘var’.
Try executing the following command from the terminal:
The output of the ‘env’ command gives us our environment variables.
One of the lines from this output shows our PATH. I will explain this in the next section.
If you followed along with the last section where we created a variable called ‘var’ and stored the value of 25 in it, we can now export this variable with the following command:
Now if we run the command:
again, we can see our variable has been stored into our environment.
We can check that the variable has been removed by looking at the output of:
again, or by executing:
And we will see that nothing is returned to the terminal.
What is the 'PATH'?
When we execute a command from the command line, the system will only look in a few specific folders to find the executable program you are trying to run.
The following path is usually where we keep administrator executables:
The following path is where other, non-system-administrator executables are kept:
notice how ‘sbin’ is short for ‘system binary’ and ‘bin’ for ‘binary’, as in ‘binary executable’.
We need to store these paths in a configuration file so that the system knows where to look for these executables. This selection of paths is what we call, the PATH.
We can take a look in these directories to see what commands exist there. For example we can execute:
This will show us the executable binaries that are stored in the /bin (slash bin) directory.
If we execute the following command from the terminal:
We can see what our PATH is. In my case then, the PATH is:
Each path entry is divided by colons (:).
As we can see, I have 8 different paths by default that make up my PATH.
Before I can move on to the next section on how to create your very own path where you and I can place our personal programs in so we can execute them from anywhere in the file system, we need to fist talk about tilde.
First off, we are professionals here. We no longer call it ‘That Squiggly Line’!
We call it Tilde (pronounce it how you like, but I call it: Til-dee).
Tilde is a shortcut for your home directory.
My home is: /home/installtekz
YOUR home will most likely have your own named account.
To understand how to use tilde then try the following steps from a terminal:
The above command will change our current working directory to the very top level. For me to go back to my home directory, I can execute:
This is known as the ‘absolute path name’. But as a shortcut, I can run the following instead:
This will take me back to my home directory of /home/installtekz.
Just to check that I’m now back in my home, I can execute:
Tilde comes in useful when we need to run scripts that were written by others.
Because no matter what your account name is, the tilde will take us to our home directory.
When we edit our PATH, we CANNOT/SHOULD NOT use tilde.
We should specify the absolute path. Meaning we should start from the first / (slash) and write the whole path name.
As a regular user, we should only place our very own personal files in our home. In fact, the system is designed to work this way.
Understanding How ‘PATH’ Works
Next I will cover the following steps in order to explain how the PATH works.
- Create A Simple Script
- Make The Script Executable
- Why Our Script Doesn’t Run
- Running Our Script From Current Working Directory
1: Create A Simple Script
Okay, firstly open up a terminal (If you don’t have one open already!) and just to make sure that we are in the home directory, execute the following command:
Now we need to make a text file with a simple command in it. I will use the nano text editor here. Run the next command:
When nano opens up our file, type in the following line:
Now if we hold Control (Ctrl) and press x, then y to save, then Return(Enter) and we will exit out of nano. If we type the following from the command line:
we can see our new file called myscript
2: Make The Script Executable
Now we need to make the file executable. Linux will not run a program if it is not executable. Execute the following command to make our new file executable:
chmod +x myscript
Don’t worry about how the command is working right now. What you have done is changed the file permissions. I will cover file permissions in another post.
3: Why Our Script Doesn’t Run
4:Running Our Script From Current Working Directory
To get linux to find our program if it doesn’t exist in our PATH, we need to use the dot (.) to indicate that we want linux to search for the file in the directory we are currently in.
Now if we run the following command:
The program should now work, and we should see ‘Hello’ output to the terminal.
I can also specify the absolute (full) path with the following command:
But YOU will need to replace ‘installtekz’ with YOUR username.
Adding Another Path
If we create a program or shell script and run it, the system needs to know where to find it.
Rather than specifying where it’s located every time we want to run it, we can add it to our PATH.
Now that we have our executable file from the previous section, we can now place this program into our PATH.
By doing so, we can then execute our program from anywhere in the linux file system.
- Place Script Into A Folder
- Place This Folder Into Our PATH
- Execute Our Script From Anywhere
1: Place Script Into A Folder
Let’s create our own ‘programs’ folder:
Now move the ‘myscript’ file into this ‘programs’ folder:
mv ~/myscript ~/programs
2: Place This Folder Into Our Path
We cannot use the tilde here (~) as we need to enter the ABSOLUTE path. To do this you will need to use my home directory as an example!
Enter the following command in the terminal but replace ‘installtekz’ with YOUR user name:
now execute the following command to export the PATH:
3: Execute Our Script From Anywhere
Now we can type the name of our program (myscript) anywhere in the system, and it will run.
We can also check to see what our PATH is by executing the ‘env’ command again:
And we should now see the ‘programs’ absolute path, in our PATH.
If we wanted our PATH to stay persistent, (remain the same across reboots) then we can add this PATH line to our .bashrc file. (located at ‘~/.bashrc‘ or ‘/home/installtekz/.bashrc‘ in my case).
The TAB auto-completion feature in bash is an excellent feature that I use all of the time and I really couldn’t live without it.
When typing out the name of a command and it’s either a long name, or we simply can’t remember the whole name, we can double-tap the TAB key and it will auto-complete.
This feature depends on the PATH.
When we double-tap the TAB key, the system will use the paths that are defined in PATH and look for a matching command name in those directories.
If your command is ‘ambiguous’, meaning there’s more than 1 match for what you have typed, then bash will show you the possible matches, rather than auto-completing.
This auto-completion feature also works when typing out a path to a directory too which is very handy when we either have long directory names or when we can’t remember the name of the folder, or even when we don’t know what folders even exist!
The ‘history‘ in bash is another great feature that I couldn’t live without.
We can type the following command in the terminal:
to show us the last commands that we have executed. I don’t use this command very often at all but history can be scrolled through by using the UP and DOWN arrow keys on the keyboard at any one time.
These arrow keys save so much time when you’re looking for a command that you used a while back.
Did you ever watch the series ‘Lost’ where a bunch of people crash landed on a strange island?
Imagine it was YOU who discovered the computer that was displaying a terminal on the screen.
What would be the very first command you would execute? bearing in mind that you have no idea what system it is.
The above command on a linux terminal will usually return the reply “linux“.
This is a good start. We now know we are dealing with linux. But what else can we discover about the system?
(Think of the “-a” switch as “all“) The above command will give us a lot more information about the linux system. It usually displays the kernel version, the build number, the date of build and the processor architecture.
This info is great to know from just running a single command and will give you a general overview about what you’re dealing with.
The ‘man’ (manual) command is most certainly one of those very basic commands that you will need to know. No one knows everything about linux (as much as some people may think they do) but we all need help on commands occasionally.
This is where the ‘man’ command comes to the rescue. To use man, we simply execute the man command, followed by a command that you need more information on. For example, If I wanted more help on the ‘ls’ command, I would execute:
Once we have the manual open, we can exit by pressing the ‘q’ key, we can scroll up and down by either using the arrow keys, or the ‘End’, ‘Home’, ‘Page Up’ and ‘Page Down’ Keys.
If we want help on something but we’re not sure what command we need, we can use the ‘k’ switch to search for a specific term, like in the following command:
man -k ls
Where ‘ls’ is just an example here.
The command: ‘apropos’ works very similar to ‘man -k’. As an example:
apropos -k ls
and I believe it’s pronounced: apro-po.
‘exec’ is another command that you may need to be aware of for the LPIC1 exam and also another command worth knowing for the real world too.
exec will run a bash shell from inside another bash shell.
Running the exec command on another command will execute that command without the shell environment variables. You may notice this because the files and directories may not be color coded.
Try the following commands for yourself in a terminal:
You will notice that the terminal quits!
Open the terminal up again and now type:
once again. The new bash shell that we opened has now been exited by the exec command and returned to the original bash shell. If we type:
again, the terminal window will quit on us once again.
This post was intended to teach you how to work on linux terminal (Expressed as: “Work on the command line” in the LPIC1 certification).
There’s many different shell environments available to us in linux and UNIX but the one we need to be concerned about the most (and especially for the LPIC1 exam) is of course, bash.
Hopefully by now you feel more comfortable with working around the bash shell.
In this post I covered the PATH, where we can configure linux to look for executable files, setting environment variables using the .profile and .bashrc files, using the ‘history’ command and a bunch of other commands and shortcut keys that the certification is looking at you to test you on.
There’s a lot to take in here in this post but once you get all these basics down then you’ll be on your way to becoming a pro at linux in no time!