Basic Linux Commands
Working on the command line is an integral part of being a successful Linux user. You need to have a firm grasp of certain commands to work effectively. There are even certain commands that you must memorize since they will be used more often than others. It is even more important for those working on Linux servers. For servers, you will almost always work on the command line. Here are some of the most important commands you need to have an understanding of to be successful with Linux.
What Linux Commands Are
Before you get started working on the command line, you need to have an understanding of what Linux commands are. The Linux operating system is based on UNIX. One of the most important things that Linux inherited from UNIX is its philosophy on programs. In Windows, you work with big hefty programs that do a lot of things. Linux takes the alternative route, which means you have thousands of smaller programs that are really good at doing one thing. To do major things, you have to create shell scripts that combine many of the smaller programs together to perform a task. Therefore, you need to have an understanding of how these command work, and you also need to understand how to combine them to do bigger things.
A Linux shell command is a self-contained program. It is the same as an executable program on Windows. These shell programs run in User space on top of the kernel. Some of them even have one to one functions compared to what the kernel does. When working in Linux, the most common tasks you will need to do are file management tasks. Therefore, we will start with those commands.
To fully utilize Linux commands, you need to understand how to pass arguments. Arguments are passed by entering the command and then using the "-" symbol. For most commands, you will need to enter the argument with the relevant commands to perform the exact task you want.
Working with Files
ls (List Files) and ls -R (List files including subdirectories)The 'ls'
command is responsible for listing files in a directory. Think of it as double-clicking on a folder in Windows. You enter this command, and it tells you all files and directories inside the current directory. It is a command you should know by heart since it is so ubiquitous in everything you do on the Linux command line. There are various arguments you can pass to this command, such as 'ls -R'
, which will show you directories and subdirectories.
The caveat to the 'ls'
command is it does not show you hidden files. These are files that start with the "." (period) symbol. To show these files, you need to use the 'ls -a'
command.
The 'ls -al'
command provides detailed info on each and every file in the form of a column.
While viewing files is a great thing, you need to know how to create and manipulate these files. The 'cat'
command is how you create a file. You can enter 'cat > filename'
which will create a new file called filename in the current directory. The cat
command also allows you to combine two files into one using the '>'
symbol. To do this, you would enter 'cat file1 file2 > newfilename'
. The result of this command is a single file with the concatenated results of first and second. You should realize that this program will never let you know that the operation ended successfully. However, it will show you an error in case something fails. It is up to you to check to make sure that the operation succeeded. After you've created a file, you can view it by entering the command 'cat filename'
.
Once you have learned how to create files, the next step is learning how to delete and move them.
You can delete a file using the 'rm'
command. You simply write 'rm filenametobedeleted'
to remove the file. This command can be very dangerous if you're not careful. Linux will not tell you when you are deleting an important file. If you've ever deleted an essential file for the operating system, you could be in a world of trouble. Make sure you take caution when working with this command.
You can rename and move files using the 'mv'
command. The syntax is what you would expect from a Linux command. You simply enter 'mv file newlocation'
, and you have successfully moved one file to a new location.
For example, if you wanted to move a file called "example1" to this location: /admin/linuxjournal/Home then you would enter 'mv example1 /admin/linuxjournal/Home'
A potential issue that can come up when moving a file is you don't have permission to do so. To fix that problem, you would use the 'sudo'
command. This command allows you to enter another command as the root user. As you may know, root is the administrator on a Linux system. This user has permission to read and write everything. However, it may not be advisable to work in a Linux system under the root user. If you ever get a permission error, then you will need to enter the same command but with sudo
in front of it.
Another thing you should know about the sudo
command is it maintains a log of all the times it was invoked. As a system administrator, you can go in and track when someone has done something wrong.
To move a file from one location to the next, you do the same thing as you would with the 'mv'
command.
This is all you need to know for manipulating files. These commands will usually get you to a good enough place that you only need to do basic research to do other things. The next step is to look at how to manipulate entire directories. These are the equivalent to folders in a Linux system.
Working with Directories
mkdir (Make a directory)A directory can be created by using the 'mkdir'
command. This command stands for make directory. You simply enter 'mkdir directoryname'
and a new directory is created. Invoking this command creates a new directory inside the directory you are currently in. If you don't have permission to create a directory in the current place, then the command will throw a permissions error. If you want to make a directory in a specific place, then you would need to invoke the 'mkdir /location/where/directory/should/be'
command. As usual, it will throw an error if you don't have permission to create anything in this directory. You can create multiple directories using one command. For example, you can do something like 'mkdir folder1 folder2 folder3'
to create these three directories at the same time.
To remove a directory, you would use the 'rmdir'
command. This command is responsible for deleting a directory from the file system. You would accomplish this by entering 'rmdir folder1'
then pressing 'y' when it asks if you are sure you want to do this. Linux is pretty good about asking you to confirm when you delete files.
You can rename a directory by using the same command as above. This is the 'mv'
command. You can rename a directory in the same format by entering 'mv folder1 folder2'
which would rename folder1 to folder2.
These are the foundational commands necessary to have a basic and proficient knowledge of Linux.
Working With Processes
Other than files, the most important thing you will be working with within Linux is a process. A process is a name given to a program that runs continuously in the background. For example, on a server, your database and server application would run continuously in the background. There are also built-in Linux processes that are called daemons. You will need to understand how to manipulate and manage these programs to be a successful Linux user.
top (Display running processes)The first command you need to know when it comes to these processes is the 'top'
command. This command is responsible for telling you about running processes, and showing you which ones are using the most resources. It is a simple and easy way for you to check if there is a problem with a process. Depending on your Linux distribution, you can also use the 'service processname status'
command. This command will tell you the status of a process on the system. When trying to fix a problem with the system, you need to remember how to use this command.
The Manual Command
man (Show the manual for a command)You will inevitably need to read the manual while using Linux. The 'man'
command allows you to read the manual pages for a variety of commands or programs. This is a simple program to use. You enter 'man commandname'
and it will show you all the commands available for that command.
Showing the Command History
history (Show the command history)You might also want to look at a command history inside your Linux session. The command history will tell you all the commands that have been executed in the current session. You can use it to see which commands to use again. To see a history of commands, you simply enter the 'history'
command.
Along with showing a command history, you can also clear a window using the 'clear'
command. This command returns your terminal to how it was when you first logged in.
Installing Software
yum, apt-get, aptitude (Package manager ([Depends on your Distro])You will inevitably have to install software from the Linux command line. The program that you use to manage and install software is called the package manager. The package manager will vary depending on the distribution you are using. It is crucial for you to know all the commands your current distribution supports. Examples of package management software include yum
, apt-get
, and aptitude
. All three of these package managers will allow you to install, update, and remove applications from your system.
Sending and Receiving Mail
mail -s 'subject' - c 'cc' -b 'bcc' 'to' (Send an email using your local SMTP server)You might also want to send mail using your Internet-facing Linux server. You can send mail by using the mail -s 'subject' -c 'cc-address' -b 'bcc-address' 'to-address'
string. This command will use your default SMTP server to deliver mail to anyone on the Internet. Email is one of the most complicated things to manage on a server, so you should do more research to figure out how to get your mail delivered correctly. However, the most important thing to know is the receiving server must whitelist your IP address. You also need to configure the SMTP server correctly to make sure that you always reach the inbox of the person you are trying to email.
Printing in the Terminal
pr (Print formatted output to the terminal) lp or lpr (Print formatted output to your printing device)While using Linux, you will inevitably want to print to the terminal in a formatted way. After formatting the file in the terminal, you can then print to a physical printer. The starting place for doing all of this is the 'pr'
command. This command takes in certain parameters that are responsible for dividing the file into columns and also working with the header. You also have the option of adding line numbers to the file. When you are done formatting the file, you can use the lp
or lpr
command to print to an actual printer. Before you can do this, the printer needs to be configured to work with your Linux system. Once this has been completed, you can then continue as normal with the printing.
Other Important Commands You Should Know
While these might be the most important things for you to know, there are a few associated programs that you should know about as well.
The first thing you should know about is all the default locations for your specific distribution. Linux distributions vary widely in how they come configured. For example, Ubuntu doesn't allow you to use the root user out-of-the-box. You can only operate superuser commands using the sudo
command. These defaults are useful to you when looking for configuration files. It will help you dramatically when doing basic maintenance and troubleshooting on your Linux machine.
Finally, the most important thing you need to have a complete understanding of is your text editor. You will spend a lot of time in your text editor, creating shell scripts and text files. You also spend a lot of time in your editor reading configuration files for default programs. You should have a clear understanding of how to do things in your text editor, so you don't spend a lot of time trying to navigate a large configuration file.