Home Networking-and-servers Linux Utilities Cookbook

Linux Utilities Cookbook

By James Kent Lewis
books-svg-icon Book
Subscription
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
Subscription
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Using the Terminal / Command Line
About this book

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.

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.

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.


Based on the author's extensive experience, there is a section on best practices that every Linux user should be familiar with.

Publication date:
October 2013
Publisher
Packt
Pages
224
ISBN
9781782163008

 

Chapter 1. Using the Terminal / Command Line

In this chapter we will cover:

  • Command retrieval and line editing

  • Using history

  • Filename auto-completion

  • The shell prompt

  • Other environment variables

  • Using aliases

  • The .bashrc file

  • Dealing with blanks and special characters in filenames

  • Understanding the $? variable

  • Redirection and piping

  • Sending output from one terminal to another

  • Using the Screen program

 

Introduction


Knowing how to use the command line efficiently will really help you get the most out of your computer. There are many ways to save time and effort when typing commands, you just need to know what they are.

There are many different Linux environments available. This chapter focuses on the popular Bash shell.

 

Command retrieval and line editing


A standard Bash terminal is automatically set to insert mode, so you don't have to press the Insert key to insert text. Use the up and down arrow keys to recall a previous command, and then other cursor keys to edit that line as needed.

Getting ready

All you need for this example is a terminal running the Bash shell. Other terminals may not have these capabilities.

How to do it...

We will run a few commands as follows:

  1. Type in the command route and press the Enter key.

  2. Do the same for uptime, ls, date, and sync, pressing Enter after each command.

  3. Now press the up arrow key one time. You should see the following command:

    sync
    
  4. Now press the up arrow two more times. You should see date and ls.

  5. Press Enter. The ls command will run again. Pressing Enter will always run the command shown.

How it works...

The line is stored in a buffer with full editing capabilities. This buffer is sent to the OS when the Enter key is pressed.

The summary of the keys used for retrieval and editing is as follows:

  • Up arrow: It is used to scroll up the history buffer

  • Down arrow: It is used to scroll down the history buffer

  • Home: It is used to bring the cursor to the beginning of the line

  • End: It is used to bring the cursor to the end of the line

  • Delete: It is used to delete the character to the right of the cursor

  • Backspace: It is used to delete the character to the left of the cursor and shift the line

  • Left and right arrow: These are the cursor movement keys

 

Using history


The standard Bash shell includes a history function. It records each command in a database that can be shown by running the history command. In this section we have shown how this is done.

Getting ready

All you need is a Bash terminal to follow the given steps.

See the following screenshot:

How to do it...

  1. Run a few commands such as route, uptime, date, and sync.

  2. Run the history command.

  3. Look for a command you would like to run again, but instead of typing the command, type an exclamation point (!) and then the number next to the command as shown in the history listing, and press Enter.

  4. That command will run again.

How it works...

Think of the command line history as a linear database. You can scroll up and down until you see the command you want. This is also helpful to recall something you did a while back. The HISTSIZE environment variable controls how many commands will be saved in the buffer.

Tip

Be careful with this feature. Make sure you have the correct command before running it.

 

Filename auto-completion


When running a command, you do not have to type the entire filename. This saves a lot of time and effort, and also helps prevent typos.

The Tab key is used to invoke filename auto-completion. See the following screenshot:

You only need to type enough characters to make the filename you want unique, and then press Tab. If you didn't type enough characters, you will hear the console beep (in most shells). If you now press Tab again, all of the possibilities will be displayed.

Getting ready

All you need for this example is a terminal running the Bash shell.

How to do it...

  1. Change to your home directory, in my case it's:

    cd /home/jklewis.
    
  2. Create a directory using the following command:

    mkdir Linuxbook
    
  3. Change to it Linuxbook using the following command:

    cd Linuxbook
    ls > file2.txt
    ls > file3.txt
    ls > file4.txt
    ls > unique1.txt
    
  4. Now let's create some dummy files; run using the following command:

    ls > file1.txt
    
  5. Now type ls -la u and then press Tab. The rest of the filename "unique1.txt" will appear. Press Enter.

  6. Now type ls -al file and press Tab. Nothing will happen, and your console may beep. Press Tab again. Now all 4 filenames will appear.

  7. Add a 4, press Tab again, and then Enter. The command ls -la file4 will run.

This may look complicated or even clumsy but if you give it a chance you will become an expert in no time at all.

 

The shell prompt


A standard terminal usually has a rather cryptic command line prompt. This should be changed by modifying the PS1 environment variable.

How to do it...

An example is shown in the following screenshot:

Refer to the line export PS1="\u \h \w \$ "

  1. The \u command means to show the current user of this shell.

  2. The \h command shows the hostname of this machine.

  3. The \w command means to show the full path of the current directory. This change is highly recommended, as the user doesn't have to type pwd (Print Working Directory) all the time to know what directory is being used.

  4. The \$ means to display a $ or # depending on the effective UID.

