103.1 Lesson 1
Certificate: |
LPIC-1 |
---|---|
Version: |
5.0 |
Topic: |
103 GNU and Unix Commands |
Objective: |
103.1 Work on the command line |
Lesson: |
1 of 2 |
Introduction
Newcomers to the world of Linux administration and the Bash shell often feel a bit lost without the reassuring comforts of a GUI interface. They are used to having right-click access to the visual cues and contextual information that graphic file manager utilities make available. So it is important to quickly learn and master the relatively small set of command line tools through which you can instantly tap into all the data offered by your old GUI — and more.
Getting System Information
While staring at the flashing rectangle of a command line prompt, your first question will probably be “Where am I?” Or, more precisely, “Where in the Linux filesystem am I right now and if, say, I created a new file, where would it live?” What you are after here is your present work directory, and the pwd
command will tell you what you want to know:
$ pwd /home/frank
Assuming that Frank is currently logged in to the system and he is now in his home directory: /home/frank/
. Should Frank create an empty file using the touch
command without specifying any other location in the filesystem, the file will be created within /home/frank/
. Listing the directory contents using ls
will show us that new file:
$ touch newfile $ ls newfile
Besides your location in the filesystem, you will often want information about the Linux system you are running. This might include the exact release number of your distribution or the Linux kernel version that is currently loaded. The uname
tool is what you are after here. And, in particular, uname
using the -a
(“all”) option.
$ uname -a Linux base 4.18.0-18-generic #19~18.04.1-Ubuntu SMP Fri Apr 5 10:22:13 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
Here, uname
shows that Frank’s machine has the Linux kernel version 4.18.0 installed and is running Ubuntu 18.04 on a 64-bit (x86_64
) CPU.
Getting Command Information
You will often come across documentation talking about Linux commands with which you are not yet familiar. The command line itself offers all kinds of helpful information on what commands do and how to effectively use them. Perhaps the most useful information is found within the many files of the man
system.
As a rule, Linux developers write man
files and distribute them along with the utilities they create. man
files are highly structured documents whose contents are intuitively divided by standard section headings. Typing man
followed by the name of a command will give you information that includes the command name, a brief usage synopsis, a more detailed description, and some important historical and licensing background. Here is an example:
$ man uname UNAME(1) User Commands UNAME(1) NAME uname - print system information SYNOPSIS uname [OPTION]... DESCRIPTION Print certain system information. With no OPTION, same as -s. -a, --all print all information, in the following order, except omit -p and -i if unknown: -s, --kernel-name print the kernel name -n, --nodename print the network node hostname -r, --kernel-release print the kernel release -v, --kernel-version print the kernel version -m, --machine print the machine hardware name -p, --processor print the processor type (non-portable) -i, --hardware-platform print the hardware platform (non-portable) -o, --operating-system print the operating system --help display this help and exit --version output version information and exit AUTHOR Written by David MacKenzie. REPORTING BUGS GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Report uname translation bugs to <http://translationproject.org/team/> COPYRIGHT Copyright©2017 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. SEE ALSO arch(1), uname(2) Full documentation at: <http://www.gnu.org/software/coreutils/uname> or available locally via: info '(coreutils) uname invocation' GNU coreutils 8.28 January 2018 UNAME(1)
man
only works when you give it an exact command name. If, however, you are not sure about the name of the command you are after, you can use the apropos
command to search through the man
page names and descriptions. Assuming, for instance, that you cannot remember that it is uname
that will give you your current Linux kernel version, you can pass the word kernel
to apropros
. You will probably get many lines of output, but they should include these:
$ apropos kernel systemd-udevd-kernel.socket (8) - Device event managing daemon uname (2) - get name and information about current kernel urandom (4) - kernel random number source devices
If you do not need a command’s full documentation, you can quickly get basic data about a command using type
. This example uses type
to query four separate commands at once. The results show us that cp
(“copy”) is a program that lives in /bin/cp
and that kill
(change the state of a running process) is a shell builtin — meaning that it is actually a part of the Bash shell itself:
$ type uname cp kill which uname is hashed (/bin/uname) cp is /bin/cp kill is a shell builtin which is /usr/bin/which
Notice that, besides being a regular binary command like cp
, uname
is also “hashed.” That is because Frank recently used uname
and, to increase system efficiency, it was added to a hash table to make it more accessible the next time you run it. If he would run type uname
after a system boot, Frank would find that type
once again describes uname
as a regular binary.
Note
|
A quicker way to clean up the hash table is to run the command |
Sometimes — particularly when working with automated scripts — you will need a simpler source of information about a command. The which
command that our previous type
command traced for us, will return nothing but the absolute location of a command. This example locates both the uname
and which
commands.
$ which uname which /bin/uname /usr/bin/which
Note
|
If you want to display information about “builtin” commands, you can use the |
Using Your Command History
You will often carefully research the proper usage for a command and successfully run it along with a complicated trail of options and arguments. But what happens a few weeks later when you need to run the same command with the same options and arguments but cannot remember the details? Rather than having to start your research over again from scratch, you will often be able to recover the original command using history
.
Typing history
will return the most recent commands you have executed with the most recent of those appearing last. You can easily search through those commands by piping a specific string to the grep
command. This example will search for any command that included the text bash_history
:
$ history | grep bash_history 1605 sudo find /home -name ".bash_history" | xargs grep sudo
Here a single command is returned along with is sequence number, 1605.
And speaking of bash_history
, that is actually the name of a hidden file you should find within your user’s home directory. Since it is a hidden file (designated as such by the dot that precedes its filename), it will only be visible by listing the directory contents using ls
with the -a
argument:
$ ls /home/frank newfile $ ls -a /home/frank . .. .bash_history .bash_logout .bashrc .profile .ssh newfile
What is in the .bash_history
file? Take a look for yourself: you will see hundreds and hundreds of your most recent commands. You might, however to surprised to find that some of your most recent commands are missing. That is because, while they are instantly added to the dynamic history
database, the latest additions to your command history are not written to the .bash_history
file until you exit your session.
You can leverage the contents of history
to make your command line experience much faster and more efficient using the up and down arrow keys on your keyboard. Hitting the up key multiple times will populate the command line with recent commands. When you get to the one you would like to execute a second time, you can run it by pressing Enter. This makes it easy to recall and, if desired, modify commands multiple times during a shell session.
Guided Exercises
-
Use the
man
system to determine how to tellapropos
to output a brief command so that it outputs only a brief usage message and then exits. -
Use the
man
system to determine which copyright license is assigned to thegrep
command.
Explorational Exercises
-
Identify the hardware architecture and Linux kernel version being used on your computer in an easy-to-read output format.
-
Print out the last twenty lines of the dynamic
history
database and the.bash_history
file to compare them. -
Use the
apropos
tool to identify theman
page where you will find the command you will need to display the size of an attached physical block device in bytes rather than megabytes or gigabytes.
Summary
In this lesson, you learned:
-
How to get information about your filesystem location and OS software stack.
-
How to find help for command usage.
-
How to identify the filesystem location and type of command binaries.
-
How to find and reuse previously-executed commands.
The following commands were discussed in this lesson:
pwd
-
Print the path to the current working directory.
uname
-
Print your system’s hardware architecture, Linux kernel version, distribution, and distribution release.
man
-
Access help files documenting command usage.
type
-
Print the filesystem location and type for one or more commands.
which
-
Print the filesystem location for a command.
history
-
Print or reuse commands that you have previously executed.
Answers to Guided Exercises
-
Use the
man
system to determine how to tellapropos
to output a brief command so that it outputs only a brief usage message and then exits.Run
man apropos
and scroll down through the “Options” section until you get to the--usage
paragraph. -
Use the
man
system to determine which copyright license is assigned to thegrep
command.Run
man grep
and scroll down to the “Copyright” section of the document. Note that the program uses a copyright from the Free Software Foundation.
Answers to Explorational Exercises
-
Identify the hardware architecture and Linux kernel version being used on your computer in an easy-to-read output format.
Run
man uname
, read through the “Description” section, and identify the command arguments that will display only the exact results you want. Note how-v
will give you the kernel version and-i
will provide hardware platform.$ man uname $ uname -v $ uname -i
-
Print out the last twenty lines of the dynamic
history
database and the.bash_history
file to compare them.$ history 20 $ tail -n 20 .bash_history
-
Use the
apropos
tool to identify theman
page where you will find the command you will need to display the size of an attached physical block device in bytes rather than megabytes or gigabytes.One way, would be to run
apropos
with the stringblock
, read through the results, note thatlsblk
lists block devices (and would, therefore, be the most likely tool for our needs), runman lsblk
, scroll through the “Description” section and note that-b
will display a device size in bytes. Finally, runlsblk -b
to see what comes out.$ apropos block $ man lsblk $ lsblk -b