Size: 13529
Comment:
|
Size: 13409
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 81: | Line 81: |
* Arguments are usually given either plainly, or by --switch="argument". Commonly they also have a short form eg, -s. |
ContentsBRTableOfContents |
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!
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. 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. 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
GNOMEBR Applications | Accessories | TerminalBR
KDEBR
XFCEBR
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.
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. 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. 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.
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.
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. If you want it to always ask before running any deletion, you can run a simple command:
alias rm='rm -i'
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! You can have a look at for instance:
[http://www.ss64.com/bash/ HTML - Bash command reference]
[http://www.indiana.edu/~uitspubs/b017/b017.pdf PDF - Unix commands: A Quick Reference Card]
[http://tinyurl.co.uk/k6xz Book - UNIX for Dummies Quick Reference @ amazon.com]
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.
Background Processes
The CLI is not a serial interface to the system. You can give the system commands while it is executing others. To start a process in the background, append a '&' to the command:
sleep 60 & ls
The sleep command runs in the background, and you can still interact with the computer. The best way to think of '&' is like ';' except that it starts commands asynchronously.
If you have a command that is taking a long time, and you want to put it into the background, it is easy. Simply type ctrl-z while the command is running, and it will stop. Then type bg to put it into the background. The command fg will put it back into the foreground.
sleep 60 <ctrl-z> bg fg
Finally, you can kill a foreground process with ctrl-c
Environment
Special variables. PATH, PS1, ...
Manipulating Variables
Simple scripting
Further reading
http://rute.2038bug.com/index.html.gz