There's more...

There are many more options, such as showing the time and date, using colors, and so on. For more information, run man bash and search for PS1.

 

Other environment variables


The PS1 variable is only one of literally hundreds of environment variables. Don't worry, you don't have to know them all! The following are a few very useful ones:

  • PS1: It shows and sets the command line prompt

  • USER: It shows the current user

  • HOSTNAME: It shows the current hostname for this machine

  • HOME: It shows the home directory of the current user

  • SHELL: It shows the current shell this terminal is running in

  • TERM: It shows which terminal type is being used

  • PATH: It shows and sets the directories where programs are searched for

  • PWD: It shows the current working directory

  • EDITOR: It can be set to the full path to your desired text editor for use with certain commands such as crontab -e

  • TZ: It shows and sets the time zone variable

  • HISTSIZE: It shows and sets the size of the history buffer

Most of these are self-explanatory; however, a few need more discussion. The PATH environment variable is where commands are searched for in the filesystem.

The echo command is used to display the contents of a variable:

How to do it...

  1. Prepending a dot to the PATH means the program will be looked for in the current directory first, before searching the rest of the path. This is very useful during the code development for example. Do this by running:

    export PATH=".:$PATH"
    
  2. The EDITOR variable can be set to your favorite text editor. Most people use vi (or vim); however, you can point it to the one you want. If you change this, be sure to use the full path. To change the EDITOR variable do this:

    export EDITOR=/lewis/bin64/kw
    
  3. An export can be removed by setting it to nothing:

    export EDITOR=
    
  4. By convention, environment variables are usually written in uppercase. View the man pages and/or search Google for more information on these variables.

How it works...

Think of these environment variables just as you would if you were using a programming language. In this case, the type of the variable is determined by the OS. For example, you could type A=1 or A="This is a string".

The OS knows the difference. Also, there is variable scope. Notice I did not use export above. That means this A is local to this shell. Only exporting a variable will make it available to other shells (after sourcing the file).

 

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.

 

The .bashrc file


There are many environment variables we can look at and change. However, we certainly don't want to enter these every time we start a new shell. There is a special file, named .bashrc, which is used to store your settings. It is located in the user's home directory. For example, the .bashrc file for the root user is in the /root directory.

Here is a .bashrc file from one of my systems:

How to do it...

The description of the lines is as follows:

  1. To comment a line, precede it with a # symbol.

  2. To create a variable, use the export tag.

  3. To create an alias, use the alias tag (as shown earlier in this chapter).

  4. Control statements are allowed; see the if clause in the previous screenshot.

  5. After modifying your .bashrc file, remember to source it using the dot operator as follows:

    . .bashrc
    
 

Dealing with blanks and special characters in filenames


Linux (and Unix) filesystems were not originally designed to handle blanks in filenames. This can cause quite a few problems, as the shell treats each item after a blank as another file or parameter. A solution is to use quotes, the backslash, or the Tab key.

The following sections assume the user has not modified the Bash Internal Field Separator (IFS) variable.

How to do it...

See the following screenshot. I purposely created three "bad" filenames:

  1. Run ls -la file with blanks.txt and notice the errors.

  2. Now run it again, but enclose the filename in quotes: ls -la "file with blanks.txt"; it will work properly now.

  3. Enter ls -la file and press Tab. It will escape the blanks for you.

  4. Run ls -la special>.txt. Observe the error.

  5. Enclose in quotes as before using the following command:

    ls -la "special>.txt"
    
  6. Now try ls -la -startswithdash.txt and then try quoting it. Doesn't work, right?

  7. Precede the filename with the ./ operator using the following command:

    ls -la ./-starWtswithdash.txt
    

As you can see, this can also be a problem if special characters have been used in the filename. Study this one a bit and it will become clear. Remember the Tab key; it works really well for just about every case. If the file starts with a dash, use the ./ operator. It means to refer to the file in the current directory.

There's more...

The issue of blanks and special characters is even more of a problem in scripts. This will be covered in more detail in Chapter 8, Working with Scripts.

 

Understanding the $? variable


Typically, when a command is run in Linux it performs a task; it either reports what it did or indicates an error occurred. An internal return code is also generated, and is displayed by running the echo $? command. Note that this must be the very next thing typed after the original command.

The following screenshot shows echo $?:

How to do it...

Here is a quick example of echo $?:

  1. Run the following command:

    ping -c 1 packt.com
    
  2. It should succeed. Run the following command:

    echo $?
    
  3. You should get a 0 for success.

  4. Now run the following command:

    ping -c 1 phooey
    

    Tip

    It may take a second or two to complete.

  5. Run echo $? again. It should return a non-zero value to indicate failure.

