6.1 Combining Commands Flashcards
A ________ is several commands that are normally written out one-by-one, chained together in order to create a new command.
A **compound command** is several commands that are normally written out one-by-one, chained together in order to create a new command.
What are the three components of a command?
program -options arguments
- The **program**, which is a binary program that you are running.
- The program’s **options**, which changes the behavior of the program being run in the first part of the command.
- The **arguments** provided to the program, usually a reference to a directory or file that you want the program to act on.
Compound commands typically follow this format:
program -options arguments | program -options | program -options | program -options
Breakdown this command:
file $(find / -iname *.txt 2>/dev/null) > ~/Desktop/text_files ; tail ~/Desktop/text_files
- 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.
Explain these command:
- ls > list.txt
- > list.txt
- ls >> list.txt
- This command takes the output of the
ls
command and sends it into a new file namedlist.txt
. _If_ the filelist.txt
already exists, it is overwritten with the output of thels
command. - In this case, we didn’t put a command in front of
\>
so there is no output to send to thelist.txt
file.
* However, the file is still written, just with no output. So a blank file is written. _If_ the filelist.txt
exists, it is overwritten with nothing. - >> will append the output of the
ls
command to thelist.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.
What do pipes ( | ) do?
The pipe ( |
) takes the output of one command and sends it to the input of another command.
Explain this command:
ls -l | grep *.txt
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 fromls
into the command that follows. -
grep
searches the files fromls
for the string that follows. -
*.txt
matches any file that ends with.txt
.
Compound commands with pipes typically follow this format:
program -options arguments | program -options | program -options | program -options
What are some common programs users pipe to:(5)
- | 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.
Explain this command:
cat /etc/passwd | grep sysadmin | awk -F ‘:’ ‘{print $6}’
-
cat /etc/passwd
dumps the contents of/etc/passwd
to output. -
|
pipes that output into the command that follows. -
grep sysadmin
displays lines that containsysadmin
. -
|
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 afield separator
, but in this case we want it to separate the line by a colon, because/etc/passwd
uses colons to separate its fields.
In addition to |, you can use ____ to run a series of commands back to back.
You can also use a ;
to run a series of commands back to back.
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
mkdir dir; cd dir; touch file; ls -l
Each command will happen in succession.
- First, the mkdir
command, then cd
, touch
, and finally ls
.
Compound commands using ;
typically follow this format:
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
If you only want to run the second command if the fist command was successful, what operator would you use?
&&
Explain this command:
mkdir dir && cd dir && touch file && ls -l
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.