6.1 Combining Commands Flashcards

1
Q

A ________ is several commands that are normally written out one-by-one, chained together in order to create a new command.

A

A **compound command** is several commands that are normally written out one-by-one, chained together in order to create a new command.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the three components of a command?

A

program -options arguments

  1. The **program**, which is a binary program that you are running.
  2. The program’s **options**, which changes the behavior of the program being run in the first part of the command.
  3. The **arguments** provided to the program, usually a reference to a directory or file that you want the program to act on.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Compound commands typically follow this format:

A

program -options arguments | program -options | program -options | program -options

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Breakdown this command:

file $(find / -iname *.txt 2>/dev/null) > ~/Desktop/text_files ; tail ~/Desktop/text_files

A
  • Searches the entire computer for files ending in .txt;
  • Verifies that the files found are text files; Ignores any errors it comes across; Creates a list of all found files before saving the list to the desktop
  • Finally, it will open the file and print the last ten lines that were added.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain these command:

  1. ls > list.txt
  2. > list.txt
  3. ls >> list.txt
A
  1. This command takes the output of the ls command and sends it into a new file named list.txt. _If_ the file list.txt already exists, it is overwritten with the output of the ls command.
  2. In this case, we didn’t put a command in front of \> so there is no output to send to the list.txt file.
    * However, the file is still written, just with no output. So a blank file is written. _If_ the file list.txt exists, it is overwritten with nothing.
  3. >> will append the output of the ls command to the list.txt file.
  • If the list.txt file does not exist, it is created.
  • Therefore, using \>\> instead of \> is always safer, unless you want the file to be overwritten.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What do pipes ( | ) do?

A

The pipe ( | ) takes the output of one command and sends it to the input of another command.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain this command:

ls -l | grep *.txt

A

The pipe ( | ) takes the output of one command and sends it to the input of another command.

  • ls -l creates a list of files.
  • | pipes the list from ls into the command that follows.
  • grep searches the files from ls for the string that follows.
  • *.txt matches any file that ends with .txt.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Compound commands with pipes typically follow this format:

A

program -options arguments | program -options | program -options | program -options

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are some common programs users pipe to:(5)

A
  • | head prints only the first 10 lines of output.
  • | tail prints only the last 10 lines of output.
  • | sort sorts the output alphabetically.
  • | sed searches and replaces parts of the output.
  • | awk display only specified parts of the output.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain this command:

cat /etc/passwd | grep sysadmin | awk -F ‘:’ ‘{print $6}’

A
  • cat /etc/passwd dumps the contents of /etc/passwd to output.
  • | pipes that output into the command that follows.
  • grep sysadmin displays lines that contain sysadmin.
  • | pipes that output into the command that follows.
  • awk -F ':' '{print $6}' prints only the sixth field of the line.
  • awk usually looks for a space to use as a field separator, but in this case we want it to separate the line by a colon, because /etc/passwd uses colons to separate its fields.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

In addition to |, you can use ____ to run a series of commands back to back.

A

You can also use a ; to run a series of commands back to back.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Rather than typing the following, how would you use ; :

bash
 $ mkdir dir
 $ cd dir
 $ touch file
 $ ls -l
 -rw-r--r-- 1 user user 0 Sep 4 15:33 file
A

mkdir dir; cd dir; touch file; ls -l

Each command will happen in succession.
- First, the mkdir command, then cd, touch, and finally ls.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Compound commands using ; typically follow this format:

A

program -options arguments ; program -options arguments ; program -options arguments ; program -options arguments

For example: `mkdir dir; cd dir; touch file; ls -l

  • The output would be:
bash
 $ mkdir dir; cd dir; touch file; ls -l
 -rw-r--r-- 1 user user 0 Sep 4 15:33 file
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

If you only want to run the second command if the fist command was successful, what operator would you use?

A

&&

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain this command:

mkdir dir && cd dir && touch file && ls -l

A

cd would only run if mkdir were successful, touch would only run if cd were successful and ls would only run if touch were successful.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Compound commands using && typically follow this format:

A

program -options arguments && program -options arguments && program -options arguments && program -options arguments

17
Q

Explain these operators:

  1. >
  2. >>
  3. |
  4. ;
  5. &&
A
  1. > to create files with the output of a command.
  2. >> to append the output of a command to a file. Creates a file if the file does not exist.
  3. | pipes the output of one command into another command.
  4. -; to chain commands together in succession.
  5. && to chain commands together. The second command runs only if the first command was successful.
18
Q

Compound commands are useful but _do_ require a lot of typing. If you use a compound command often, it might be nice to save it somewhere so you can easily reference it. You can do this by creating an ________.

A

Alias

19
Q

An _______ is a shorthand or custom command that you can define, which will launch any command or compound command, including arguments and redirection.

A

An **alias** is a shorthand or custom command that you can define, which will launch any command or compound command, including arguments and redirection.

20
Q

In computer programing, a ________ is a location that stores some kind of data.

A

In computer programing, a variable is a location that stores some kind of data.

  • We can think of it as a box that holds something so you can refer to it later.
  • If you no longer need what is in the box, you can overwrite its contents with new contents.
21
Q

Bash has a number of built-in variables called _______. They are also known as _______.

A

Bash has a number of built-in variables called **environment variables**. They are also known as **shell variables**.

22
Q

True or False:

Built-in variables are always defined with all lower case letters.

A

False

They are always defined with all upper case letters. For example, $PWD is an environment variable that returns the pwd command.

23
Q

What do these built-in variables do?

  • echo "My name is $USER"
  • echo "My home directory is $HOME"
  • echo "The name of my computer is $HOSTNAME"
  • echo "My type of computer is a $MACHTYPE"
  • echo "My user ID is $UID":
A
  • echo "My name is $USER": Provides the user name of the current user.
  • echo "My home directory is $HOME": Provides the home folder of the current user.
  • echo "The name of my computer is $HOSTNAME": Provides the name of the computer.
  • echo "My type of computer is a $MACHTYPE": Provides the type of computer
  • echo "My user ID is $UID": Provides the UID of the current user.
24
Q

________ in bash refers to any time something on the command line expands or morphs into something else.

A

**Expansion** in bash refers to any time something on the command line expands or morphs into something else.

25
Q

What part of the following command _expands_?

file $(find / -iname *.txt 2>/dev/null) > ~/Desktop/text\ files ; tail ~/Desktop/text\ files

A

In the command, the find command between the $() runs before any other part of the command. This find command _expands_ into a list of items that it found. The rest of the commands are acting on that list, not acting on the find command itself.

26
Q

True or False:

Bash syntax uses the $() for command expansion.

A

True

  • You can put any amount of commands chained together inside these brackets.
  • Bash reads that chunk as whatever is returned from running the commands inside it.
  • Then, the rest of the commands on the line run.

This is quite helpful when writing a script if we want one command to run before another command.

27
Q

True or False

Bash script files often end in .sh to indicate they are a shell script. However, a script file will still run with any extension.

A

True

Linux generally ignores file extensions. Instead, it looks at the contents of the file in order to determine how to use it. Therefore, you can create text files without any extension at all, but it is best practice to use the .sh file if you think other users may interact with your script.

28
Q

One of the most important environment variables on Linux operating systems is the _______ variable. This variable holds the colon-separated list of directories used to find commands that you enter.

A

One of the most important environment variables on Linux operating systems is the PATH variable. This variable holds the colon-separated list of directories used to find commands that you enter. The sole purpose of this environment variable is to hold a list of directories.

For example, if PATH is set to /bin:/usr/sbin and you type echo, Linux looks for an executable program called echo in /bin and then in /usr/sbin.