Diff for "CommandlineHowto"


Differences between revisions 50 and 51
Revision 50 as of 2006-12-03 19:30:51
Size: 9272
Editor: 68-235-112-223
Comment:
Revision 51 as of 2006-12-03 20:59:58
Size: 10657
Editor: 68-235-112-223
Comment:
Deletions are marked like this. Additions are marked like this.
Line 193: Line 193:
Commands read input from the keyboard (standard input or stdin) and write to output (standard out or stdout). There is also a special category for error messages called standard error (or stderr). These are automatically created by your shell when it runs you command.

We can redirect input and output to and from a command

=== Simple Redirection ===

If you wanted the output of a command to goto a file instead of going to 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 a file, if one is not found. '''NOTE''' If their is a file that already exists, the above command will overwrite the contents of the file. If you want to add to the end of a existing file then you would use the following syntax.

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 appened with the new information.
Line 213: Line 265:

=== Simple Redirection ===

Note that this is still a work in progress. If you're just looking for a basic introduction to the Linux command line, please see BasicCommands.

Introduction

Even though Ubuntu is the newbie friendly and polished graphical distribution, there are still situations when a serious amount of time and mouse-clickage 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! Smile :)

What is it?

A Command Line is in all simplicity a user interface which is 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), to 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. Warning /!\ The impatient can move right on to the Basic Usage section.

History

In the early days of computers there was only the Command Line. The concept of a Graphical User Interface (GUI) to which most GUI are modelled after today were invented 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 were released. XFree86 was, and is even today, a free adoptation of the original X server.

As mentioned earlier, the CLI (Command Line Interface) were 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 dervatives of UNIX. HP-UX, 1BSD, Solaris, OpenVMS, IRIX, SCO XENIX, etc etc... As more time progressed, GNU/Linux emerged. However, the history of Linux itself is way off the scope for this HOWTO. As time progressed alternative CLI to sh emerged. zsh, ksh, bourne shell, etc 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, it will suffice to say that POSIX is the underlying standards and functionality of how your CLI responds and interacts.

Advantages of using the commandline

So, this all sounds very dull and boring. Not to mention hard work and thinking?BR Well, it isn't really. Wink ;) It's quite easy once you understand the basics.

Some advantage of using the commandline 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, using a graphical interface start you timer - go. Now go to a command line and type du | sort -n we will describe more later. It is faster to do some things on the command line and sometimes easier for graphical.

Prerequisites

This assumes that you are running any version of Ubuntu Linux.BR Desire to learn the inner working of Ubuntu Linux

How to invoke it

For the purpose of this document we will invoke the command line interface from the Desktop.BR

UbuntuBR Applications | Accessories | TerminalBR

KubuntuBR KDE Menu | System | Konsole Terminal ProgramBR

Xubuntu 6.10BR Applications | System | TerminalBR

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 computers hostname, and ~ is the current 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 interpeter for your terminal.
  • A command is usually a small utility which 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 command/application that is currently running on your computer. It doesn't actually have to be active, it can sleep or be in a number of 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 shell in ubuntu.

Single Command

The command syntax will vary with each command, here are some examples.

The simpliest way to use some commands is to type just the command.BR

  • 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 content of the current directory, while other commands may require 1 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 show you the content of the file1.txt file, by adding the filename as the argument for the cat command. All the 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 current directory has been displayed in the reverse order.

Multiple Commands

Sometimes the desired task may require more then command to be completed.

If you want to execute 2 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,

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:~ $

The example above you will notice nothing happened when command 1 did not complete successfully.

If you want command1 to fail 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,
dud@shadowplay:~ $ grep purple file1.txt && grep blue file1.txt
Violets are blue,
dud@shadowplay:~ $

The example above you will notice command2 was only executed when command1 failed.

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 category for error messages called standard error (or stderr). These are automatically created by your shell when it runs you command.

We can redirect input and output to and from a command

Simple Redirection

If you wanted the output of a command to goto a file instead of going to 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 a file, if one is not found. NOTE If their is a file that already exists, the above command will overwrite the contents of the file. If you want to add to the end of a existing file then you would use the following syntax.

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 appened with the new information.

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 following example is using the output from the ls as the input into the sort command, you will notice the listed has been sorted.

CommandlineHowto (last edited 2017-09-11 20:21:18 by ckimes)