Diff for "CommandlineHowto"


Differences between revisions 11 and 12
Revision 11 as of 2005-12-03 14:39:56
Size: 12955
Editor: c211-31-5-226
Comment: added a little on IO and pipes.
Revision 12 as of 2006-01-07 08:02:11
Size: 13097
Editor: c-24-125-47-253
Comment:
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
||<tablestyle="float:right; font-size: 0.9em; width:40%; background:#F1F1ED; margin: 0 0 1em 1em;" style="padding:0.5em;">'''Contents'''[[BR]][[TableOfContents]]||
Line 8: Line 9:

[[TableOfContents]]
Line 46: Line 45:
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. Sometimes you need a hammer, sometimes a screwdriver. 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. Sometimes you need a hammer, sometimes a screwdriver.

Command Line HOWTO/Tutorial

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, kzh, 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.

Why you should care

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.

You should care because the CLI can;

  • save you time.
  • help you out of a rough-spot.
  • enable you to use Linux in ways that using a GUI exclusively can not.
  • help you figure out why something doesn't work as expected, and you will most likely be able to fix the issue.

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. Sometimes you need a hammer, sometimes a screwdriver.

Prequisites

This assumes that you are running any version of Ubuntu Linux, with default GNOME. You can however run Fluxbox, KDE, XFCE or basically anything else that you like, but this assumes GNOME for simplicity sake.

How to invoke it

Click on your GNOME Menu. Then proceed to Accessories -> Terminal.BR This will launch what is called gnome-terminal. Which is the terminal emulator that ships with GNOME.

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. Each line of command is parsed by the computer after you press the Enter key. You can try now writing:

ls [ENTER]

This will give you a list of the files and directories within your current location. Warning /!\ Do not type out [ENTER], but hit the physical Enter key! From now on, its assumed that you understand this, and [ENTER] won't be used!

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. You can drive a BMW or a Honda, but you'll still be driving in the same direction on the same road. It's slightly different ways to interact, simply speaking.
  • A command is usually a small application which the shell will execute for you. Usually a command creates output, and often it takes arguments (input).
  • 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.
  • Arguments are usually given either plainly, or by --switch="argument". Commonly they also have a short form eg, -s.
  • 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.

Basic Usage

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.

Commands

I will now list a few of the basic and most useful commands that comes with Ubuntu Linux. Warning /!\ Remember to type the commands exactly as they are written, and end with pressing the Enter key!

Listing files:

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

ls will default to listing your current directory, and sorting by alphabetical order. It will also be quite sparse about the information it returns. Common arguments for ls:

  • ls / will return the file listing for directory /. If ls is given one argument, it will take that as the directory it should work on. It will not use your current working directory in other words.

  • ls -l will give you a more detailed listing.

  • ls -a will list all files, even hidden(files starting with .) files.

  • ls -h will give file sizes in KB/MB/GB, instead of mere Bytes.

Listing current directory:

dud@shadowplay:~ $ pwd
/home/dud

Changing directory:

dud@shadowplay:~ $ pwd
/home/dud
dud@shadowplay:~ $ cd /root/
dud@shadowplay:~ $ pwd
/root

You can see how pwd first lists /home/dud as current directory, then cd changes it to /root/. Which pwd confirms.

Echoing:

dud@shadowplay:~ $ echo "Hello World"
Hello World

A very useful command, which will quite simply echo back what you give it as arguments. Warning /!\ It's important to note that although this works without the quotes, it's a very good practise to start using quotes as you'll be needing that often later on!

List content of a file:

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

The command cat will print out content of text files to your terminal. The file file1.txt had a poem as we saw. Wink ;)

Copying a file:

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

Moving a file:

dud@shadowplay:~ $ ls
file1.txt
file2.txt
dud@shadowplay:~ $ mv file1.txt new_file.txt
dud@shadowplay:~ $ ls
file2.txt
new_file.txt

You might have noticed by now that the CLI is rather mute in terms of giving feedback. Most common commands can be given a -v argument to be a bit more verbose.

dud@shadowplay:~ $ mv -v file1.txt new_file.txt
`file1.txt' -> `new_file.txt'

This will tell you that it moved file1.txt to new_file.txt.

Creating an empty file:

dud@shadowplay:~ $ ls
file1.txt
dud@shadowplay:~ $ touch tempfile.txt
dud@shadowplay:~ $ ls
file1.txt
tempfile.txt

Creating a directory:

dud@shadowplay:~ $ ls
file1.txt
tempfile.txt
dud@shadowplay:~ $ mkdir test_dir
dud@shadowplay:~ $ ls
file1.txt
tempfile.txt
test_dir

Removing a file/directory

dud@shadowplay:~ $ ls -p
file1.txt
tempfile.txt
test_dir/
dud@shadowplay:~ $ rm -i tempfile.txt
rm: remove regular empty file `test.txt'? y
dud@shadowplay:~ $ ls -p
file1.txt
test_dir/
dud@shadowplay:~ $ rm test_dir
rm: cannot remove `test_dir': Is a directory
dud@shadowplay:~ $ rm -R test_dir
dud@shadowplay:~ $ ls -p
file1.txt

So what happened here? First we listed the files, which has 2 files and a directory.

  • You can use ls -p, which will indicate if an item is a file/directory/symlink/etc.

  • Next we used rm -i to delete the file. the -i switch will ask for confirmation before doing things; interactivity.

  • We then tried to delete the directory with rm test_dir, which failed because you have to use rm -R to delete directories.

Warning /!\ EXTREMELY IMPORTANT: The CLI will automatically assume that you have full and utter control of what you are doing! If you tell it to rm -Rf / it will delete your entire harddrive without even telling you that it is doing so! By always using rm -i, you assure that you at least will have to confirm before doing stupid things.

Listing processes:

dud@shadowplay:~ $ ps
  PID TTY          TIME CMD
11278 pts/1    00:00:00 bash
24448 pts/1    00:00:00 ps

This shows the processes running, that is "owned" by you.

  • ps -a will show all processes that is running, for all owners.

  • ps auxww is incredibly handy! It will show all processes except for some specific system processes, but it will also list it in a more readable format; with extra details.

Ok, that should do as an introduction! Big Grin :) You can have a look at for instance:

Control flow

Input/Output

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

./mycommand < input.comes.from.here > output.goes.here 2> errors.go.here

So we can find all instances of a word in a file, eg myfile and place it in result.text like this:

grep -i command < myfile > result.text

You will be able to see all error messages on the console.

Pipes

The true power of Linux comes from the ability to combine simple programs into more complex functions by simply joining them together. This is done by the pipe character '|' represented by a broken bar on the keyboard. For example let's sort our grep command above:

grep -i command < myfile | sort > result.text

Search for command in myfile, push this output into sort and then write the sorted file to result.text.

Environment

Special variables. PATH, PS1, ...

Manipulating Variables

Simple scripting

Further reading


CategoryDocumentation CategoryCleanup

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