Reader small image

You're reading from  Mastering Ubuntu Server - Fourth Edition

Product typeBook
Published inSep 2022
PublisherPackt
ISBN-139781803234243
Edition4th Edition
Concepts
Right arrow
Author (1)
Jay LaCroix
Jay LaCroix
author image
Jay LaCroix

Jeremy "Jay" LaCroix is a technologist and open-source enthusiast, specializing in Linux. He has a net field experience of 20 years across different firms as a Solutions Architect and holds a master's degree in Information Systems Technology Management from Capella University. In addition, Jay also has an active Linux-focused YouTube channel with over 250K followers and over 20M views, available at LearnLinuxTV, where he posts instructional tutorial videos and other Linux-related content. He has also written Linux Mint Essentials and Mastering Linux Network Administration, published by Packt Publishing.
Read more about Jay LaCroix

Right arrow

Boosting Your Command-line Efficiency

Throughout this book, we’ve been using the command line quite heavily. Using the shell, we’ve installed packages, created users, edited configuration files, and more. In the last chapter, we took a look at file management to enhance our terminal skills further. This time around, we’ll dedicate an entire chapter to the shell, with the goal of becoming more efficient with it. Here, we’ll take what we already know and add some useful time-saving tips, some information on looping, variables, and we’ll even look into writing scripts.

In this chapter, we will cover the following topics:

  • Understanding the Linux shell
  • Understanding Bash history
  • Learning some useful command-line tricks
  • Understanding variables
  • Writing simple scripts
  • Putting it all together: Writing an rsync backup script

Let’s begin this chapter with further discussion regarding the Linux shell...

Understanding the Linux shell

When it comes to the Linux shell, it’s important to understand what exactly the term pertains to. We’ve been using the command line repeatedly throughout the book, but we haven’t yet had any formal discussion about the actual interface through which our commands are entered.

Essentially, we’ve been entering our commands into a command interpreter known as the Bourne Again Shell, or simply Bash. Bash is just one of many different shells that you can use to enter commands.

There are other options, including Zsh, Fish, and ksh, but Bash is the default command shell for the majority of Linux distributions. It’s even available on macOS (although the default on that platform is Zsh nowadays), as well as on Windows by installing the Windows Subsystem for Linux. Therefore, by understanding the basics of Bash, your knowledge will be compatible with other distributions and platforms. While it’s fun to learn other...

Understanding Bash history

Speaking of history, let’s dive right into that concept. By default, Bash keeps track of all the commands you enter during your sessions, so that if you need to recall a previously entered command, you can definitely do so. History also serves another purpose, and that is seeing what other users have been up to. However, since users can edit their own history to cover their tracks, it’s not always useful for that purpose.

You may have already seen Bash’s history feature in some form, if you’ve ever pressed the up and down arrows on the shell to recall a previously used command. If you didn’t already know you could do that, go ahead and give it a try. You should see that by pressing the up and down arrows, you can cycle through commands that you’ve used previously.

Another trick is that you can also simply type history in the shell and see a list of previously entered commands, as shown in Figure 6.2:

...

Learning some useful command-line tricks

Productivity hacks utilizing the shell are some of my favorite things in this world, right up there with music, video games, and Diet Pepsi. There’s nothing like the feeling you get when you discover a useful feature that saves you time or increases your efficiency. There are many things I’ve discovered along the way that I wish I had known earlier on. One of my goals while writing this book is to teach you as many things as I can that took me longer to learn than I’m comfortable admitting to. In this section, in no particular order, I’ll go over a few tricks that increased my workflow.

First, entering !! (two exclamation marks) in your terminal will repeat the command you last used. By itself, this may not seem like much. After all, you can press the up arrow key once and press Enter to recall the previous command and execute it. But, when paired with sudo, !! becomes more interesting. Imagine for a moment that...

Understanding variables

Bash is more than just a shell. You could argue that it is very similar to a complete programming language, and you wouldn’t be wrong. Bash has a built-in scripting engine (we will get into scripting later) and there are many debates as to what distinguishes a scripting language from a programming language, and that line becomes more and more blurred as new languages come out.

As with any scripting language, Bash supports variables. The concept of variables is very simple in Bash, but I figured I’d give it its own (relatively short) section to make sure you understand the basics. You can set a variable with a command such as the following:

myvar='Hello world!'

When Bash encounters an equal sign after a string, it assumes you’re creating a variable. Here, we’re creating a variable named myvar and setting it equal to Hello world! Whenever we refer to a variable, though, we need to specifically clarify to Bash...

Writing simple scripts

This is the section where everything we’ve talked about so far starts to come together. Writing scripts can be very fun and rewarding, as they allow you to automate large jobs or just simplify something that you find yourself doing over and over. The most important point about scripting is this: if it’s something you’ll be doing more than once, you really should be making it into a script. This is a great habit to get into.

A script is a very simple concept; it’s just a text file that contains commands for your shell to execute one by one. A script written to be executed by Bash is known as a Bash script, and that’s what we’ll work on creating in this section.

At this point, I’m assuming that you’ve practiced a bit with a text editor in Linux. It doesn’t matter if you use Vim or Nano. Since we’ve edited text files before (we went over that in Chapter 5, Managing Files and Directories...

Putting it all together – Writing an rsync backup script

Let’s close this chapter with a Bash script that will not only prove to be very useful but will also help you enhance your skills. The rsync utility is one of my favorites; it’s very useful for copying data from one place to another, as well as being helpful for setting up a backup job. Let’s use the following example rsync command to practice automation:

rsync -avb --delete --backup-dir=/backup/incremental/08-17-2022 /src /target

This example rsync command uses the -a (archive) option, which retains the metadata of the file(s) it copies to the target, such as the timestamp and owner. The -v option gives us verbose output, so we can see exactly what rsync is doing. The -b option enables backup mode, which means that if a file on the target will be overwritten by a file from the source, the previous version of that file will be renamed so it won’t be overwritten. Combining these three...

Summary

In this chapter, we dived into a number of more advanced concepts relating to shell commands, such as redirection, Bash history, command aliases, some command-line tricks, and more. Working with the shell is definitely something you’ll continue to improve upon, so don’t be worried if you have any trouble committing all of this knowledge to memory. After over 20 years of working with Linux, I’m still learning new things myself. The main takeaway in this chapter is to serve as a starting point to broaden your command-line techniques and also serve as the basis for future exploration into the subject.

In the next chapter, we’ll take a look at managing processes, which will include job management, taming misbehaving processes, and more. See you there!

Relevant videos

Further reading

Join our community on Discord

Join our community’s Discord space for discussions with the author and other readers:

https://packt.link/LWaZ0

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Ubuntu Server - Fourth Edition
Published in: Sep 2022Publisher: PacktISBN-13: 9781803234243
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Jay LaCroix

Jeremy "Jay" LaCroix is a technologist and open-source enthusiast, specializing in Linux. He has a net field experience of 20 years across different firms as a Solutions Architect and holds a master's degree in Information Systems Technology Management from Capella University. In addition, Jay also has an active Linux-focused YouTube channel with over 250K followers and over 20M views, available at LearnLinuxTV, where he posts instructional tutorial videos and other Linux-related content. He has also written Linux Mint Essentials and Mastering Linux Network Administration, published by Packt Publishing.
Read more about Jay LaCroix