Linux Terminal Tutorial
At 1 time we have all been interested in the idea of using a Linux distro for a variety of reasons. Whether it be customization, privacy or just the idea of the operating system being free of charge, it sounds like a great idea until that dark cloud known as the Terminal turns its ugly head.
The commands used within the Terminal might seem scary and confusing but the more you work with it the easier it becomes to read and understand.
To begin we will open the Terminal. From here we will take a look at our Linux system to familiarize ourselves and to gather information that will prove useful to us.
LS
We would use the command "ls" to see what files and directories are listed in the current directory
Definition:
List files and directories
Example:
ls
CD
"CD" will allow us to change into a different directory by providing the path to where we want to go.
Definition:
Change Directory
Example:
cd /etc/
PWD
As we navigate to different parts of our OS we might find ourselves lost and not knowing what directory we're in. This is where "PWD" comes in handy.
Definition:
Print Working Directory
Example:
pwd
MKDIR
If we wanted to create a folder from Terminal we would use the "mkdir" command to do so followed by whatever we wanted to name the folder.
Definition:
Make Directory
Example:
mkdir test
TOUCH
If we continue with our create directory example and we cd into our newly created directory, we would want to create a file. We can do so with the "TOUCH" command. We follow the command with the name of the file and whatever file extension needed. Below we will create a text file.
Definition:
Creation of a file
Example:
touch test.txt
VIM
Now that we have created our file, we want to edit that file. To do so we will use the command vim. This will open the editor within the Terminal. Be sure to follow the command with the path or name of your file. To add text to this file we will first need to press 'i' (for insert) this will allow us to enter edit mode. Once we have edited our file we can save it by leaving the editing mode by pressing 'Escape' then save it by pressing ':' and then typing 'wq' 'w' is to save and 'q' is to quit. If we did not want to save and edit the editor we would press 'Escape' then ':' and 'q!' this will allow us to quit the editor without saving the changes we made.
Definition:
A text editor that is based on VI. It is used heavily by developers and system administrators.
Example:
vim test.txt
CAT
Now that we have our file. If we wanted to view the contents of that file without using the editor then the 'CAT' command comes in handy. It will show the contents of the file within the terminal. This is useful for small files where you might need to copy a line or 2. be sure to add the path or file name after the command.
Definition
Derived from the word "concatenate" cat will list the contents of the file in the Terminal window.
Example
cat test.txt
GREP
Grep is used to find matching strings within a file and proves to be very useful. When paired with the right tags it can be extremely helpful. In our example we will use 'grep' in basic way to find a specific word we created in our file. We will work with the term "hello there"
Definition:
Example:
grep hello /usr/$USER/desktop/test/test.txt
CP
With our test.txt file if we wanted to duplicate this file we could do so with the 'cp' command. We would follow 'cp' with the path to the filename if it isn't in the current directory then leave a space and the path to the destination ending with the new name of the file.
Definition:
Copy a file or directory
Example:
File
cp test.txt test2.txt
File (copy to another location)
cp test.txt /usr/$USER/Desktop/test.txt
Folder (We use the -r to copy the contents of the folder as well)
cp -r /usr/$USER/Desktop/test /usr/$USER/Documents/test
MV
If we take the File (copy to another location) example from above let us try to move the test.txt from within the test folder to the desktop and call it testNew.txt.
Definition:
Move a file or directory
Example:
mv /usr/$USER/desktop/test/test.txt /usr/$USER/desktop/testNew.txt
FIND
Find is a command that comes in very handy when we have a hard time finding our file if we do not know the exact location of the file. We can use this command by being in a parent folder and using the command followed by the file name.
Definition:
To locate a file or directory.
Example:
find test.txt
RM
With our newly made files and folders let us end this part of the command tutorial and clean up what we have created. Overall we might have 2 files on our desktop. An empty folder on our desktop and a folder in Documents which has a file inside of it.
Definition:
Remove a file or directory
Example:
Remove the text files on the desktop
rm /usr/$USER/desktop/*.txt
Remove the test folder from desktop
rm -r /usr/$USER/desktop/test
Remove the test folder from Documents
rm -r /usr/$USER/Documents/test
The commands we have looked at so far alot of them relate to desktop management, in the next part we will look at commands that the operating system utilize and some of the tools that we can use.
DF -H
When we want to see how much disk space we have available on not just our operating system but any drives we have connected to our system we would use the 'df' tag.
Definition:
Free Disk Space the '-h' refers to a tag that will made the output more readable to the human eye.
Example:
df -h
DU -H
If we wanted to get an overview of the size of files and directories we have within a folder we can get that information via the 'du -h' command. Again the '-h' is for human readable. This command would be run within the folder we want the information for.
Definition:
Disk usage
Example:
du -h
WHOAMI
If we wanted to know the active username that was being used in the Terminal session 'whoami' is perfect.
Definition:
Who Am I logged in as?
Example:
whoami
WHEREIS
There are some applications that might be hard to locate. 'whereis' plays a role to help us find a system application we might have a tough time locating. We will search for Python in this example
Definition:
Where is a system application located
Example:
whereis python
MORE
There are certain files that we might come across that contain more information than can be displayed on the screen. For this we have the 'more' command. This command will allow us to scroll through the file and with certain tags we're given more flexibility. Let us take a look at our bash file.
Definition:
Example:
-d will provide us with with some navigation.
more -d /etc/bash.bashrc
TOP
When we need to troubleshoot or if we're just curious to know how much cpu power is being used for a specific application we can use the 'top' command to provide us with that information.
Definition:
Information related to the CPU.
Example:
top
KILL
We would use the 'KILL' command if we notice a program that was not responding or if it was taking up too much CPU usage. How we would address it would be to use the 'top' command we learned in the previous example to help us identify the process that needs to be closed. We would identify the process number by looking at the PID column and matching it to the process in question. We would then quit out of 'top' and use the 'kill' command followed by the Process ID.
Definition:
Used to terminate a process.
Example:
kill (PID)
APT
Apt allows us to manage our packages on our Linux system; it is Debian based and supports those distros of Linux that are under the Debian umbrella, such as Ubuntu. We will install a package that will be used in the next example called "cowsay".
Definition:
Advanced Package Tool, used in the Terminal to interact with the packaging system.
Example:
sudo apt install cowsay
COWSAY
Cowsay is a fun little app that allows you to have messages output by a ASCII generated cow. You will need to install the application first which we have done in the previous example.
Definition:
Allows you to add a message to an ASCII generated cow.
Example:
cowsay Look mom Im Linuxing!
PING
Ping is a command that is used to see if our computer can connect to a specific site. It is used to help identify issues with network stability. If ping can connect to the site we're provided with information with how fast it was to ping the site.
Definition:
To reach a specific internet address with specifics on time and data packet health.
Example:
ping google DNS
ping 8.8.8.8
NMAP
This tool gives us a glimpse into what ports are open on our machines. This proves useful when it comes to security and network troubleshooting.
Definition:
A network map of open ports on the device.
Example:
sudo nmap (ip address)
CHMOD
CHMOD acts as a gatekeeper regarding the permissions of the file. Extensive reading should be done on this topic before modifying the permissions of a folder or file as it can cause major issues if done incorrectly. Overall the settings for CHMOD has 3 permission categories. First the owner, then the group and then everyone else.
Definition:
Modifies the permissions of a file or folder
CHOWN
You might find yourself with a set of files that belong to a variety of different users. The likely scenario that can cause this is transferring files between computers. To use this command we would need to use 'sudo' then 'chwon' followed by the user we want to own the file or directory and then the name of the directory or file.
Defintion:
Change of Ownership
Example:
sudo chown (username) (filename)
EXIT
When we're finished using the Terminal and we want to close it we would use the 'exit' command.
Definition:
To quit the Terminal
Example:
quit
And there we have it, a look into Terminal commands for beginners. Hopefully this article proved useful and encourages you to play around and get familiar with the Terminal and tools and commands that it has to offer.
Top comments (0)