If you are getting into Linux whether it be as a hobby or as a requirement for work, you will need to familiarize yourself with the command line. Unlike Windows where you can do pretty much everything with a graphical user interface, with Linux sooner or later you will have no choice but to open a terminal (command prompt or powershell in Windows). Don’t let this intimidate you though, the terminal is actually much easier to learn than it seems!
In this post I will be showing you some of the basic linux commands, including how to navigate the terminal, work with files, checking logs, and more. If you have any questions or comments, feel free to leave them below in the comment section.
Navigating The Terminal
When working with the terminal, you will absolutely need to know the basic linux command for changing directories. I will include a few simple tricks that make things much easier like automatically going into your home directory, moving up one directory, etc.
Print Working Directory
The pwd command can be useful if you are unsure of where on the filesystem you are actually working out of. Here is an example of the command being run.
mike@ubuntu:~$ pwd /home/mike
You can change which directory you are working out of with the cd (change directory) command. In the example below I will change into the /var folder.
cd <place> mike@ubuntu:~$ cd /var mike@ubuntu:/var$ pwd /var
Moving Up A Directory
You can move up a directory without having to type in the entire path by using the following shortcut.
cd .. mike@ubuntu:/var$ cd /var/log mike@ubuntu:/var$ pwd /var/log mike@ubuntu:/var/log$ cd .. mike@ubuntu:/var$ pwd /var
Home Folder Shortcut
If you need to quickly access your home directory, you can use the following shortcut. This also works when copying files or running scripts located in your home directory (we will cover this later).
cd ~ mike@ubuntu:/var$ cd ~ mike@ubuntu:~$ pwd /home/mike
Working With Files
In the next segment of this guide I will show you how to manage and work with files, this will be a somewhat more lengthy section but this is a very important part of learning the terminal. If you don’t learn how to work with files then you will not be able to get much done when it comes to configuring and checking things on your server. Lets begin with a few basic commands like cp, mv, etc.
Listing All Files/Directories
If you need to see what files are in a certain directory then you can use the ls command. Here is a basic example of using the ls command.
In Linux there are hidden files (any file that starts with a period), to show these files we need to add a -a, here is an example of showing hidden files.
ls -a /etc/
Finally to show an actual list with file permissions and everything, you will use the -l command. A-lot of people set ls to automatically use -l and -a together using aliases, we will cover setting up aliases in a different guide though as this is intended to be a basic linux commands guide. Here is an example of using the -l and -a together.
ls -al /etc/
You can copy files using the cp command. When using this command you can also name the copy whatever you like, for example you can copy a configuration file and add a .backup to the end of the file name. This way if your new configuration doesn’t work you can easily revert back to your working configuration. Here is an example of copying a file to make a backup.
cp /path/to/example.txt /path/to/example.txt.backup
One thing to remember when copying files is that you don’t have to specify the path if you are already working out of the directory the file is located in. Here is an example of copying a file when already in the same directory.
cp example.txt example.txt.backup
Moving And Renaming Files
One of the differences in using the Linux terminal vs using Windows CMD (command prompt) is that there is no rename command, instead you simply use the same command (mv) as moving to rename. Just like the copy command above, you do not have to specify the path if you are already in the same folder as the file. If the directory you are moving files to doesn’t exist then this will fail, so always create your directories ahead of time. Here is a quick example of moving a file.
mv /path/to/example.txt /new-path/to/example.txt
To rename a file in Linux you will basically do the same as above, the only difference is you will change your file name on the second argument. Here is an example of renaming a file.
mv example.txt renamed.txt
This portion of the guide is where you should pay close attention as once you delete a file it is gone forever. There is no trash can for files removed via the terminal. To remove a file or more than one files you will use the rm command. Here are some quick examples with what they do.
# Remove with confirmation rm -i filename # Remove without confirmation rm filename # Force file removal (if there is an error or warning) rm -f filename # Remove all files and directories inside working directory # DO NOT RUN THIS IN AS ROOT INSIDE ROOT DIRECTORY! rm -f -r *
When it comes to deleting folders, you will use the rmdir command (remove directory). This command will fail however if the folder is not empty, so if you are trying to remove a folder with files in it then you will want to delete the files inside first with one of the above commands. Here is an example of removing a directory.
This portion of the guide has several different commands that more or less do the same. You can use any of these to read the contents of files however for longer files you may want to use grep (will cover grep further into this guide).
The first method is using the cat command. Cat will simply display everything inside of the specified file and then exit, there is no pause if the file is longer than your terminal window. You can however scroll up with ctrl + page up if you are using putty and need to. Here is an example of reading a file using the cat command.
The next two commands more or less (pun intended) do somewhat the same thing. They allow you to view the contents of a file in chunks and scroll throw them. The commands are more and less, here are 2 examples of the commands in action.
more filename less filename
This next command comes in handy all the time when looking for a configuration file among other uses. To search for a file you will be using the find command. Here are a few examples of using the find command, it actually has many more features than this but we will try to keep this basic for now.
# Find files in current directory find . -name filename # Find files in specified directory find /etc -name filename # Find files (not case sensitive) find /etc -iname filename # Find all html files find /var/www/html -type f -name "*.html"
Searching File Contents
To search inside of a file for a specific string, for example searching for the term “error” inside of a log file, you can use the grep command. I wrote an entire guide for using grep if you are interested, for this guide I will just be covering basic linux commands. Here are 2 basic examples of using grep.
grep error /var/log/dmesg cat /var/log/dmesg | grep error
Next lets cover looking at logs. Your log files will usually be located in /var/log/ although some specific programs may log elsewhere. If you are having hardware issues on your server, usually you will want to look at your dmesg and messages (although some distros won’t log there) log files.
For dmesg you can run either of the following commands.
dmesg cat /var/log/dmesg
For your other logs such as messages, you can use the same command just a different file.
cat /var/log/messages cat /var/log/filename
Other Useful Commands
The Echo Command
The echo command does what its name implies, it echos your input back at you. While this may seem pointless at first, this is actually much more useful than it sounds when you get into using operators such as && (if previous command succeeded) and || (or). Here are some examples of echo in use.
echo “hello” cat /var/log/thiswillfail || echo “failed” cat /var/log/dmesg >/dev/null && echo “succeeded”
With Linux everything is permissions based, you can set them so that certain users or groups can’t access files or directories, you can set them so that users can read but not write to files, etc. I will try to keep this basic for the sake of this guide, if you are interested in learning more about permissions I will gladly write another guide strictly about setting permissions.
So lets get into actually understanding how permissions work and how to set them. So each file/directory/folder has an owner (account) and owner group. You can set this by using the chown command, below is an example of using the chown command.
chown username:groupname /path/to/filename
This is great if you want to do one file or even use a wildcard but if there are directories/folders inside of the directory/folder you are working out of then you need to use the -R argument. Here is an example of applying the chown command to all files and folders inside of the current folder.
chown -R username:groupname filename
Next we can set permissions at the file and folder level, for example we want everyone to be able to read files in the accounting directory but we don’t want anyone but the accountants group to be allowed to make changes.
The best way to learn how to set permissions is to use the octal method. This is done by using 3 digits, for example we can set it so that everyone can read, write, and execute all files in our folder using the chmod command. Here is an example of using the chmod command. Again I will try to keep this a basic commands guide, if you want more information on setting permissions then please comment below and I will write one.
chmod 777 /path/to/files/*
Rebooting The System
Rebooting a Linux server requires either sudo access or root account access. To do so you can use the reboot command as-well as a few other commands. Here is a list of commands that will reboot your system. Keep in mind that for both rebooting and shutting down your system, the ipmitool commands are not graceful and depending on your server may not even be supported. They will not wait for services to shutdown, they are the same as pressing the reboot button or pulling the plug on your PC.
sudo reboot sudo init 6 sudo ipmitool power chassis reset
Shut Down Your System
As with rebooting your system, you can perform a shutdown using multiple commands. Here is a list of commands that will shutdown your system.
sudo shutdown -h now sudo init 0 sudo ipmitool chassis power off
Checking Available RAM/CPU
You can check your available RAM and CPU using the top command or if installed, htop. There are a number of other ways in which you can check your available memory and CPU, I go into a bit more detail in my check available ram post. To open top, just run the following command, to close it and return to your terminal session just press ctrl+c.
If you just want to see how much free memory you have then you can run the free command. One thing to note is that this command rounds down, so if you check available gigabytes of memory and you have 0.9 free gigabytes left, then it will display 0 available ram. Here are 2 examples of the free command.
# Megabytes free -m # Gigabytes free -g
Check Your Uptime
You can see how long your server has been online by using the uptime command. The uptime command will also display your servers load averages (I will post about this another day). Here is the command for checking your uptime.
Here are some quick tips when working with the command line to make your life a little easier.
- When typing a file or directory name, press the tab button to automatically complete the name.
- If you would like to clear your terminal window then press ctrl+l.
- To exit any command or script that is currently running, press ctrl+c.
- You can use wildcards (*) which will include all files/folders in the specified directory.
- You can pause a process for example top with ctrl+z and resume it with fg.
- You can run a process in the background by adding an & to the end, for example top &.
- You can list all of your background processes by using the jobs command, and resume them with fg process id (ex. fg 1332).
I hope that you have enjoyed this guide, please don’t forget to like or share this post. If you have any questions or comments then feel free to leave them below. Thanks!