Linux Professional Institute Learning Logo.
Skip to main content
  • Home
    • All Resources
    • LPI Learning Materials
    • Become a Contributor
    • Publishing Partners
    • Become a Publishing Partner
    • About
    • FAQ
    • Contributors
    • Roadmap
    • Contact
  • LPI.org
103.1 Lesson 1
Topic 101: System Architecture
101.1 Determine and configure hardware settings
  • 101.1 Lesson 1
101.2 Boot the system
  • 101.2 Lesson 1
101.3 Change runlevels / boot targets and shutdown or reboot system
  • 101.3 Lesson 1
Topic 102: Linux Installation and Package Management
102.1 Design hard disk layout
  • 102.1 Lesson 1
102.2 Install a boot manager
  • 102.2 Lesson 1
102.3 Manage shared libraries
  • 102.3 Lesson 1
102.4 Use Debian package management
  • 102.4 Lesson 1
102.5 Use RPM and YUM package management
  • 102.5 Lesson 1
102.6 Linux as a virtualization guest
  • 102.6 Lesson 1
Topic 103: GNU and Unix Commands
103.1 Work on the command line
  • 103.1 Lesson 1
  • 103.1 Lesson 2
103.2 Process text streams using filters
  • 103.2 Lesson 1
103.3 Perform basic file management
  • 103.3 Lesson 1
  • 103.3 Lesson 2
103.4 Use streams, pipes and redirects
  • 103.4 Lesson 1
  • 103.4 Lesson 2
103.5 Create, monitor and kill processes
  • 103.5 Lesson 1
  • 103.5 Lesson 2
103.6 Modify process execution priorities
  • 103.6 Lesson 1
103.7 Search text files using regular expressions
  • 103.7 Lesson 1
  • 103.7 Lesson 2
103.8 Basic file editing
  • 103.8 Lesson 1
Topic 104: Devices, Linux Filesystems, Filesystem Hierarchy Standard
104.1 Create partitions and filesystems
  • 104.1 Lesson 1
104.2 Maintain the integrity of filesystems
  • 104.2 Lesson 1
104.3 Control mounting and unmounting of filesystems
  • 104.3 Lesson 1
104.5 Manage file permissions and ownership
  • 104.5 Lesson 1
104.6 Create and change hard and symbolic links
  • 104.6 Lesson 1
104.7 Find system files and place files in the correct location
  • 104.7 Lesson 1
  1. Topic 103: GNU and Unix Commands
  2. 103.1 Work on the command line
  3. 103.1 Lesson 1

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 hash -d.

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 help command.

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

  1. Use the man system to determine how to tell apropos to output a brief command so that it outputs only a brief usage message and then exits.

  2. Use the man system to determine which copyright license is assigned to the grep command.

Explorational Exercises

  1. Identify the hardware architecture and Linux kernel version being used on your computer in an easy-to-read output format.

  2. Print out the last twenty lines of the dynamic history database and the .bash_history file to compare them.

  3. Use the apropos tool to identify the man 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

  1. Use the man system to determine how to tell apropos 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.

  2. Use the man system to determine which copyright license is assigned to the grep 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

  1. 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
  2. Print out the last twenty lines of the dynamic history database and the .bash_history file to compare them.

    $ history 20
    $ head -n 20 .bash_history
  3. Use the apropos tool to identify the man 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 string block, read through the results, note that lsblk lists block devices (and would, therefore, be the most likely tool for our needs), run man lsblk, scroll through the “Description” section and note that -b will display a device size in bytes. Finally, run lsblk -b to see what comes out.

    $ apropos block
    $ man lsblk
    $ lsblk -b

© 2020 Linux Professional Insitute Inc. All rights reserved. Visit the Learning Materials website: https://learning.lpi.org
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

Next Lesson

103.1 Work on the command line (103.1 Lesson 2)

Read next lesson

© 2020 Linux Professional Insitute Inc. All rights reserved. Visit the Learning Materials website: https://learning.lpi.org
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

LPI is a non-profit organization.

Linux Professional Institute (LPI) is the global certification standard and career support organization for open source professionals. With more than 200,000 certification holders, it's the world’s first and largest vendor-neutral Linux and open source certification body. LPI has certified professionals in over 180 countries, delivers exams in multiple languages, and has hundreds of training partners.

Our purpose is to enable economic and creative opportunities for everybody by making open source knowledge and skills certification universally accessible.

  • LinkedIn
  • flogo-RGB-HEX-Blk-58 Facebook
  • Twitter
  • Contact Us
  • Privacy and Cookie Policy

Spot a mistake or want to help improve this page? Please let us know.

© Copyright 1999-2020 The Linux Professional Institute Inc. All rights reserved.