特殊:Badtitle/NS100:CommandlineHowto:修订间差异

来自Ubuntu中文
跳到导航跳到搜索
Oneleaf留言 | 贡献
新页面: {{From|https://help.ubuntu.com/community/CommandlineHowto}} {{Languages|UbuntuHelp:CommandlineHowto}} == Introduction == Even though Ubuntu is the newbie friendly and polished graphica...
 
Wikibot留言 | 贡献
无编辑摘要
第1行: 第1行:
{{From|https://help.ubuntu.com/community/CommandlineHowto}}
{{From|https://help.ubuntu.com/community/CommandlineHowto}}
{{Languages|UbuntuHelp:CommandlineHowto}}
{{Languages|UbuntuHelp:CommandlineHowto}}
== Introduction ==
== Introduction ==
Even though Ubuntu is the newbie friendly and polished graphical distribution, there are still situations where a significant amount of time and mouse-clicking can be spared by typing a bit. I don't think this is a bad thing at all; no matter what you do, Linux has one of its real strengths in the Command Line! :)  
Even though Ubuntu is the newbie friendly and polished graphical distribution, there are still situations where a significant amount of time and mouse-clicking can be spared by typing a bit. I don't think this is a bad thing at all; no matter what you do, Linux has one of its real strengths in the Command Line! :)  
=== What is it? ===
=== What is it? ===
A Command Line is, in all simplicity, a user interface based on lines of commands. You can say that it is a '''textual direct serial processor'''. Most commonly, the user interacts directly with the computer by typing one line (although it can be more than one), which triggers actions from the computer based on the syntax of the current processor.<br>
A Command Line is, in all simplicity, a user interface based on lines of commands. You can say that it is a '''textual direct serial processor'''. Most commonly, the user interacts directly with the computer by typing one line (although it can be more than one), which triggers actions from the computer based on the syntax of the current processor.<br>
Before everything gets too complicated, we can quite simply move on.
Before everything gets too complicated, we can quite simply move on.
/!\ The impatient can move right on to the '''Command Syntax''' section.
/!\ The impatient can move right on to the '''Command Syntax''' section.
=== History ===
=== History ===
In the early days of computers, there was only the Command Line. The concept of a Graphical User Interface (GUI) after which most GUI are modeled was developed by engineers at '''Xerox's Palo Alto Research Center (PARC)'''. A bit later, Apple paid a whole bunch of money to be allowed to "study" their GUI idea. And, after a while, Apple had their own GUI.  
In the early days of computers, there was only the Command Line. The concept of a Graphical User Interface (GUI) after which most GUI are modeled was developed by engineers at '''Xerox's Palo Alto Research Center (PARC)'''. A bit later, Apple paid a whole bunch of money to be allowed to "study" their GUI idea. And, after a while, Apple had their own GUI.  
Not until 1986 did UNIX get its first GUI, developed by the '''MIT Project'''. They named it X. Linux, however, had to wait ten more years before XFree86 was released. XFree86 was, and is even today, a free adaptation of the original X server.
Not until 1986 did UNIX get its first GUI, developed by the '''MIT Project'''. They named it X. Linux, however, had to wait ten more years before XFree86 was released. XFree86 was, and is even today, a free adaptation of the original X server.
As mentioned earlier, the CLI (Command Line Interface) was the only way to communicate with computers before the GUI was invented. In 1969, '''Bell Telephone Laboratories''' released V1 of the UNIX Timeshare System. UNIX had a shell called sh, which was the only means of communicating with the computer, and it would stay that way for quite some time.
As mentioned earlier, the CLI (Command Line Interface) was the only way to communicate with computers before the GUI was invented. In 1969, '''Bell Telephone Laboratories''' released V1 of the UNIX Timeshare System. UNIX had a shell called sh, which was the only means of communicating with the computer, and it would stay that way for quite some time.
Later on, there came derivatives of UNIX: HP-UX, 1BSD, Solaris, OpenVMS, IRIX, SCO XENIX, etc. As time progressed, GNU/Linux emerged. However, the history of Linux itself is way off the scope of this HOWTO. Suffice to say that alternative CLI to sh emerged: zsh, ksh, bourne shell, etc.
Later on, there came derivatives of UNIX: HP-UX, 1BSD, Solaris, OpenVMS, IRIX, SCO XENIX, etc. As time progressed, GNU/Linux emerged. However, the history of Linux itself is way off the scope of this HOWTO. Suffice to say that alternative CLI to sh emerged: zsh, ksh, bourne shell, etc.
==== POSIX ====
==== POSIX ====
The Wikipedia defines POSIX as the following:
The Wikipedia defines POSIX as the following:
"POSIX is the collective name of a family of related standards specified by the IEEE to define the application program interface (API) for software designed to run on variants of the Unix OS. They are formally designated as IEEE 1003 and the international standard name is ISO/IEC 9945. The standards emerged from a project, begun circa 1985. The term POSIX was suggested by Richard Stallman in response to an IEEE request for a memorable name; before that the standards effort was called IEEE-IX. POSIX is a near acronym for Portable Operating System Interface, with the X signifying the Unix heritage of the API."
"POSIX is the collective name of a family of related standards specified by the IEEE to define the application program interface (API) for software designed to run on variants of the Unix OS. They are formally designated as IEEE 1003 and the international standard name is ISO/IEC 9945. The standards emerged from a project, begun circa 1985. The term POSIX was suggested by Richard Stallman in response to an IEEE request for a memorable name; before that the standards effort was called IEEE-IX. POSIX is a near acronym for Portable Operating System Interface, with the X signifying the Unix heritage of the API."
This sounds fancy and all, but to stay rather concise, POSIX is the underlying standard and functionality of how your CLI responds.
This sounds fancy and all, but to stay rather concise, POSIX is the underlying standard and functionality of how your CLI responds.
=== Advantages of using the command line ===
=== Advantages of using the command line ===
So this all sounds very dull and boring, not to mention hard.<br>
So this all sounds very dull and boring, not to mention hard.<br>
Well, it isn't, really. ;) It's quite easy once you understand the basics.
Well, it isn't, really. ;) It's quite easy once you understand the basics.
Some advantage of using the command line are:
Some advantage of using the command line are:
* save you time.
* save you time.
* can help when you are unable to use the GUI, such as a system crash or a configuration issue.
* can help when you are unable to use the GUI, such as a system crash or a configuration issue.
* enable you to use Linux in ways that using a GUI exclusively can not.
* enable you to use Linux in ways that using a GUI exclusively can not.
For example, you have been called by the systems administrator that you have used too much space.  You want to quickly work out where the most space is used, so using a graphical interface, start your timer - go.  Now, go to a command line and type: `du | sort -n` (we will describe more later). See? It is faster to do some things on the command line (and other times, easier for graphical).
For example, you have been called by the systems administrator that you have used too much space.  You want to quickly work out where the most space is used, so using a graphical interface, start your timer - go.  Now, go to a command line and type: `du | sort -n` (we will describe more later). See? It is faster to do some things on the command line (and other times, easier for graphical).
=== Prerequisites ===
=== Prerequisites ===
This assumes that you are running any version of Ubuntu Linux and have a desire to learn its inner workings.<br>
This assumes that you are running any version of Ubuntu Linux and have a desire to learn its inner workings.<br>
=== How to invoke it ===
=== How to invoke it ===
For the purpose of this document, we will invoke the command line interface from the Desktop.<br>
For the purpose of this document, we will invoke the command line interface from the Desktop.<br>
'''Ubuntu'''<br>
'''Ubuntu'''<br>
''Applications | Accessories | Terminal''<br>
''Applications | Accessories | Terminal''<br>
'''Kubuntu'''<br>
'''Kubuntu'''<br>
''KDE Menu | System | Konsole Terminal Program''<br>
''KDE Menu | System | Konsole Terminal Program''<br>
'''Xubuntu''' ''6.10''<br>
'''Xubuntu''' ''6.10''<br>
''Applications | System | Terminal''<br>
''Applications | System | Terminal''<br>
=== Basic structure and concepts ===
=== Basic structure and concepts ===
The first thing that you should notice is something like:
The first thing that you should notice is something like:
第64行: 第42行:
</nowiki></pre>
</nowiki></pre>
What you see here is called the prompt. It signifies that the computer is ready and awaiting user input. In my case, '''dud''' is the user that I'm logged in as. '''shadowplay''' is the computer's hostname, and '''~''' is the current directory (the user's home directory).
What you see here is called the prompt. It signifies that the computer is ready and awaiting user input. In my case, '''dud''' is the user that I'm logged in as. '''shadowplay''' is the computer's hostname, and '''~''' is the current directory (the user's home directory).
Concepts:
Concepts:
* A terminal is a "physical"(direct) interface to your Linux Operating System.
* A terminal is a "physical"(direct) interface to your Linux Operating System.
第73行: 第50行:
* Input is the arguments or data that any given command will take. Input will change the way a given command acts.
* Input is the arguments or data that any given command will take. Input will change the way a given command acts.
* A process is a running application on your computer. It can be active, sleeping, or in a number of other states.
* A process is a running application on your computer. It can be active, sleeping, or in a number of other states.
== Command Syntax ==
== Command Syntax ==
This section will try to give you a good rundown of the basic usage for the bash shell, which is the default user shell in Ubuntu.
This section will try to give you a good rundown of the basic usage for the bash shell, which is the default user shell in Ubuntu.
=== Single Command ===
=== Single Command ===
The command syntax will vary with each command. Here are some of the basics.
The command syntax will vary with each command. Here are some of the basics.
The simplest way to use some commands is to type just the command.<br>
The simplest way to use some commands is to type just the command.<br>
'''command'''
'''command'''
Example:
Example:
<pre><nowiki>
<pre><nowiki>
dud@shadowplay:~ $ ls
dud@shadowplay:~ $ ls
第98行: 第67行:
file-with_other-NAME.TXT
file-with_other-NAME.TXT
</nowiki></pre>
</nowiki></pre>
The above example displays the contents of the current working directory, while other commands may require one or more arguments.
The above example displays the contents of the current working directory, while other commands may require one or more arguments.
'''command <argument>'''
'''command <argument>'''
Example:
Example:
<pre><nowiki>
<pre><nowiki>
dud@shadowplay:~ $ cat file1.txt
dud@shadowplay:~ $ cat file1.txt
第111行: 第76行:
and you have the bird-flu!
and you have the bird-flu!
</nowiki></pre>
</nowiki></pre>
The above example shows you the content of the file1.txt file by adding the filename as the argument for the ''cat'' command. Almost all commands, whether they have arguments or not, have options.
The above example shows you the content of the file1.txt file by adding the filename as the argument for the ''cat'' command. Almost all commands, whether they have arguments or not, have options.
'''command -option'''
'''command -option'''
Example:
Example:
<pre><nowiki>
<pre><nowiki>
dud@shadowplay:~ $ ls -r
dud@shadowplay:~ $ ls -r
第128行: 第89行:
file1.txt
file1.txt
</nowiki></pre>
</nowiki></pre>
Using the previous example of showing the current directory, we have added the ''-r'' option. As you can see, the listing of the current working directory has been displayed in the reverse order.
Using the previous example of showing the current directory, we have added the ''-r'' option. As you can see, the listing of the current working directory has been displayed in the reverse order.
=== Multiple Commands ===
=== Multiple Commands ===
Sometimes the desired task may require the use of more than one command to be completed. Here is the syntax for the use of multiple commands.
Sometimes the desired task may require the use of more than one command to be completed. Here is the syntax for the use of multiple commands.
If you want to execute two commands consecutively, then you would use the following syntax:
If you want to execute two commands consecutively, then you would use the following syntax:
'''command1 ; command2'''
'''command1 ; command2'''
Example:
Example:
<pre><nowiki>
<pre><nowiki>
dud@shadowplay:~ $grep red file1.txt ; grep blue file1.txt
dud@shadowplay:~ $grep red file1.txt ; grep blue file1.txt
第146行: 第100行:
Violets are blue,
Violets are blue,
</nowiki></pre>
</nowiki></pre>
In the example above, command1 and command2 are executed. However, if you need command1 to complete successfully before executing command2, then you would use the following syntax:
In the example above, command1 and command2 are executed. However, if you need command1 to complete successfully before executing command2, then you would use the following syntax:
'''command1 && command2'''
'''command1 && command2'''
Example:
Example:
<pre><nowiki>
<pre><nowiki>
dud@shadowplay:~ $ grep red file1.txt && grep blue file1.txt
dud@shadowplay:~ $ grep red file1.txt && grep blue file1.txt
第160行: 第110行:
dud@shadowplay:~ $
dud@shadowplay:~ $
</nowiki></pre>
</nowiki></pre>
In the example above, you will notice nothing happened when the first command did not complete successfully. If you want command2 to execute only if command1 fails, then you would use the following syntax:
In the example above, you will notice nothing happened when the first command did not complete successfully. If you want command2 to execute only if command1 fails, then you would use the following syntax:
'''command1 || command2'''
'''command1 || command2'''
Example:
Example:
<pre><nowiki>
<pre><nowiki>
dud@shadowplay:~ $ grep red file1.txt || grep blue file1.txt
dud@shadowplay:~ $ grep red file1.txt || grep blue file1.txt
第174行: 第120行:
dud@shadowplay:~ $
dud@shadowplay:~ $
</nowiki></pre>
</nowiki></pre>
In the example above, you will notice command2 was only executed when command1 failed.
In the example above, you will notice command2 was only executed when command1 failed.
=== Wildcards ===
=== Wildcards ===
There is a powerful feature when using arguments called wildcards, which are single or multiple characters that can represent anything or a specific range of characters. The syntax for the wildcard is as follows:
There is a powerful feature when using arguments called wildcards, which are single or multiple characters that can represent anything or a specific range of characters. The syntax for the wildcard is as follows:
'''command *'''
'''command *'''
Example:
Example:
<pre><nowiki>
<pre><nowiki>
dud@shadowplay:~ $ ls f*
dud@shadowplay:~ $ ls f*
第193行: 第133行:
file-with_other-NAME.TXT
file-with_other-NAME.TXT
</nowiki></pre>
</nowiki></pre>
The example above gives a list of all files that start with the letter f. The next wildcard we will use allows you to match any single character. The syntax for this wildcard is as follows:
The example above gives a list of all files that start with the letter f. The next wildcard we will use allows you to match any single character. The syntax for this wildcard is as follows:
'''command ?'''
'''command ?'''
Example:
Example:
<pre><nowiki>
<pre><nowiki>
dud@shadowplay:~ $ ls file?.pdf
dud@shadowplay:~ $ ls file?.pdf
file2.pdf
file2.pdf
</nowiki></pre>
</nowiki></pre>
The example above gives you output of only one file that fits the pattern. Finally, the following wildcard allows you to use a range of values, and its syntax is:
The example above gives you output of only one file that fits the pattern. Finally, the following wildcard allows you to use a range of values, and its syntax is:
'''command [abc...]'''
'''command [abc...]'''
Example:
Example:
<pre><nowiki>
<pre><nowiki>
dud@shadowplay:~ $ ls file[0-9].*
dud@shadowplay:~ $ ls file[0-9].*
第218行: 第150行:
file1.pdf
file1.pdf
</nowiki></pre>
</nowiki></pre>
In the above example, there were two wildcards used: the range wildcard followed by matching any strings wildcard. You can see with the above examples just how powerful using wildcards can be.  
In the above example, there were two wildcards used: the range wildcard followed by matching any strings wildcard. You can see with the above examples just how powerful using wildcards can be.  
=== Control Flow ===
=== Control Flow ===
Commands read input from the keyboard (standard input, or stdin) and write to output (standard out, or stdout). There is also a special output category for error messages called standard error (or stderr). These three locations are created automatically for each program.  
Commands read input from the keyboard (standard input, or stdin) and write to output (standard out, or stdout). There is also a special output category for error messages called standard error (or stderr). These three locations are created automatically for each program.  
We can redirect input and output to and from a command.  
We can redirect input and output to and from a command.  
==== Redirection ====
==== Redirection ====
If you wanted the output of a command to go to a file instead of the terminal, then you would use the following syntax:
If you wanted the output of a command to go to a file instead of the terminal, then you would use the following syntax:
'''command > filename'''
'''command > filename'''
Example:
Example:
<pre><nowiki>
<pre><nowiki>
dud@shadowplay:~ $ ls > file4.txt
dud@shadowplay:~ $ ls > file4.txt
第247行: 第170行:
file4.txt
file4.txt
</nowiki></pre>
</nowiki></pre>
The above example will create file4.txt if it is not found. '''NOTE:''' If file4.txt exists already, the above command will overwrite its contents. If you want to add to the end of a existing file, then you would use the following syntax:
The above example will create file4.txt if it is not found. '''NOTE:''' If file4.txt exists already, the above command will overwrite its contents. If you want to add to the end of a existing file, then you would use the following syntax:
'''command >> filename'''
'''command >> filename'''
Example:
Example:
<pre><nowiki>
<pre><nowiki>
dud@shadowplay:~ $ ls >> file4.txt
dud@shadowplay:~ $ ls >> file4.txt
第274行: 第193行:
file4.txt
file4.txt
</nowiki></pre>
</nowiki></pre>
In the example, you will notice the file was appended with the new information. Now we are going to do a different redirection: We are going to take the input from a file for the command to be executed. Here is the syntax for this redirection:
In the example, you will notice the file was appended with the new information. Now we are going to do a different redirection: We are going to take the input from a file for the command to be executed. Here is the syntax for this redirection:
'''command < filename'''
'''command < filename'''
Example:
Example:
<pre><nowiki>
<pre><nowiki>
dud@shadowplay:~ $ sort < file4.txt
dud@shadowplay:~ $ sort < file4.txt
第298行: 第213行:
Yet-Another_file.txt
Yet-Another_file.txt
</nowiki></pre>
</nowiki></pre>
As you can see from this example, we used the file4.txt as input into the sort command.
As you can see from this example, we used the file4.txt as input into the sort command.
==== Pipe ====
==== Pipe ====
When you need the output from command 1 for the input into command 2, then you would use pipe character '|'. Here is the syntax for the pipe character:
When you need the output from command 1 for the input into command 2, then you would use pipe character '|'. Here is the syntax for the pipe character:
'''command | command'''
'''command | command'''
Example:
Example:
<pre><nowiki>
<pre><nowiki>
dud@shadowplay:~ $ ls | sort
dud@shadowplay:~ $ ls | sort
第320行: 第228行:
Yet-Another_file.txt
Yet-Another_file.txt
</nowiki></pre>
</nowiki></pre>
The above example is using the output from ls as input to the sort command. You will notice the list has been sorted.
The above example is using the output from ls as input to the sort command. You will notice the list has been sorted.
As you can see, the command line is an easy and powerful way of completing many tasks. If you want more information on using the command line, then look at the further reading section of this document.
As you can see, the command line is an easy and powerful way of completing many tasks. If you want more information on using the command line, then look at the further reading section of this document.
== Further reading ==
== Further reading ==
http://rute.2038bug.com/index.html.gz <br>
http://rute.2038bug.com/index.html.gz <br>
BasicCommands has a list of basic commands.<br>
BasicCommands has a list of basic commands.<br>

2007年11月30日 (五) 16:29的版本

{{#ifexist: :CommandlineHowto/zh | | {{#ifexist: CommandlineHowto/zh | | {{#ifeq: {{#titleparts:CommandlineHowto|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:CommandlineHowto|1|-1|}} | zh | | }}

Introduction

Even though Ubuntu is the newbie friendly and polished graphical distribution, there are still situations where a significant amount of time and mouse-clicking can be spared by typing a bit. I don't think this is a bad thing at all; no matter what you do, Linux has one of its real strengths in the Command Line! :)

What is it?

A Command Line is, in all simplicity, a user interface based on lines of commands. You can say that it is a textual direct serial processor. Most commonly, the user interacts directly with the computer by typing one line (although it can be more than one), which triggers actions from the computer based on the syntax of the current processor.
Before everything gets too complicated, we can quite simply move on. /!\ The impatient can move right on to the Command Syntax section.

History

In the early days of computers, there was only the Command Line. The concept of a Graphical User Interface (GUI) after which most GUI are modeled was developed by engineers at Xerox's Palo Alto Research Center (PARC). A bit later, Apple paid a whole bunch of money to be allowed to "study" their GUI idea. And, after a while, Apple had their own GUI. Not until 1986 did UNIX get its first GUI, developed by the MIT Project. They named it X. Linux, however, had to wait ten more years before XFree86 was released. XFree86 was, and is even today, a free adaptation of the original X server. As mentioned earlier, the CLI (Command Line Interface) was the only way to communicate with computers before the GUI was invented. In 1969, Bell Telephone Laboratories released V1 of the UNIX Timeshare System. UNIX had a shell called sh, which was the only means of communicating with the computer, and it would stay that way for quite some time. Later on, there came derivatives of UNIX: HP-UX, 1BSD, Solaris, OpenVMS, IRIX, SCO XENIX, etc. As time progressed, GNU/Linux emerged. However, the history of Linux itself is way off the scope of this HOWTO. Suffice to say that alternative CLI to sh emerged: zsh, ksh, bourne shell, etc.

POSIX

The Wikipedia defines POSIX as the following: "POSIX is the collective name of a family of related standards specified by the IEEE to define the application program interface (API) for software designed to run on variants of the Unix OS. They are formally designated as IEEE 1003 and the international standard name is ISO/IEC 9945. The standards emerged from a project, begun circa 1985. The term POSIX was suggested by Richard Stallman in response to an IEEE request for a memorable name; before that the standards effort was called IEEE-IX. POSIX is a near acronym for Portable Operating System Interface, with the X signifying the Unix heritage of the API." This sounds fancy and all, but to stay rather concise, POSIX is the underlying standard and functionality of how your CLI responds.

Advantages of using the command line

So this all sounds very dull and boring, not to mention hard.
Well, it isn't, really. ;) It's quite easy once you understand the basics. Some advantage of using the command line are:

  • save you time.
  • can help when you are unable to use the GUI, such as a system crash or a configuration issue.
  • enable you to use Linux in ways that using a GUI exclusively can not.

For example, you have been called by the systems administrator that you have used too much space. You want to quickly work out where the most space is used, so using a graphical interface, start your timer - go. Now, go to a command line and type: `du | sort -n` (we will describe more later). See? It is faster to do some things on the command line (and other times, easier for graphical).

Prerequisites

This assumes that you are running any version of Ubuntu Linux and have a desire to learn its inner workings.

How to invoke it

For the purpose of this document, we will invoke the command line interface from the Desktop.
Ubuntu
Applications | Accessories | Terminal
Kubuntu
KDE Menu | System | Konsole Terminal Program
Xubuntu 6.10
Applications | System | Terminal

Basic structure and concepts

The first thing that you should notice is something like:

dud@shadowplay:~ $
or
[dud@shadowplay ~]$

What you see here is called the prompt. It signifies that the computer is ready and awaiting user input. In my case, dud is the user that I'm logged in as. shadowplay is the computer's hostname, and ~ is the current directory (the user's home directory). Concepts:

  • A terminal is a "physical"(direct) interface to your Linux Operating System.
  • A terminal emulator is what we'll be using here. This is a CLI wrapped within your running GUI. Any applications running in a terminal emulator will be killed if you close the terminal emulator.
  • A shell is an interpreter for your terminal.
  • A command is usually a small utility that the shell will execute for you.
  • Output is what a command returns; most often this is returned on the terminal.
  • Input is the arguments or data that any given command will take. Input will change the way a given command acts.
  • A process is a running application on your computer. It can be active, sleeping, or in a number of other states.

Command Syntax

This section will try to give you a good rundown of the basic usage for the bash shell, which is the default user shell in Ubuntu.

Single Command

The command syntax will vary with each command. Here are some of the basics. The simplest way to use some commands is to type just the command.
command Example:

dud@shadowplay:~ $ ls
file1.txt
file2.pdf
file3.mp3
file1.pdf
another_file.txt
Yet-Another_file.txt
file-with_other-NAME.TXT

The above example displays the contents of the current working directory, while other commands may require one or more arguments. command <argument> Example:

dud@shadowplay:~ $ cat file1.txt
Roses are red.
Violets are blue,
and you have the bird-flu!

The above example shows you the content of the file1.txt file by adding the filename as the argument for the cat command. Almost all commands, whether they have arguments or not, have options. command -option Example:

dud@shadowplay:~ $ ls -r
file-with_other-NAME.TXT
Yet-Another_file.txt
another_file.txt
file1.pdf
file3.mp3
file2.pdf
file1.txt

Using the previous example of showing the current directory, we have added the -r option. As you can see, the listing of the current working directory has been displayed in the reverse order.

Multiple Commands

Sometimes the desired task may require the use of more than one command to be completed. Here is the syntax for the use of multiple commands. If you want to execute two commands consecutively, then you would use the following syntax: command1 ; command2 Example:

dud@shadowplay:~ $grep red file1.txt ; grep blue file1.txt
Roses are red,
Violets are blue,

In the example above, command1 and command2 are executed. However, if you need command1 to complete successfully before executing command2, then you would use the following syntax: command1 && command2 Example:

dud@shadowplay:~ $ grep red file1.txt && grep blue file1.txt
Roses are red,
Violets are blue,
dud@shadowplay:~ $ grep purple file1.txt && grep blue file1.txt
dud@shadowplay:~ $

In the example above, you will notice nothing happened when the first command did not complete successfully. If you want command2 to execute only if command1 fails, then you would use the following syntax: command1 || command2 Example:

dud@shadowplay:~ $ grep red file1.txt || grep blue file1.txt
Roses are red,
dud@shadowplay:~ $ grep purple file1.txt || grep blue file1.txt
Violets are blue,
dud@shadowplay:~ $

In the example above, you will notice command2 was only executed when command1 failed.

Wildcards

There is a powerful feature when using arguments called wildcards, which are single or multiple characters that can represent anything or a specific range of characters. The syntax for the wildcard is as follows: command * Example:

dud@shadowplay:~ $ ls f*
file1.txt
file2.pdf
file3.mp3
file1.pdf
file-with_other-NAME.TXT

The example above gives a list of all files that start with the letter f. The next wildcard we will use allows you to match any single character. The syntax for this wildcard is as follows: command ? Example:

dud@shadowplay:~ $ ls file?.pdf
file2.pdf

The example above gives you output of only one file that fits the pattern. Finally, the following wildcard allows you to use a range of values, and its syntax is: command [abc...] Example:

dud@shadowplay:~ $ ls file[0-9].*
file1.txt
file2.pdf
file3.mp3
file1.pdf

In the above example, there were two wildcards used: the range wildcard followed by matching any strings wildcard. You can see with the above examples just how powerful using wildcards can be.

Control Flow

Commands read input from the keyboard (standard input, or stdin) and write to output (standard out, or stdout). There is also a special output category for error messages called standard error (or stderr). These three locations are created automatically for each program. We can redirect input and output to and from a command.

Redirection

If you wanted the output of a command to go to a file instead of the terminal, then you would use the following syntax: command > filename Example:

dud@shadowplay:~ $ ls > file4.txt
dud@shadowplay:~ $ cat file4.txt
file1.txt
file2.pdf
file3.mp3
file1.pdf
another_file.txt
Yet-Another_file.txt
file-with_other-NAME.TXT
file4.txt

The above example will create file4.txt if it is not found. NOTE: If file4.txt exists already, the above command will overwrite its contents. If you want to add to the end of a existing file, then you would use the following syntax: command >> filename Example:

dud@shadowplay:~ $ ls >> file4.txt
dud@shadowplay:~ $ cat file4.txt
file1.txt
file2.pdf
file3.mp3
file1.pdf
another_file.txt
Yet-Another_file.txt
file-with_other-NAME.TXT
file4.txt
file1.txt
file2.pdf
file3.mp3
file1.pdf
another_file.txt
Yet-Another_file.txt
file-with_other-NAME.TXT
file4.txt

In the example, you will notice the file was appended with the new information. Now we are going to do a different redirection: We are going to take the input from a file for the command to be executed. Here is the syntax for this redirection: command < filename Example:

dud@shadowplay:~ $ sort < file4.txt
another_file.txt
another_file.txt
file1.txt
file1.txt
file2.pdf
file2.pdf
file3.mp3
file3.mp3
file4.txt
file4.txt
file-with_other-NAME.TXT
file-with_other-NAME.TXT
Yet-Another_file.txt
Yet-Another_file.txt

As you can see from this example, we used the file4.txt as input into the sort command.

Pipe

When you need the output from command 1 for the input into command 2, then you would use pipe character '|'. Here is the syntax for the pipe character: command | command Example:

dud@shadowplay:~ $ ls | sort
another_file.txt
file1.pdf
file1.txt
file2.pdf
file3.mp3
file-with_other-NAME.TXT
Yet-Another_file.txt

The above example is using the output from ls as input to the sort command. You will notice the list has been sorted. As you can see, the command line is an easy and powerful way of completing many tasks. If you want more information on using the command line, then look at the further reading section of this document.

Further reading

http://rute.2038bug.com/index.html.gz
BasicCommands has a list of basic commands.
AdvancedCommandlineHowto has some advanced command line features such as scripting.