knowt logo

LFS101x: Chapter 7 - Command Line Operations

Logging in

Once your login session is started (either by logging into a text terminal or via a graphical terminal program), you can also connect and log into remote systems by using SSH (Secure SHell)

  • ex: ssh student@remote-server. com, SSH would connect securely to the remote machine (remote-server. com) and give student a command line terminal winder, using either a password (as regular logins) or a cryptographic key to sign in without providing a password to verify the identity

Rebooting and Shutting Down

Both rebooting and shutting down from the command line require superuser (root) access

  • shutdown - Issues the device to shutdown

    • shutdown -h - Halts the system

    • shutdown -r - Reboots the device instead of just shutting it down

  • halt - Issues the device to shutdown

  • poweroff - Issues the device to shutdown

Locating Applications

  • which - Locates a program

  • whereis - Locates a program in a broader range of system directories, as well as locating source and man files packaged with the program

Accessing Directories

  • pwd - Displays the present working directory

  • cd ~ (or cd) - Change to your home directory

  • cd . . - Change to the parent directory

  • cd - - Change to the previous directory

Exploring the filesystem

  • tree - Displays a tree view of the filesystem

    • tree -d - To view just the tree’s directories and to suppress listing file names

  • cd / - Changes your current directory to the root (/) directory (or path you supply)

  • ls - Lists the contents of the present working directory

    • ls -a - List all files, including hidden files and directories (those whose names start with .)

Navigating the Directory History

  • cd - - Brings you back to the last directory that you were previously in

