Linux Mint Essentials — Save 50%
A practical guide to Linux Mint for the novice to the professional with this book and ebook
In this article by Jay LaCroix, the author of the book "Linux Mint Essentials", has discussed about the importance and usage of the terminal. Mint is an incredibly rich experience, and it may seem that the developers have thought of everything and included a graphical application to configure just about anything you can think of. However, for those who aspire to be an administrator of Linux systems or just want to achieve advanced skills, learning shell commands is definitely recommended. For some tasks, executing commands can actually save time. In this article, we'll learn the basics of the Linux shell and commands related to it.
In this article, we will discuss the following topics:
- Why should we use the terminal?
- Accessing the shell
- Executing commands
- Navigating the filesystem
- Managing files
- The nano text editor
- Reading manual pages with the man command
- Searching for files
- Using the watch command
- Introduction to scripting
(For more resources related to this topic, see here.)
Why should we use the terminal?
With Mint containing a complete suite of graphical tools, one may wonder why it is useful to learn and use the terminal at all. Depending on the type of user, learning how to execute commands in a terminal may or may not be beneficial. If you are a user who intends to use Linux only for basic purposes such as browsing the Internet, checking e-mails, playing games, editing documents, printing, watching videos, listening to music, and so on, terminal commands may not be a useful skill to learn as all of these activities (as well as others) are best handled by a graphical desktop environment.
However, the real value of the terminal in Linux comes with advanced administration. Some administrative activities are faster using shell commands than using the GUI. For example, if you wanted to edit the /etc/fstab file, it would take fewer steps to type sudo nano /etc/fstab than it would to open a file manager with root permissions, navigate to the /etc directory, find the fstab file, and click on it to open it. This is especially true if all you want to do is make a quick change. Similarly, typing sudo apt-get install geany may be faster if you already know the name of the package you want, compared to opening up Mint Software Manager, waiting for it to load, finding the geany package, and installing it. On older and slower systems, the overhead caused by graphical programs may delay execution time.
Another value in the Linux Shell is scripting. With a script, you can create a text file with a list of commands and instructions and execute all of the commands contained within a single execution. For example, you can create a list of packages that you would prefer to install on your system, type them out in a text file, and add your distribution package's installation command at the beginning of the list. Now, you can install all of your favorite programs with a single command. If you save this script for later, you can execute it any time you reinstall Linux Mint so that you can immediately have access to all your favorite programs. If you are administering a server, you can create a script to check the overall health of the system at various times, check for security intrusions, or even configure servers to send you weekly reports on just about anything you'd like to keep yourself updated on. There are entire books dedicated to scripting, so we won't go in detail about it in this article. However, by the end of the article, we will create a script to demonstrate how to do so.
Accessing the shell
When it comes to Linux, there is very rarely (if ever) a single way to do anything. Just like you have your pick between desktop environments, text editors, browsers, and just about anything else, you also have a choice when it comes to accessing a Linux terminal to execute shell commands. As a matter of fact, you even have a choice on which terminal emulator to use in order to interpret your commands.
Linux Mint comes bundled with an application called the GNOME Terminal. This application is actually developed for a completely different desktop environment (GNOME) but is included in Mint because the Mint developers did not create their own terminal emulator for Cinnamon. The GNOME Terminal did the job very well, so there was no need to reinvent the wheel. Once you open the GNOME Terminal, it is ready to do your bidding right away. The following screenshot shows the GNOME terminal, ready for action:
As mentioned earlier, there are other terminal emulators that are available. One of the popular terminal emulators is Konsole. It typically comes bundled with Linux distributions, which feature the KDE environment (such as Mint's own KDE edition). In addition, there is also the xfce4-terminal, which comes bundled with the Xfce environment. Although each terminal emulator is generally geared toward the desktop environment that features it, there's nothing stopping you from installing them if you find that GNOME Terminal doesn't suit your needs. However, each of the terminal emulators generally function in much the same way, and you may not notice much of a difference, especially when you're starting out.
You may be wondering what exactly a terminal emulator is. A terminal emulator is a windowed application that runs in a graphical environment (such as Cinnamon in Mint) that provides you with a terminal window through which you can execute shell commands to interact with the system. In essence, a terminal emulator is emulating what a full-screen terminal may look like, but in an application window. Each terminal emulator in Linux gives you the ability to interact with that distribution's chosen shell, and as each of the various terminal emulators interact with the same shell, you won't notice anything unique about them regarding how commands are run. The differences between one terminal emulator and another are usually in the form of features in the graphical user interface, which surround the terminal window, such as being able to open new terminal windows in tabs instead of separate instances and even open transparent windows so that you can see what is behind your terminal window as you type.
While learning about Linux, you'll often hear the term Bash when referring to the shell. Bash is a type of command interpreter that Linux uses; however, there are several others, including (but not limited to) the C shell, the Dash shell, and the Korn shell. When you interact with your Linux distribution through a terminal emulator, you are actually interacting with its shell. Bash itself is a successor to Bourne shell (originally created by Stephen Bourne) and is an acronym for "Bourne Again Shell." All distributions virtually include Bash as their default shell; it's the closest shell to a standard one in terms of shells that Linux has. As you start out on your Linux journey, Bash is the only shell you should concern yourself with and the only shell that will be covered in this article.
Scripts are generally written against the shell environment in which they are intended to run. This is why when you read about writing scripts in Linux, you'll see them referred to as Bash Scripts as Bash is the target shell and pretty much the standard Linux shell.
In addition, terminal emulators aren't the only way to access the Linux shell for entering commands. In fact, you don't even need to install a terminal emulator. You can use TTY (Teletype) terminals, which are full-screen terminals available for your use, by simply pressing a combination of keys on your keyboard. When you switch to a TTY terminal, you are switching away from your desktop environment to a dedicated text-mode console. You can access a TTY terminal by pressing Alt + Ctrl and one of the function keys (F1 through F6) at the same time. To switch back to Cinnamon, press Alt + Ctrl + F8.
Not all distributions handle TTY terminals in the same way. For example, some start the desktop environment on TTY 7 (Alt + Ctrl + F7), and others may have a different number of TTYs available. If you are using a different flavor of Mint and Alt + Ctrl + F8 doesn't bring you back to your desktop environment, try Alt + Ctrl + F7 instead.
You should notice that the terminal number changes each time you switch between TTY terminals. For example, if you press Alt + Ctrl + F1, you should see a heading that looks similar to Linux Mint XX ReleaseName HostName tty1 (notice the tty number at the end). If you press Alt + Ctrl + F2, you'll see a heading similar to Linux Mint XX ReleaseName HostName tty2.
You should notice right away that the TTY number corresponds to the function key you used to access it. The benefit of a TTY is that it is an environment separate from your desktop environment, where you can run commands and large jobs. You can have a separate command running in each TTY, each independent of the others, without occupying space in your desktop environment. However, not everyone will find TTYs useful. It all depends on your use case and personal preferences.
Regardless of how you access a terminal in Linux to practice entering your commands, all the examples in this article will work fine. In fact, it doesn't even matter if you use the bundled GNOME Terminal or another terminal emulator. Feel free to play around as each of them handles commands in the same way and will work fine for the purposes of this article.
While utilizing the shell and entering commands, you will find yourself in a completely different world compared to your desktop environment. While using the shell, you'll enter a command, wait for a confirmation that the command was successful (if applicable), and then you will be brought back to the prompt so that you can execute another command. In many cases, the shell simply returns to the prompt with no output. This constitutes a success. Be warned though; the Linux shell makes no assumptions. If you type something incorrectly, you will either see an error message or produce unexpected output. If you tell the shell to delete a file and you direct it to the wrong one, it typically won't prompt for confirmation and will bypass the trash folder. The Linux Shell does exactly what you tell it to, not necessarily what you want it to. Don't let that scare you though. The Linux Shell is very logical and easy to learn. However, with great power comes great responsibility.
To get started, open your terminal emulator. You can either open the GNOME Terminal (you will find it in the application menu under Accessories or pinned to the left pane of the application menu by default) or switch to a TTY by pressing Ctrl + Alt +F1.
You'll see a prompt that will look similar to the following:
Let's take a moment to examine the prompt. The first part of the prompt displays the username that the commands will be executed as. When you first open a terminal, it is opened under the user account that opened it. The second part of the prompt is the host name of the computer, which will be whatever you named it during the installation. Next, the path is displayed. In the preceding example, it's simply a tilde (~). The ~ character in Linux represents the currently logged-in user's home directory. Thus, in the preceding prompt, we can see that the current directory that the prompt is attached to is the user's home directory. Finally, a dollar sign symbol ($) is displayed. This represents that the commands are to be run as a normal user and not as a root user.
For example, a user named C. Norris is using a machine named Neptune. This user opens a terminal and then switches to the /media directory. The prompt would then be similar to the following:
cnorris@neptune /media $
Now that we have an understanding of the prompt, let's walk through some examples of entering some very basic commands, which are discussed in the following steps. Later in the article, we'll go over more complete examples; however, for now, let's take the terminal out for a spin.
Open a prompt, type pwd, and press Enter. The pwd command stands for print working directory. In the output, it should display the complete path that the terminal is attached to. If you ever lose your way, the pwd command will save the day. Notice that the command prints the working directly and completes it. This means that it returns you right back to the prompt, ready to accept another command.
Next, try the ls command. (That's "L" and "S", both lowercase). This stands for list storage. When you execute the ls command, you should see a list of the files saved in your current working directory. If there are no files in your working directory, you'll see no output.
For a little bit of fun, try the following command:
cowsay Linux Mint is Awesome
This command shows that the Mint developers have a sense of humor and included the cowsay program in the default Mint installation. You can make the cow say anything you'd like, but be nice. The following screenshot shows the output of the preceding cowsay command, included in Mint for laughs:
Navigating the filesystem
Before we continue with more advanced terminal usage, it's important to understand how the filesystem is laid out in Linux as well as how to navigate it. First, we must clarify what exactly is meant by the term "filesystem" as it can refer to different things depending on the context. If you recall, when you installed Linux Mint, you formatted one or more partitions with a filesystem, most likely ext4. In this context, we're referring to the type of formatting applied to a hard-disk partition. There are many different filesystems available for formatting hard disk partitions, and this is true for all operating systems.
However, there is another meaning to "filesystem" with regards to Linux. In the context of this article, filesystem refers to the default system of directories (also known as folders) in a Linux installation and how to navigate from one folder to another. The filesystem in an installed Linux system includes many different folders, each with its own purpose. In order to understand how to navigate between directories in a Linux filesystem, you should first have a basic understanding of what the folders are for.
You can view the default directory structure in the Linux filesystem in one of the following two ways:
One way is to open the Nemo file manager and click on File System on the left-hand side of the window. This will open a view of the default folders in Linux, as shown in the following screenshot:
Additionally, you can execute the following command from your terminal emulator:
ls -l /
The following screenshot shows the output of the preceding command from the root of the filesystem:
The first point to understand, especially if you're coming from Windows, is that there is no drive lettering in Linux. This means that there is no C drive for your operating system or D drive for your optical drive. The closest thing that the Linux filesystem has for a C: drive is a single forward slash, which represents the beginning of the filesystem. In Linux, everything is a subdirectory of /. When we executed the preceding command (ls -l /), we were telling the terminal emulator that we'd like a listing of / or the beginning of the drive. The -l flag tells the terminal emulator that we would like a long alphabetical listing rather than a horizontal one.
Paths are written as shown in the following command line example. In this example, the path references the Music directory under Joe's home directory:
The first slash (/home) references the beginning of the filesystem. If a path in Linux is typed starting with a single forward slash, this means that the path starts with the beginning of the drive. In the preceding example, if we start at the beginning of the filesystem, we'll see a directory there named home. Inside the home folder, we'll see another directory named joe. Inside the joe directory, we'll find another directory named Music.
The cd command is used to change the directory from the current working directory, to the one that we want to work with. Let's demonstrate this with an example. First, let's say that the prompt Joe sees in his terminal is the following:
joe@Mint ~ $
From this, we can deduce that the current working directory is Joe's home directory. We know this because the ~ character is shorthand for the user's home directory. Let's assume that Joe types the following:?
Then, his output will be as follows:
In his case, ~ is the same as /home/joe.
Since Joe is currently in his home directory, he can see the contents of that directory by simply typing the following command:
The Music directory that Joe wants to access would be shown in the output as its path is /home/joe/Music.
To change the working directory of the terminal to /home/joe/Music, Joe can type the following:
His prompt will change to the following:
joe@Mint ~/Music $
However, the cd command does not make you type the full path. With the cd command, you can type an absolute or relative path. In the preceding command line using cd command, we referenced an absolute path. The absolute path is a path from the beginning of the disk (the single forward slash), and each directory from the beginning is completely typed out. In this example, it's unnecessary to type the full path because Joe is already in his home directory. As Music is a subdirectory of the directory he's already in, all he has to do is type the following command in order to get access to his Music directory:
That's it. Without first typing a forward slash, the command interpreter understands that we are referencing a directory in the current working directory. If Joe was to use /Music as a path instead, this wouldn't work because there is no Music directory at the top level of his hard drive.
If Joe wants to go back one level, he can enter the following command:
Typing the cd command along with two periods tells the command interpreter that we would like to move backwards to the level above the one where we currently are. In this case, the command would return Joe back to his home directory.
Finally, as if the difference between a filesystem in the context of hard drive formatting and filesystem in the context of directory structure wasn't confusing enough, there is another key term you should know for use with Linux. This term also has multiple meanings that change depending on the context in which you use it. The word is root.
The user account named root is present on all Linux systems. The root account is the Alpha and Omega of the Linux system. The root user has the most permissions of any user on the system; root could even delete the entire filesystem and everything contained within it if necessary. Therefore, it's generally discouraged to use the root account for fear of a typo destroying your entire system. However, in regards to this article, when we talk about root, we're not talking about the root user account. There are actually two other meanings to the word root in Linux in regards to the filesystem.
First, you'll often hear of someone referring to the root of the filesystem. They are referring to the single forward slash that represents the beginning of the filesystem. Second, there is a directory in the root of the filesystem named root. Its path is as follows:
Linux administrators will refer to that directory as "slash root", indicating that it is a directory called root, and it is stored in the root (beginning) of the filesystem. So, what is the /root directory? The /root directory is the home directory for the root account. In this article, we have referred to the /home directory several times. In a Linux system, each user gets their own directory underneath /home. David's home directory would be /home/david and Cindy's home directory is likely to be /home/cindy. (Using lowercase for all user names is a common practice for Linux administrators). Notice, however, that there is no /home/root. The root account is special, and it does not have a home directory in /home as normal users would have. /root is basically the equivalent of a home directory for root. The /root directory is not accessible to ordinary users. For example, try the following command:
The ls command by itself displays the contents of the current working directory. However, if we pass a path to ls, we're telling ls that we want to list the storage of a different directory. In the preceding command, we're requesting to list the storage of the /root directory. Unfortunately, we can't. The root account does not want its directories visible to mortal users. If you execute the command, it will give you an error message indicating that permission was denied.
Like many Ubuntu-based distributions, the root account in Mint is actually disabled. Even though it's disabled, the /root directory still exists and the root account can be used but not directly logged in to. The takeaway is that you cannot actually log in as root.
So far, we've covered the /home and /root subdirectories of /, but what about the rest? This section of the article will be closed with a brief description of what each directory is used for. Don't worry; you don't have to memorize them all. Just use this section as reference.
/bin: This stores essential commands accessible to all users. The executables for commands such as ls are stored here.
/boot: This stores the configuration information for the boot loader as well as the initial ramdisk for the boot sequence.
/dev: This holds the location for devices to represent pieces of hardware, such as hard drives and sound cards.
/etc: This stores the configuration files used in the system. Examples include the configuration for Samba, which handles cross-platform networking, as well as the fstab file, which stores mount points for hard disks.
/home: As discussed earlier in the article, each user account gets its own directory underneath this directory for storing personal files.
/lib: This stores the libraries needed for other binaries.
/media: This directory serves as a place for removable media to be mounted. If you insert media (such as a flash drive), you'll find it underneath this directory.
/mnt: This directory is used for manual mount points; /media is generally used instead, and this directory still exists as a holdover from the past.
/opt: Additional programs can be installed here.
/proc: Within /proc, you'll find virtual files that represent processes and kernel data.
/root: This is the home directory for the root account.
/sbin: This consists of super user program binaries.
/tmp: This is a place for temporary files.
/usr: This is a directory where utilities and applications can be stored for use by all users, but it is not modified directly by users other than the root user.
/var: This is a directory where continually changing files, such as printer spools and logs, are stored.
|A practical guide to Linux Mint for the novice to the professional with this book and ebook|
eBook Price: $29.99
Book Price: $49.99
Now that you have an understanding of how to navigate the filesystem, it's time to take a look at some basic file management. After all, what good is accessing a terminal if you don't know how to create, delete, move, and rename files and directories? Once you learn the basics of navigating the filesystem, the rest will be easy. File management in the Linux Shell is very logical but also very important to learn. Take some time to go through the following examples to manage some files on your system.
First, let's take a look at creating a file. There is more than one way to do this, but the following command line accomplishes the goal very easily:
With the touch command, we created a new file named myfile. The touch command will create a file in your working directory. Thus, if your working directory was your home directory, you'll now have a file called myfile in that directory.
However, the file that it created isn't very useful, is it? In fact, the file is completely empty. In the next section, we'll go over how to modify this file and add content to it. Being able to create files in Linux is an important first step, and you have successfully done so with the touch command.
If you execute the touch command against a file or directory that already exists, the modified time of the file or directory will be updated, though its contents would not have changed. This is useful if you are using a backup program that looks for files with a certain modification time. Thus, you can execute the touch command against a file or directory that already exists to update the modification time and trigger a backup of that item.
So, what if you wanted to remove the file you created in the previous step? This is also very easy. The following rm command will allow you to easily remove the file:
So, what about directories? It's just as easy to create a directory instead of a file using the following command:
If we want to remove the directory later, we can do so with the rm command but with the -r flag added to it. Keep in mind though; if you delete a directory, you delete everything inside it as well. The command line to remove the directory is as follows:
rm -r myfolder
Now you know how to create files and directories as well as how to remove them. What about renaming files or moving a file or directory from one place to another? To set up a walkthrough of further manipulating files and directories, let's create some files and directories to work with. They are as follows:
touch myfile touch myfile2 mkdir myfolder mkdir myfolder2
Now, we have some files and directories to play around with. First, let's rename myfile to myfile1 to make it look better. The mv command will allow you to move from its old name to a new one. This can be done using the following command line:
mv myfile myfile1
If you execute ls to list the storage in your working directory, you'll see that there is no file named myfile anymore; the preceding command moved it to myfile1. We can also rename directories in the exact same way using the following command line:
mv myfolder myfolder1
Renaming files and directories isn't the only thing that can be done with the mv command. In addition, you can move a file from one place to another. Let's move myfile1 into myfolder1:
mv myfile1 myfolder1
Now, myfile1 is no longer in the working directory. It now resides inside myfolder1. You can confirm this by typing the following command:
You can also move a folder into another directory using the following command:
mv myfolder1 myfolder2
As you can see, the mv command takes care of the logic for you. You don't have to clarify whether the file you are moving or the destination is a file or directory. It's able to figure it out by itself. Type the following command in the terminal:
mv myfolder2 myfile2
You would get an error. As myfile2 is a file and not a directory, you cannot move a directory into it.
In the preceding examples, we used the rm and mv commands with files and directories that existed in our working directory. It's important to note that you are not required to be within the directory where the files are located in order for you to be able to modify them. Both the mv and rm commands accept path arguments as well. For example, if you wish to delete a file called mydocument contained under /home/users/Documents, but your working directory was some other path, you could type the following command line:
As ~ is shorthand for /home/user, you can simplify the command line even further, as follows:
Many commands accept path arguments in much the same way. Once you master relative paths, absolute paths, and how to navigate around, you're well on your way to becoming a shell guru!
The nano text editor
Until now, we have used single purpose commands to achieve very basic goals such as creating empty files and directories. In the Linux Shell, there is more to life than simple commands. There are actually complete programs, which are able to run completely within your shell window. One example is nano, which is a text editor. There are many text editors available (such as vi and emacs), but nano is quite possibly the easiest to learn when starting out. There are two ways to open the nano text editor. The first way is by simply entering the nano command in the terminal.
The nano text editor immediately opens and you are able to start typing. However, when you enter the nano command by itself, you aren't actually editing a file until you save it. To save the file, press Ctrl + O (the letter O) and you are prompted to save the file. If you simply type the name, such as myfile.txt, the file will be saved to wherever your current working directory is. For example, if your working directory was /home/user/mydocs when you entered the command and you saved the file as myfile.txt, it would create the myfile.txt file underneath /home/user/mydocs.
If you already know the name of the file you want to create and where you'd like to save it, you can type all of it into a single command, as shown in the following command:
With this command, nano will open as normal. However, when you press Ctrl + O in order to save the file, it will default to the path and filename you specified. If the file already existed when you entered that command, the contents of the file would be displayed on your screen, and you'd be able to modify it.
Feel free to play around with the nano text editor as it is very easy (and useful) to learn. Files are edited in nano in much the same way as graphical text editors by pressing the Enter key to move to a new line, the arrow keys to move your insertion point around the document, the Backspace key to delete characters, and the Tab key does exactly what you'd expect. As mentioned earlier, Ctrl + O brings up the save dialog. After you press Ctrl + O, confirm the name you'd like to use and press Enter to finalize the save process. To cut some text in order to paste later, press Ctrl + K to cut the line and then press Ctrl + U to move the line to where the insertion point currently is. To exit nano, press Ctrl + X.
Not all Linux distributions ship nano, especially older distributions or server-based platforms. If your goal is to become a Linux administrator, you should learn another terminal-based text editor as well. A good recommendation is the vi text editor, which is more advanced but very common in the field. You may run into a situation where vi is the only option on the server you are working on.
Reading manual pages with the man command
So far, you've learned several very useful commands to form the basis of your terminal skills. However, we haven't yet gone over the most important command of all—the man command.
The most important skill that any Linux administrator will ever learn is how to find useful information when in a jam. Being resourceful in the face of disaster is what separates hobbyists from professionals. In today's age, there's a wealth of information available at your fingertips. When faced with a nasty error message, often a quick Google search will find an online posting where someone has already been through the same problem and may have typed a response indicating what the solution is. In the worst case scenario, you may stumble across a bug report instead and discover that your problem is a known issue, and the developers of the software are already working on resolving it.
When you don't have the comfort of an Internet browser by your side or you'd like to quickly look up some details on a specific command, ask the man. The man command (short for manual) is one of your biggest allies in the Linux world. Knowing how to use it will help make you resourceful. You'd be surprised how much information the man command can provide. To use it, all you have to do is execute the man command and use another shell command as an argument. After you are done, simply press q on your keyboard to exit.
For example, try executing the man command against the ls command, as follows:
The following screenshot shows the output of the man command:
As you can see, there is much more to the ls command than what has been discussed in this article. There are many arguments that you can pass along to the ls command to change the way the results are displayed. For example, from the man entry for ls, you will discover that you can pass the -a argument to ls (so the command becomes ls -a) to view hidden files along with the rest of the output.
Files or directories that begin with periods, also known as dot files, are hidden. These files will neither show up in the normal ls output in the shell nor will they appear in the Nemo file manager unless you explicitly configure it to show hidden files. As mentioned in the man page for ls, you can use the -a flag to show hidden files, or you can view hidden files in Nemo by enabling Show Hidden Files in the View menu. Try this in your home directory, and you will see that there are many more files there than you might have thought.
Feel free to try the man command against other commands and view the output. In fact, you can even discover more about the man command itself by executing it:
However, not all commands have manual entries. For example, you can try the following command:
However, it won't work. The manual entry for the cd command is not included in Linux Mint. Feel free to give the man command a try and see what manual entries you're able to come up with.
Searching for files
Now that you've had a crash course on how to create and manage files, you probably have quite a few files all over the hard disk that you've created. However, what do you do when you want to update a file, but you've forgotten where it is? The find command comes to your rescue.
The find command will allow you to search your filesystem for files based on the search criteria. To use it, you type find, a path to start the search from, the search criteria, and then the name of the file. For example, consider the following command line:
find / -name myfile
In the preceding example, we chose to start our search in / (the beginning of the filesystem). We are searching for a specific name (-name), and the name we're looking for is myfile. After executing the command, a search will be conducted for the file, and the output returned is the full path of the file once (and if) it's found.
However, you are more likely to see one or more errors when executing the preceding command. As we started our search in /, the search would have been conducted in directories that a normal user may not have access to. Therefore, we can narrow down the search by starting it further up the filesystem tree using the following command:
find /home/user -name myfile
This is better. The search should be faster as we're not searching the entire hard disk for the file that we're only looking for in our home directory.
The find command is also very useful from a system administrator's standpoint. For example, if you wanted to edit a configuration file in /etc, but you weren't sure of where exactly it was located, you can search for the file. If you were looking for the smb.conf file, the find command would find it under /etc/samba/smb.conf. You would then know where the file that you'd like to edit is.
Sometimes, you may not know the actual filename you're trying to find. Perhaps you were working on a file during the last week, but you don't remember what you named it or where you saved it. The find command can still save the day. Instead of passing the -name option, you can pass -mtime (stands for modified time) instead. Let's take a look at the following example:
find /home/user -mtime -7
In the preceding example, we're looking for all files contained in /home/user that were modified seven or less days ago. Unfortunately, if you use a web browser or similar software which saves its local configuration in your home directory, this command will likely display a large list of junk files. If you also know the file extension you used, you can narrow the results down even further.
find /home/user -mtime -7 -name *.txt
In this example, we used the same find command with –mtime, as used in the preceding command, but we also appended –name, as done in our first example. The *.txt portion returns all files that end with the .txt file extension. After you put it all together, you're essentially searching for all files in your home directory that were modified seven or less than seven days ago and have a filename ending in .txt.
There are other variations of the find command. Check out the main page for the find command and experiment with finding other files in your filesystem.
Using the watch command
Another command that is very useful for system administration is the watch command. Even better is the fact that the watch command is extremely easy to learn as what it does is simple. It repeats the command typed immediately after it every two seconds by default. For example, you can view the output of the ls command every two seconds by typing the following command:
watch ls /home/user
This is not very exciting, is it? To illustrate what ls does, open another terminal window. In that window, create a new file in your home directory. You should see the new file appear in the output of the first window. If you delete the file, you'll see the file disappear from the watch output.
To break out of the watch command, press Ctrl + C on your keyboard.
While this may be useful for monitoring a single directory, it may not be a very exciting example. For an even better example, try the following command:
As long as your motherboard supports it, the sensors command will output the current temperature of your CPU. Some (but not all) computers will display the fan rotation speed as well. You've just printed the temperature of your CPU to your terminal. However, it would be more useful to have the output automatically updated without having to execute the sensors command again. As you would have guessed, the watch command can automatically update the output of sensors every two seconds. This can be achieved using the following command line:
In the following screenshot, the watch and sensors commands are used to monitor the system temperature:
Now, this is more useful. If you were running a very CPU-intensive program and wanted to keep an eye on the temperature of your processor, this is one way to do so. However, of course, it gets better. Two seconds is still a bit of a delay. Let's update the output more frequently using the following command line:
watch -n 1 sensors
By adding the -n option, we can change the number of seconds in which the output of watch updates. In the preceding example, we tell the watch command that we'd like it to update every second rather than the default two seconds. Now, the output of the sensors command is much more useful.
You can use the watch command against virtually any shell command with varying results. In fact, there may be a time where even updating every second is too slow. Take the following command for example:
The date command simply prints the current date and time. If you were to do so with the watch command with updates every second, which is shown in the following command line, the results may not be very reliable:
watch -n 1 date
Every second is pretty close, but not exact to the clock. You aren't limited to seconds with the watch command. You can update in fractions of seconds as well, using the following command:
watch -n 0.1 date
With the preceding command, our output is closer to real time. It may not be 100 percent perfect, but it's good enough to suit any purpose that would require monitoring the time.
Introduction to scripting
Now that you are beginning to grasp how to enter shell commands, it's a good time to introduce scripting. Although this article will not go into scripting in great detail, covering the very basics of it here will help you study scripting should you decide to do so.
If you do decide to further your scripting skills (it's a very useful skill for you to learn), there are many books available that are dedicated to this very subject. One such book is Linux Shell Scripting Cookbook, Second Edition, Shantanu Tushar and Sarath Lakshman, Packt Publishing.
Bash scripts are actually just text files. So, to get started, you'll need to fire up a text editor. You can use whichever text editor you'd like, such as nano (discussed earlier in this article) or even gedit, which is a graphical text editor that ships with Mint. Scripts are executed from the terminal, so you may want to use nano to edit your scripts as you will end up in the terminal at some point during this process anyway.
To get started, type the following command into your text editor:
What you just typed is known as a hashbang. This is the first line that should be typed into any script you create. In the hashbang, we declare which interpreter we'd like to use to execute the commands that will follow. In most Linux distributions, Bash is primarily the only option, so you're unlikely to diverge from this instruction.
Next, we can type any command we would like in the lines following. You can have a single command per line or as many commands as you wish.
For example, you can add the ls command to the next line. Now, your script will look as follows:
To execute the script, save it and then mark it as executable. To do so, exit the text editor, and execute the following command:
chmod +x myscript
Be sure to change myscript to whatever you named your script. Now, to execute it, simply type the following command:
In the preceding command, we executed the script that we created. However, rather than just simply typing the name of the script, we prefixed it with a period and forward slash. The reason we did this is because by default, when you type a command (a script is treated as a command), the interpreter will look for the command to be located in folders such as /bin, /usr/bin, /usr/local/bin, and so on. By prefixing the name of the script with a period and forward slash, we're telling the terminal emulator that we want to execute a command (script) that is stored in our current working directory. If you missed the period and forward slash, you would have received the following error:
bash: myscript: command not found
Once you execute the script, you should see the contents of your current directory. This is because the script simply executes the ls command. Unfortunately, this isn't very useful, is it? It's much easier to type ls than it is to type ./myscript. This script saves us no time at all. We can make a more useful script by removing ls from within the script and replacing it with the output of the sensors command. For example, try the following script instead:
#!/bin/bash watch -n 0.1 sensors
Save the script with a simple name (such as cputemp) and mark it executable, as described earlier. Now, each time you execute ./cputemp, you'll see an almost real-time representation of your CPU temperature. Now, this is useful.
From here, Bash scripting becomes more and more complex. In fact, there's little that separates Bash from actual programming languages as you have access to utilize variables, branching statements, and many other advanced techniques to create programs that can automate complex tasks.
In this article, we went over quite a bit of information regarding the Linux Shell and how to execute commands. We started with some reasons on why you'd want to use the terminal in the first place and then we discussed the Linux filesystem and how to navigate it. The nano text editor was also shown, which is a great text editor to use from within your terminal to create and edit text files. We also discussed basic file management, how to search for files, the watch command, and how to create a simple script.
Resources for Article:
- Installing Arch Linux using the official ISO [article]
- Linux Desktop Environments [article]
- Web app penetration testing in Kali [article]
|A practical guide to Linux Mint for the novice to the professional with this book and ebook|
eBook Price: $29.99
Book Price: $49.99
About the Author :
Jay LaCroix is a Linux Administrator with over 12 years of experience and nine certifications. He is a technologist who enjoys all things tech, including (but not limited to) hardware, software, servers, networking, and development. When Jay is not buried in a plethora of computer books, he enjoys photography, music, gaming, and writing. Jay is passionate about open source software, especially Linux, and its long-term adoption.
Jay is also the proud author of the self-published Sci-Fi novel, Escape to Planet 55.