How it works...

In general, a return of zero means success. A non-zero return means an error has occurred, and in many cases the code returned indicates what the error was. Remember this the next time you type a command, hit Enter, and get the shell prompt back without anything appearing to happen.

There's more...

The man and info pages for a command typically contain an entry showing what the errors mean. If the man page is lacking, consult the web.

 

Redirection and piping


Suppose you run a command, say route, and want to save the output in a file. The redirection (>) operator is used to do this instead of sending the output to the screen.

How to do it...

Let's try some redirection:

  1. Enter ifconfig > file1.txt. You won't see anything, because the output went into the file.

  2. Run cat file1.txt. You should now see the output.

  3. This works the other direction as well, to read from a file run the following command:

    sort < file1.txt
    
  4. You can even do both in one step:

    sort < file1.txt > output-file.txt
    
  5. You can also send the output to another command using the pipe operator. For example, run route | grep eth0. The above command would display only the lines from route that contain the phrase eth0.

There's more...

Here is something that I use all the time. Say I have written a program in C a long time ago, have several versions, and want to find the latest one. I could run locate to find them all:

locate crc.c

This might return quite a few lines. How can I run ls on each file to find the latest one? By piping the output into the xargs command and then ls:

locate crc.c | xargs ls -la

This will now show the time and date of each file.

This might seem a bit complicated at first, but if you experiment a little it will become second nature to you.

 

Sending output from one terminal to another


This is a really handy feature that is unique to Linux/UNIX systems. It's most useful in scripts but can be used on the command line as well. If you have a system available try the given steps.

Getting ready

You will need two open terminals.

How to do it...

We show how to send the output from one terminal to another in the following steps:

  1. In one terminal run the tty command. The output should be something like /dev/pts/16.

  2. In the other terminal run the route command. You will see the output in that terminal.

  3. Now run route again, but now using the command:

    route > /dev/pts/16
    
  4. The output will go to that other terminal.

How it works...

Terminals on Linux systems are devices that have their own buffer space. By referring to the device by name you can write to it.

There's more...

This feature is even more useful in scripts, which we will see in Chapter 8, Working with Scripts.

 

Using the Screen program


Screen is a full-screen window manager that shares a physical terminal with other processes (which are usually other terminals/shells). It is normally used when no other manager or desktop is available, such as on a server. It has a scroll-back history buffer and also allows for copy and paste of text between windows.

Getting ready

The following is a brief list of some of the many key bindings available with Screen:

  • Ctrl + A + ?: It displays a list of commands and their key bindings

  • Ctrl + A + C: It brings up a new window

  • Ctrl + A + D: It detaches a window

  • Ctrl + A + N: It is used to go to the next window in the sequence

  • Ctrl + A + P: It is used to go to the previous window in the sequence

  • Ctrl + A + # (where # is a number): It is used to go directly to that window

  • Ctrl + A + ": It shows the list of windows; user can select any one by the number

The following is a list of frequently used commands:

  • screen -list: It shows all of the windows

  • screen <program>: It creates a new window and run that program in it

How to do it...

An example of running the Screen utility is as follows:

  1. In a terminal run the screen -L command.

  2. Now press Ctrl + A and then press C. This will create another window.

  3. Do this two more times.

  4. Try typing Ctrl + A + 0.

  5. Try Ctrl + A + 3.

How it works...

In the previous section, step 1 will create a new window, window 0. If you are running inside a window manager you may notice the title change showing which window it is.

Step 2 will create another window. After step 3, you will have 4 windows in total.

When you perform the actions in step 4, you should be in window 0. Typing Ctrl + a + 3 will take you to window 3.

There's more...

Here is a helpful hint, if you are running only a command line with no desktop, you may want to change your PS1 variable to something like the following in your .bashrc file:

export PS1="screen$WINDOW \h \u \w \$ "

Now the prompt will always show which window you are in.

This describes only a small part of what Screen can do. Consult the man page for more information.

About the Author
  • James Kent Lewis

    James Kent Lewis has been in the computer industry for over 35 years. He started out writing BASIC programs in high school and used punch cards in college for his Pascal, Fortran, COBOL, and assembly language classes. He himself learned the C programming language by writing various utilities, including a fully-functional text editor, which he uses everyday. He started out using DOS and AIX, and then OS/2. Linux is now his operating system of choice. Jim has worked in the past for several companies, including IBM, Texas Instruments, Tandem, Raytheon, Hewlett-Packard, and others. Most of these positions dealt with low-level device drivers and operating system internals. In his spare time he likes to create video games in Java.He has written articles for IBM Developer Works and has one patent.

    Browse publications by this author
Latest Reviews (1 reviews total)
Good but a little more difficult to utilize.
Linux Utilities Cookbook
Unlock this book and the full library FREE for 7 days
Start now