Viewing Files

  • cat - Used for viewing files that are not very long

    • It doesn’t provide any scroll-back

  • tac - Used to look at a file backward, starting with the last line

  • less - Used to view larger files because it’s a paging program

    • It pauses at each screen full of text, provides scroll-back capabilities, and lets you search and navigate within the file

  • tail - Used to print the last 10 lines of a file by default

    • You can change the number of lines by doing, for example, -n 15 or just -15 if you wanted to look at the last 15 lines instead of the default

  • head - Used to print the first 10 lines of a file by default

    • You can change the number of lines by doing, for example, -n 15 or just -15 if you wanted to look at the last 15 lines instead of the default

  • touch - Often used to set or update the access, change, and modify times of files (By default, it resets a file's timestamp to match the current time)

    • You can also create an empty file using touch: $ touch <filename>

      • This is normally done to create an empty file as a placeholder for a later purpose

    • touch provides several useful options (ex: -t allows you to set the date and timestamp of the file to a specific value: $ touch -t 12091600 myfile, which sets the myfile file's timestamp to 4pm December 9th)

  • mkdir - Used to create a directory

    • mkdir smpdir - Creates a sample directory named sampdir under the current directory

    • mkdir /usr/sampdir - Creates a sample directory called sampdir under /usr

  • rmdir - Removes a directory (The directory must be empty or the command will fail)

  • rm - Remove a file

    • rm -rf - Forcefully remove a directory and all of its content recursively

      • It is extremely dangerous and should be used with the utmost care, especially when used by root (recall that recursive means drilling down through all sub-directories, all the way down a tree)

    • rm -i - Interactively remove a file

  • mv - It can simply rename a file or it can also move a file to another location, while possibly changing its name at the same time

Searching for Files

locate

  • locate - This program performs a search taking advantage of a previously constructed database of files and directories on your system, matching all entries that contain a specified character string (This can sometimes result in a very long list)

Options

  • locate | grep - Used to search and display a shorter and possibly more relevant list of matches found by using the grep program as a filter

  • This program uses a database created by updatedb

    • Most Linux systems run this automatically once a day

    • However, you can update it at any time by just running updatedb from the command line as the root user

find

  • find - This utility recurses down the filesystem tree from any particular directory (or set of directories) and locates files that match specified conditions (The default pathname is always the present working directory)

    • When no arguments are given, find lists all files in the current directory and all of its subdirectories

  • Options

    • -name - Only lists files with a certain pattern in their name (ex: $ find /usr -name gcc)

    • -iname - Ignores the case of filenames (ex: $ find /usr -iname gcc)

    • -type - Will restrict the results to files of a certain specified type (ex: d for directory, l for symbolic link, f for a regular file, etc.)(ex: $ find /usr type -d -name gcc)

    • -exec - Used to run commands on the files that match your search criteria

      • { } is a placeholder that will be filled with all the file names that result from the find expressions

      • You also have to end the command with either ';' (including the single quotes) or ";"

    • -ok - Behaves the same as -exec, except that find will prompt you for permission before executing the command

    • -ctime - When the inode metadata (i.e. file ownership, permissions, etc.) last changed;

      • it is often, but not necessarily, when the file was first created.

      • ex: $ find / -ctime 3

    • -atime - Searches for accessed/last read

      • ex: $ find / -atime 3

    • -mtime - Searches for modified/last written

      • ex: $ find / -mtime 3

      • The number is the number of days and can be expressed as either

        • a number (n) that means exactly that value

        • n+, which means greater than that number

        • -n, which means less than that number

        • There are similar options for times in minutes (-cmin, -amin, -mmin)

    • -size - Used to find files based on size

      • You can specify bytes (c), kilobytes (k), megabytes (M), gigabytes (G), etc.

      • As with the time numbers above, file sizes can also be exact numbers (n), +n or -n

Installing Software

Package Management Systems on Linux

the core parts of a Linux distribution and most of its add-on software are installed via the Package Management System

  • Each package contains the files and other instructions needed to make one software componenet work well and cooperate with the other components that comprise the entire system

  • Packages can depend on each other (ex: A package for a web-based application written in PHP can depend on the PHP package)

  • There are two broad families of package managers

    • Those based on Debian

    • Those which use RPM as their low-level package manager

      • The two systems are incompatible, but provide the same features and satisfy the same needs (There are also some other systems that are used by more specialized Linux distributions)

Package Managers: Two Levels

Both Debian based and RPM package management systems operate on two distinct levels:

  • A low-level tool (such as dpkg or rpm) takes care of the details of unpacking individual packages, running scripts and getting the software installed correctly

  • A high-level tool (such as apt-get, dnf, yum, zypper, etc.) works with groups of packages, downloads packages from the vendor and figures out dependencies

    • Most of the time users need to work only with the high-level tool, which will take care of calling the low-level tool as needed

    • Dependency resolution is a particularly important feature of the high-level tool, as it handles the details of finding and installing each dependency for you

      • Be careful because installing a single package could result in many dozens or even hundreds of dependent packages being installed

Working With Different Package Management Systems

Advanced Packaging Tool (apt)

The apt (Advance Packaging Tool) is the underlying package management system that manages software on Debian-based systems

  • While it forms the backend for graphical package managers (such as the Ubuntu Software Center and synaptic) its native UI is at the command line, with programs that include apt (or apt-get) and apt-cache

Dandified YUM (dnf)

dnf is the open source command-line package-management utility for the RPM-compatible Linux systems that b elongs to the Red Hat family

  • dnf has both command line and graphical UI

  • Fedora and RHEL 8 replaced the older yum utility with dnf (Thereby eliminating a lot of historical baggage, as well as introducing many nice new capabilities) (dnf is backwards-compatible with yum for day-to-day commands)

zypper

zypper is the package management system for the SUSE/openSUSE family and is also based on RPM.

  • zypper also allows you to manage repositories from the command line.

  • zypper is fairly straightforward to use and resembles dnf/yum quite closely.

Absolute and Relative Paths

There are two ways to identify paths:

Absolute pathname

This path begins with the root directory and follows the tree, branch by branch until it reaches the desired directory or file (Absolute paths always start with /)

  • ex: cd /usr/bin

Relative pathname

This path begins from the present working directory (Relative paths never start with /)

  • This pathname is usually the most convenient to use since it requires less typing

    • . - Present directory

    • . . - Parent directory

    • ~ - Your home directory

  • ex: . . / . . /usr/bin

Hard and Soft (Symbolic) Links

Hard Link

A hard link is a file all its own, and the file references or points to the exact spot on a hard drive where the Inode stores the data

  • ln - Used to create hard links

    • Hard links are handy and they save space, but you have to be very careful with their use

Soft Link

A soft link isn't a separate file, it points to the name of the original file, rather than to a spot on the hard drive

  • ln -s - Used to create soft (symbolic) links

    • Symbolic links take no extra space on the filesystem (unless their names are very long)

    • They’re highly convenient, as they can be easily modified to point to different places

      • An easy way to create a shortcut from your home directory to long pathnames is to create a symbolic link

    • Unlike hard links, soft links can point to objects even on different filesystems, partitions, and/or disks and other media, which may or may not be currently available or even exist

      • In the case where the link doesn’t point to a currently available or existing object, you obtain a dangling link

PS1

The PS1 variable is the character string that's displayed as the prompt on the command line

  • Most distributions set PS1 to a known default value, which is suitable in most cases

    • However, users may want custom information to show on the command line

      • Ex: Some systems administrators require the user and the host system to show up on the command line as in student@c8 $, which could be implemented by setting the PS1 variable to \u@\h $

        $ echo $PS1

        $

        $ PS1="\u@\h $ "

        student@c8 $ echo $PS1

        \u@\h $

        student@c8 $

  • This could be useful if you're working in multiple roles and want to be always reminded of who you are and what machine you are on

    • By convention, most systems are set up so that the root user has a pound sign (#) as their prompt

Standard File Streams

When commands are executed, by default there are three standard streams (or descriptors) always open for use

  • Standard input (standard in or stdin, value: 0, ex: keyboard)

  • Standard output (standard out or stdout, value: 1, ex: terminal)

  • Standard error (stderr, value: 2, ex: log file)

Usually, stdin is your keyboard, and stdout and stderr are printed on your terminal

  • stdin is supplied by directing input to come from a file or from the output of a previous command through a pipe

  • stdout is often redirected into a file

  • stderr is often redirected to an error logging file

In Linux, all open files are represented internally by file descriptors (They're represented by numbers starting at zero)

  • stdin - 0

  • stdout - 1

  • stderr - 2

  • If other files are opened in addition to these three mentioned above, which are opened by default, they'll start at file descriptor 3 and increase from there

I/O Redirection

Through the command shell, we can redirect the three standard file streams so that we can get input from either a file or another command, instread of from our keyboard, and we can write output and errors to files and use them to provide input for subsequent commands

  • For example:

    • If we have a program called do_something that reads from stdin and writes to stdout and stderr, we can change its input source by using the less-than sign (<) followed by the name of the file to be consumed for input data: $ do_something < input-file

    • If you want to send the output to a file, use the greater-than sign (>) as in: $ do_something > output-file

  • Because stderr isn't the same as stdout, error messages will still be seen on the terminal window in the example above

  • If you want to redirect stderr to a seperate file, you use stderr's file descriptor number (2), the greater-than sign (>), followed by the name of the file you want to hold everything the running command writes to stderr: $ do_something 2> error-file

  • A special shorthand notation can send anything written to file descriptor 2 (stderr) to the same place as file descriptor 1 (stdout): 2>&1; $ do_something > all-output-file 2>&1, bash permits an easier syntax: $ do_something >& all output-file

Pipes

The UNIX/Linux philosophy is to have many simple and short programs (or commands) cooperate together to produce quite complex results, rather than have one complex program with many possible options and modes of operation

  • In order to do this, we use the vertical-bar, pipe symbol (|), between commands, which we call a pipeline (ex: $ command1 | command2 | command3)

    • A pipeline allows Linux to combine the actions of several commands into one

      • This is extraordinarily efficient because command2 and command3 don't have to wait for the previous pipeline commands to complete before they can begin hacking at the data in their previous input streams; on multiple CPU or core systems, the available computing power is much better utilized and things get done quicker

      • There's also no need to save output in (temporary) files between the stages in the pipeline, which saves disk space and reduces reading and writing from disk, which is often the slowest bottleneck in getting something done

Wildcards and Matching File Names

You can search for a filename containing specific characters using wildcards

  • ? - Matches any single character

    • To search for files using the**?** wildcard, replace each unknown character with ?

    • ex: ls ba?.out

  • ***** - Matches any string of characters

    • To search for files using the * wildcard, replace the unknown string with *

    • ex: ls *.out

  • [set] - Matches any character in the set of characters

    • ex: [adf] will match any occurrence of a, d, or f

  • [!set] - Matches any character not in the set of character

M

LFS101x: Chapter 7 - Command Line Operations

Logging in

Once your login session is started (either by logging into a text terminal or via a graphical terminal program), you can also connect and log into remote systems by using SSH (Secure SHell)

  • ex: ssh student@remote-server. com, SSH would connect securely to the remote machine (remote-server. com) and give student a command line terminal winder, using either a password (as regular logins) or a cryptographic key to sign in without providing a password to verify the identity

Rebooting and Shutting Down

Both rebooting and shutting down from the command line require superuser (root) access

  • shutdown - Issues the device to shutdown

    • shutdown -h - Halts the system

    • shutdown -r - Reboots the device instead of just shutting it down

  • halt - Issues the device to shutdown

  • poweroff - Issues the device to shutdown

Locating Applications

  • which - Locates a program

  • whereis - Locates a program in a broader range of system directories, as well as locating source and man files packaged with the program

Accessing Directories

  • pwd - Displays the present working directory

  • cd ~ (or cd) - Change to your home directory

  • cd . . - Change to the parent directory

  • cd - - Change to the previous directory

Exploring the filesystem

  • tree - Displays a tree view of the filesystem

    • tree -d - To view just the tree’s directories and to suppress listing file names

  • cd / - Changes your current directory to the root (/) directory (or path you supply)

  • ls - Lists the contents of the present working directory

    • ls -a - List all files, including hidden files and directories (those whose names start with .)

Navigating the Directory History

  • cd - - Brings you back to the last directory that you were previously in

Viewing Files

  • cat - Used for viewing files that are not very long

    • It doesn’t provide any scroll-back

  • tac - Used to look at a file backward, starting with the last line

  • less - Used to view larger files because it’s a paging program

    • It pauses at each screen full of text, provides scroll-back capabilities, and lets you search and navigate within the file

  • tail - Used to print the last 10 lines of a file by default

    • You can change the number of lines by doing, for example, -n 15 or just -15 if you wanted to look at the last 15 lines instead of the default

  • head - Used to print the first 10 lines of a file by default

    • You can change the number of lines by doing, for example, -n 15 or just -15 if you wanted to look at the last 15 lines instead of the default

  • touch - Often used to set or update the access, change, and modify times of files (By default, it resets a file's timestamp to match the current time)

    • You can also create an empty file using touch: $ touch <filename>

      • This is normally done to create an empty file as a placeholder for a later purpose

    • touch provides several useful options (ex: -t allows you to set the date and timestamp of the file to a specific value: $ touch -t 12091600 myfile, which sets the myfile file's timestamp to 4pm December 9th)

  • mkdir - Used to create a directory

    • mkdir smpdir - Creates a sample directory named sampdir under the current directory

    • mkdir /usr/sampdir - Creates a sample directory called sampdir under /usr

  • rmdir - Removes a directory (The directory must be empty or the command will fail)

  • rm - Remove a file

    • rm -rf - Forcefully remove a directory and all of its content recursively

      • It is extremely dangerous and should be used with the utmost care, especially when used by root (recall that recursive means drilling down through all sub-directories, all the way down a tree)

    • rm -i - Interactively remove a file

  • mv - It can simply rename a file or it can also move a file to another location, while possibly changing its name at the same time

Searching for Files

locate

  • locate - This program performs a search taking advantage of a previously constructed database of files and directories on your system, matching all entries that contain a specified character string (This can sometimes result in a very long list)

Options

  • locate | grep - Used to search and display a shorter and possibly more relevant list of matches found by using the grep program as a filter

  • This program uses a database created by updatedb

    • Most Linux systems run this automatically once a day

    • However, you can update it at any time by just running updatedb from the command line as the root user

find

  • find - This utility recurses down the filesystem tree from any particular directory (or set of directories) and locates files that match specified conditions (The default pathname is always the present working directory)

    • When no arguments are given, find lists all files in the current directory and all of its subdirectories

  • Options

    • -name - Only lists files with a certain pattern in their name (ex: $ find /usr -name gcc)

    • -iname - Ignores the case of filenames (ex: $ find /usr -iname gcc)

    • -type - Will restrict the results to files of a certain specified type (ex: d for directory, l for symbolic link, f for a regular file, etc.)(ex: $ find /usr type -d -name gcc)

    • -exec - Used to run commands on the files that match your search criteria

      • { } is a placeholder that will be filled with all the file names that result from the find expressions

      • You also have to end the command with either ';' (including the single quotes) or ";"

    • -ok - Behaves the same as -exec, except that find will prompt you for permission before executing the command

    • -ctime - When the inode metadata (i.e. file ownership, permissions, etc.) last changed;

      • it is often, but not necessarily, when the file was first created.

      • ex: $ find / -ctime 3

    • -atime - Searches for accessed/last read

      • ex: $ find / -atime 3

    • -mtime - Searches for modified/last written

      • ex: $ find / -mtime 3

      • The number is the number of days and can be expressed as either

        • a number (n) that means exactly that value

        • n+, which means greater than that number

        • -n, which means less than that number

        • There are similar options for times in minutes (-cmin, -amin, -mmin)

    • -size - Used to find files based on size

      • You can specify bytes (c), kilobytes (k), megabytes (M), gigabytes (G), etc.

      • As with the time numbers above, file sizes can also be exact numbers (n), +n or -n

Installing Software

Package Management Systems on Linux

the core parts of a Linux distribution and most of its add-on software are installed via the Package Management System

  • Each package contains the files and other instructions needed to make one software componenet work well and cooperate with the other components that comprise the entire system

  • Packages can depend on each other (ex: A package for a web-based application written in PHP can depend on the PHP package)

  • There are two broad families of package managers

    • Those based on Debian

    • Those which use RPM as their low-level package manager

      • The two systems are incompatible, but provide the same features and satisfy the same needs (There are also some other systems that are used by more specialized Linux distributions)

Package Managers: Two Levels

Both Debian based and RPM package management systems operate on two distinct levels:

  • A low-level tool (such as dpkg or rpm) takes care of the details of unpacking individual packages, running scripts and getting the software installed correctly

  • A high-level tool (such as apt-get, dnf, yum, zypper, etc.) works with groups of packages, downloads packages from the vendor and figures out dependencies

    • Most of the time users need to work only with the high-level tool, which will take care of calling the low-level tool as needed

    • Dependency resolution is a particularly important feature of the high-level tool, as it handles the details of finding and installing each dependency for you

      • Be careful because installing a single package could result in many dozens or even hundreds of dependent packages being installed

Working With Different Package Management Systems

Advanced Packaging Tool (apt)

The apt (Advance Packaging Tool) is the underlying package management system that manages software on Debian-based systems

  • While it forms the backend for graphical package managers (such as the Ubuntu Software Center and synaptic) its native UI is at the command line, with programs that include apt (or apt-get) and apt-cache

Dandified YUM (dnf)

dnf is the open source command-line package-management utility for the RPM-compatible Linux systems that b elongs to the Red Hat family

  • dnf has both command line and graphical UI

  • Fedora and RHEL 8 replaced the older yum utility with dnf (Thereby eliminating a lot of historical baggage, as well as introducing many nice new capabilities) (dnf is backwards-compatible with yum for day-to-day commands)

zypper

zypper is the package management system for the SUSE/openSUSE family and is also based on RPM.

  • zypper also allows you to manage repositories from the command line.

  • zypper is fairly straightforward to use and resembles dnf/yum quite closely.

Absolute and Relative Paths

There are two ways to identify paths:

Absolute pathname

This path begins with the root directory and follows the tree, branch by branch until it reaches the desired directory or file (Absolute paths always start with /)

  • ex: cd /usr/bin

Relative pathname

This path begins from the present working directory (Relative paths never start with /)

  • This pathname is usually the most convenient to use since it requires less typing

    • . - Present directory

    • . . - Parent directory

    • ~ - Your home directory

  • ex: . . / . . /usr/bin

Hard and Soft (Symbolic) Links

Hard Link

A hard link is a file all its own, and the file references or points to the exact spot on a hard drive where the Inode stores the data

  • ln - Used to create hard links

    • Hard links are handy and they save space, but you have to be very careful with their use

Soft Link

A soft link isn't a separate file, it points to the name of the original file, rather than to a spot on the hard drive

  • ln -s - Used to create soft (symbolic) links

    • Symbolic links take no extra space on the filesystem (unless their names are very long)

    • They’re highly convenient, as they can be easily modified to point to different places

      • An easy way to create a shortcut from your home directory to long pathnames is to create a symbolic link

    • Unlike hard links, soft links can point to objects even on different filesystems, partitions, and/or disks and other media, which may or may not be currently available or even exist

      • In the case where the link doesn’t point to a currently available or existing object, you obtain a dangling link

PS1

The PS1 variable is the character string that's displayed as the prompt on the command line

  • Most distributions set PS1 to a known default value, which is suitable in most cases

    • However, users may want custom information to show on the command line

      • Ex: Some systems administrators require the user and the host system to show up on the command line as in student@c8 $, which could be implemented by setting the PS1 variable to \u@\h $

        $ echo $PS1

        $

        $ PS1="\u@\h $ "

        student@c8 $ echo $PS1

        \u@\h $

        student@c8 $

  • This could be useful if you're working in multiple roles and want to be always reminded of who you are and what machine you are on

    • By convention, most systems are set up so that the root user has a pound sign (#) as their prompt

Standard File Streams

When commands are executed, by default there are three standard streams (or descriptors) always open for use

  • Standard input (standard in or stdin, value: 0, ex: keyboard)

  • Standard output (standard out or stdout, value: 1, ex: terminal)

  • Standard error (stderr, value: 2, ex: log file)

Usually, stdin is your keyboard, and stdout and stderr are printed on your terminal

  • stdin is supplied by directing input to come from a file or from the output of a previous command through a pipe

  • stdout is often redirected into a file

  • stderr is often redirected to an error logging file

In Linux, all open files are represented internally by file descriptors (They're represented by numbers starting at zero)

  • stdin - 0

  • stdout - 1

  • stderr - 2

  • If other files are opened in addition to these three mentioned above, which are opened by default, they'll start at file descriptor 3 and increase from there

I/O Redirection

Through the command shell, we can redirect the three standard file streams so that we can get input from either a file or another command, instread of from our keyboard, and we can write output and errors to files and use them to provide input for subsequent commands

  • For example:

    • If we have a program called do_something that reads from stdin and writes to stdout and stderr, we can change its input source by using the less-than sign (<) followed by the name of the file to be consumed for input data: $ do_something < input-file

    • If you want to send the output to a file, use the greater-than sign (>) as in: $ do_something > output-file

  • Because stderr isn't the same as stdout, error messages will still be seen on the terminal window in the example above

  • If you want to redirect stderr to a seperate file, you use stderr's file descriptor number (2), the greater-than sign (>), followed by the name of the file you want to hold everything the running command writes to stderr: $ do_something 2> error-file

  • A special shorthand notation can send anything written to file descriptor 2 (stderr) to the same place as file descriptor 1 (stdout): 2>&1; $ do_something > all-output-file 2>&1, bash permits an easier syntax: $ do_something >& all output-file

Pipes

The UNIX/Linux philosophy is to have many simple and short programs (or commands) cooperate together to produce quite complex results, rather than have one complex program with many possible options and modes of operation

  • In order to do this, we use the vertical-bar, pipe symbol (|), between commands, which we call a pipeline (ex: $ command1 | command2 | command3)

    • A pipeline allows Linux to combine the actions of several commands into one

      • This is extraordinarily efficient because command2 and command3 don't have to wait for the previous pipeline commands to complete before they can begin hacking at the data in their previous input streams; on multiple CPU or core systems, the available computing power is much better utilized and things get done quicker

      • There's also no need to save output in (temporary) files between the stages in the pipeline, which saves disk space and reduces reading and writing from disk, which is often the slowest bottleneck in getting something done

Wildcards and Matching File Names

You can search for a filename containing specific characters using wildcards

  • ? - Matches any single character

    • To search for files using the**?** wildcard, replace each unknown character with ?

    • ex: ls ba?.out

  • ***** - Matches any string of characters

    • To search for files using the * wildcard, replace the unknown string with *

    • ex: ls *.out

  • [set] - Matches any character in the set of characters

    • ex: [adf] will match any occurrence of a, d, or f

  • [!set] - Matches any character not in the set of character