Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
C++ System Programming Cookbook

You're reading from   C++ System Programming Cookbook Practical recipes for Linux system-level programming using the latest C++ features

Arrow left icon
Product type Paperback
Published in Feb 2020
Publisher Packt
ISBN-13 9781838646554
Length 292 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Onorato Vaticone Onorato Vaticone
Author Profile Icon Onorato Vaticone
Onorato Vaticone
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started with System Programming 2. Revisiting C++ FREE CHAPTER 3. Dealing with Processes and Threads 4. Deep Dive into Memory Management 5. Using Mutexes, Semaphores, and Condition Variables 6. Pipes, First-In First-Out (FIFO), Message Queues, and Shared Memory 7. Network Programming 8. Dealing with Console I/O and Files 9. Dealing with Time Interfaces 10. Managing Signals 11. Scheduling 12. Other Books You May Enjoy

Learning the Linux fundamentals - shell

A shell is a command interpreter that receives commands in an input, redirects them to GNU/Linux, and returns back the output. It is the most common interface between a user and GNU/Linux. There are different shell programs available. The most used ones are Bash shell (part of the GNU Project), tcsh shell, ksh shell, and zsh shell (this is basically an extended Bash shell).

Why would you need a shell? A user needs a shell if they need to interact with the operating system through the command line. In this recipe, we'll show some of the most common shell commands. Quite often, the terms shell and Terminal are used interchangeably, even though, strictly speaking, they are not exactly the same thing.

How to do it...

In this section, we will learn the basic commands to run on the shell—for example, to find a file, grep a text into a file, copy, and delete:

  1. Opening a shell: Depending on the GNU/Linux distribution, opening a new shell command has different shortcuts. On Ubuntu, press Ctrl + Alt + T, or press Alt + F2, then type gnome-terminal.
  2. Closing a shell: To close Terminal, just type exit and press Enter.
  3. The find command: This is used to search files in a directory hierarchy. In its simplest form, it appears like this:
find . -name file

It supports wildcards, too:

$ find /usr/local "python*"
  1. The grep command prints the lines by matching a pattern:
 $ grep "text" filename

grep also supports recursive search:

 $ grep "text" -R /usr/share
  1. Pipe commands: Commands running on the shell can be concatenated, to make the output of one command the input for another. The concatenation is done with the | (pipe) operator:
$ ls -l | grep filename
  1. Editing a file: The most two common tools to edit a file on Linux are vi and emacs (if you're not interested in editing the file, cat filename will print the file to the standard output). While the first is inherited by the Unix operating system, the latter is part of the GNU Project. This book will extensively use vi:
 $ vi filename

Next, we will look at shell commands related to file manipulation.

  1. This is the command to remove files:
$ rm filename
  1. This is the command to remove directories:
$ rm -r directoryName
  1. This is the command to clone a file:
$ cp file1 file2
  1. This is the command to clone a folder:
$ cp -r folder1 folder2  
  1. This is the command to clone a folder using a relative and absolute path:
$ cp -r /usr/local/folder1 relative/folder2

The next section will describe these commands.

How it works...

Let's have a look at the commands discussed in the How to do it... section, in detail:

  1. The first command searches (.) from the current folder and can contain absolute paths (for example, /usr/local) or relative paths (for example, tmp/binaries). For example, here, -name is the file to search.
  2. The second command searches from the /usr/local folder any file or folder that starts with python. The find command offers huge flexibility and a wide variety of options. For more information, refer to man page through the man find command.
  3. The grep command searches and prints any line that contains the word text in the filename file.
  4. The grep recursive search command searches and prints any line that contains the word text in any file recursively from the /usr/share folder.
  5. Pipe command (|): The output of the first command is shown in the following screenshot. A list of all the files and directories is passed as input to the second command (grep), which will be used to grep the filename:

Now, let's look at the commands that perform actions such as editing a file, and adding/removing files and directories.

Editing a file:

  • The vi command will open the filename in edit mode, assuming the current user has writing permissions on it (we will discuss permissions in more detail later).
    The following is a short summary of the most used commands in vi:
    • Shift + : (that is, the Shift key + colon) to switch in edit mode.
    • Shift + :i to insert.
    • Shift + :a to append.
    • Shift + :q! to quit the current session without saving.
    • Shift + :wq to save and quit the current session.
    • Shift + :set nu to show the line numbers on the file.
    • Shift + :23 (Enter) goes at line 23.
    • Press the (Esc) key to switch to command mode.
    • . to repeat the last command.
    • cw to change the word, or do this by pointing the cursor at the beginning of the word.
    • dd to remove the current line.
    • yy to copy the current line. If a number N is selected before the yy command, the N line will be copied.
    • p to paste the copied line with the yy command.
    • u to undo.

Adding and removing files and directories:

  1. The first command removes the file named filename.
  2. The second command removes directoryName and its content, recursively.
  3. The third command creates file2, which is an exact copy of file1.
  4. The fourth command creates folder2 as a clone of folder1:

There is a common pattern in the execution of the commands shown in this recipe. They are listed as follows:

  1. The user types a command and hits Enter.
  2. The command is interpreted by Linux.
  3. Linux interacts with its different parts (memory management, networking, filesystem, and more) to execute the command. This happens in kernel space.
  4. The results are returned to the user.

There's more...

This recipe showed some of the most recurrent commands. Mastering all the options, even just for the most common shell commands, is tricky, and that is why man pages were created. They contain a solid and clear reference for the Linux user.

See also

Chapter 8, Dealing with Console I/O and Files, will go deeper into console I/O and file management.

lock icon The rest of the chapter is locked
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.
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 €18.99/month. Cancel anytime