Book Image

Linux Utilities Cookbook

By : James Kent Lewis
Book Image

Linux Utilities Cookbook

By: James Kent Lewis

Overview of this book

<p>Linux is a stable, reliable and extremely powerful operating system. It has been around for many years, however, most people still don't know what it can do and the ways it is superior to other operating systems. Many people want to get started with Linux for greater control and security, but getting started can be time consuming and complicated. <br /><br />A practical, hands-on guide that provides you with a number of clear step-by-step examples to help you solve many of the questions that crop up when using an operating system you may not be familiar with.</p> <p>Presenting solutions to the most common Linux problems in a clear and concise way, this helpful guide starts with spicing up the terminal sessions by command retrieval and line editing, and shell prompt variables. We will then get to know the different desktops (GUIs) available for Linux systems and which is the best fit for you. We will then explore the world of managing files and directories, connectivity, and what to do when it goes wrong. We will also learn a range of skills, from creating and managing user accounts to securing your system, managing and limiting processes, and letting information flow from one process to another using pipes. Later, we will master disk management, working with scripts and automating tasks quickly, and finally, understand the need for a custom kernel and tips on how to build one.</p> <p><br />Based on the author's extensive experience, there is a section on best practices that every Linux user should be familiar with.</p>
Table of Contents (19 chapters)
Linux Utilities Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using aliases


Wouldn't it be nice if you could easily create a simple command without having to make a script out of it? Well, there is a way. This is done using aliases.

How to do it...

The following are the steps to create an alias:

  1. Type tput clear and press Enter. Your screen should have cleared.

  2. Now enter alias cls="tput clear". Now when you run cls it will do the same thing.

  3. Let's create some more. To show a long directory listing enter alias la="ls -la". Enter 'la' to run the alias.

  4. To show a long listing with the most current files last enter 'alias lt="ls -latr"'.

If you create an alias and then decide you no longer want it you can remove it by using the unalias command, for example, unalias cls.

You can also use aliases to move around the filesystem efficiently. This is very handy and will save you an incredible amount of typing. Here are some examples:

  1. mkdir /home/jklewis/linuxbook

  2. alias lbook="cd /home/jklewis/linuxbook"

  3. lbook

You will now be taken to that directory. Here is something I make frequent use of on my systems:

  1. export LBOOK="/home/jklewis/linuxbook"

  2. alias lbook="cd $LBOOK"

  3. lbook

As you can see, running lbook will take you to the directory as shown above. However, you can also use the LBOOK variable to copy files to that directory:

  1. cd /tmp

  2. touch f1.txt

  3. cp f1.txt $LBOOK

The file f1.txt will now exist in the /home/jklewis/linuxbook directory. This becomes even more handy when extremely long filenames are used.

Tip

To remove the lbook alias run unalias lbook

You can list your aliases by just running alias without any parameters. Any time you find yourself constantly typing the same commands or filenames consider creating an alias for it.

There's more...

Note that the above examples will only be effective in that terminal and will not persist across a reboot. See the next section on how to make the changes permanent.

Also, in some cases, what you want to do may be too complicated for an alias, for example, to check for the proper number of parameters. This is where you can create a shell script, which will be covered in Chapter 8, Working with Scripts.