Introducing
Your new presentation assistant.
Refine, enhance, and tailor your content, source relevant images, and edit visuals quicker than ever before.
Trending searches
Linux admin
week 3
If you spend any amount of time with experienced Linux users, you have heard them mention the command line. Some, especially those who have begun their journey in the Linux world using distributions that make it easy to complete many tasks using a graphical user interface (GUI), such as Ubuntu, might speak with trepidation about the mysteries of the text interface. Others either praise its power or comment about doing something via the command line as if it is the most natural and obvious way to complete a task.
It is not necessary for you to embrace either extreme. You might develop an affinity for the command line when performing some tasks and prefer the GUI for others. This is where most users end up today. Some might say that you will never need to access the command line because Ubuntu offers a slew of graphical tools that enable you to configure most things on your system. Although the premise might be true most of the time, there are some good reasons to acquire a fundamental level of comfort with the command line that you should consider before embracing that view.
Sometimes things go wrong, and you might not have the luxury of a graphical interface to work with. In these situations, a fundamental understanding of the command line and its uses can be a real lifesaver. Also, some tasks end up being far easier and faster to accomplish from the command line. More important, though, you will be able to make your way around a command-line-based system, which you will encounter if you ever work with a Linux server because most Linux servers have no GUI, and all administration is done using a command-line interface.
Although you can happily log in on your computer, an act known as a local login, you can also log in to your computer via a network connection from a remote computer. Linux-based operating systems provide a number of remote access commands you can use to log in to other computers on your local area network (LAN), wide area network (WAN), or the Internet. Note that you must have an account on the remote computer, and the remote computer must be configured to support remote logins; otherwise, you won’t be able to log in.
Sometimes, the output of a command is too long to view on one screen. At other times, there might be an advantage to preserving the output as a file, perhaps to be edited later. You can’t do that using cat or less, at least not using them as we have described so far. Good news: It is possible using redirection.
Commands take their input and give their output in a standard place. This is called standard input and standard output. By default, this is configured as the output of a keyboard for standard input because it comes in to the computer from the keyboard, and the screen for standard output because the computer displays the results of a command to the user using that screen. Standard input and standard output can be redirected.
The two most common things a user wants to know when comparing two files is what in the files is the same and what is different. This is especially useful when comparing current versions of configuration files with backup versions of the same files. These commands make that task easy. Because looking for differences is more common, we start there.
The diff command compares files line by line and outputs any lines that are not identical.
matthew@seymour:~$ diff file1 file2
This command outputs every line that is different between the two. If file1 and file2 are different versions of a configuration file, say the current and a backup, the output quickly tells you what, if anything, has changed. This can help when a config file is automatically updated during an operating system upgrade or when you make a change that doesn’t work as well as you had planned and then go back a couple of weeks later to change the configuration back.
There are several options you may use when running diff (the original UNIX-style versions like -i and the newer style versions like --ignore-case are identical in what they do; it might simply be easier for you to remember one than the other). Here are a few of the more useful ones to get you started:
Image -i or –ignore-case ignores case differences in file contents.
Image -b or –ignore-space-change ignores changes in the amount of white space.
Image -w or –ignore-all-space ignores all white space.
Image -q or --brief outputs only whether the files differ.
Image -l or --paginate passes the output through 'pr' to paginate it.
comm command
The comm command compares files line by line and outputs any lines that are identical
matthew@seymour:~$ comm file1 file2
This command output displays in three columns: column 1 shows lines only in file1, column2 shows every line only in file2, and column 3 shows every line that is the same between the two files. This is a much more detailed comparison than with diff, and the output can be overwhelming when all you want is to find or check for one or two simple changes. However, it can be incredibly useful when you aren’t terribly familiar with either file and want to see how they compare.
There are fewer options available when running comm. These three are the ones you are most likely to be interested in:
Image -1 (the number one) suppresses the output of column 1.
Image -2 (the number two) suppresses the output of column 2.
Image -3 (the number three) suppresses the output of column 3.
combining commands
All the commands we have looked at have printed their information to the screen, but this is often flexible.
A pipe is a connector between one command’s output and another’s input. Instead of sending its output to your terminal, using a pipe sends that output directly to another command as input.
Two of the commands we have looked at so far are ps and grep: the process lister and the string matcher. We can combine the two to find out which users are playing Nethack right now:
ps aux | grep nethack
If you want to run a second command only if the first command is successfully completed, you can. Every command you issue to the system outputs an exit status, 0 for true (successful) and 1 for false (failed). The system receives these, even if they are not displayed to the user. The && operator, when added to the end of a command, reads that exit status and confirms its value as 0 for true before allowing the next command to be run. Again, the letters represent commands.
matthew@seymour:~$ i && k
You can do the exact opposite with , which only runs the following command if the first one returns an exit status of 1 for false.
matthew@seymour:~$ m || n
If you want to have a set of commands run in order, but not use the output from one as the input to the next one, you can. Separating commands with a ; (semicolon) will cause the system to treat each item as if it was entered on a new line after the previous item finished running. Let’s say you have three commands called doctor, rose, and tardis. You could run each in order using this command:
matthew@seymour:~$ doctor ; rose ; tardis
Note that the spaces before and after the semicolon are optional, but they do make the line easier to read.
Process Substitution
Sometimes the output of one or more commands is precisely what you want to use as the input to another command. You can use output redirection for this as well, using what we call process substitution. In process substitution, you surround one or more commands with ()s and precede each list with a <. When you do this, do NOT insert a space between the < and the opening (. The resulting command looks like this:
matthew@seymour:~$ cat <(ls -al)
This is faster because you don’t have to wait for temporary files to be written and then read; it saves both disk space and the time needed to clean up temporary files. One especially neat advantage of doing this is that Bash automatically runs the multiple tasks being used as input in parallel, which is faster than doing them sequentially with redirects like this: matthew@seymour:~$ ls firstdirectory > file1.txt
matthew@seymour:~$ ls seconddirectory > file2.txt
matthew@seymour:~$ diff file1.txt file2.txt
A number of in-memory variables are assigned and loaded by default when you log in. These variables are known as environment variables and can be used by various commands to get information about your environment, such as the type of system you are running, your /home directory, and the shell in use. Environment variables are used to help tailor the computing environment of your system and include helpful specifications and setup, such as default locations of executable files and software libraries. If you begin writing shell scripts, you might use environment variables in your scripts. Until then, you need to be aware only of what environment variables are and do.
The following list includes a number of environment variables, along with descriptions of how the shell uses them:
Image PWD—Provides the full path to your current working directory, used by the pwd command, such as /home/matthew/Documents
Image USER—Declares the user’s name, such as matthew
Image LANG—Sets the default language, such as English
Image SHELL—Declares the name and location of the current shell, such as /bin/bash
Image PATH—Sets the default locations of executable files, such as /bin, /usr/bin, and so on
Image TERM—Sets the type of terminal in use, such as vt100, which can be important when using screen-oriented programs, such as text editors
You can print the current value of any environment variable using echo $VARIABLENAME, like this: