What are the most useful UNIX commands

The most important Unix commands

The Unix shell

What is that

In principle, one can work with an operating system on different levels in order to e.g. B. Create, edit, move or delete files:

  • as a user of a graphical user interface (GUI) using programs such as the OS X Finder, Windows Explorer or Gnome Nautilus,
  • as a programmer by calling up functions of the operating system,
  • as a shell user who (apparently) writes cryptic commands into text input.

And it is precisely this last way that is meant to be here.

In principle, there are at least five good reasons why you would want to use a shell:

  • It is usually faster to do something in a shell than to get the equivalent result in a GUI.
  • Shell scripts can be written faster than (C) programs.
  • A shell is usually smaller (in terms of the consumption of computing resources) than a GUI.
  • If you log in to another computer via SSH, you get a shell (getting a GUI is not trivial).
  • In a shell, commands for extremely simple and extremely complex processes are structured according to the same principle (with a GUI you often have to use different tools or even install additional software).
  • If you know about it, every few weeks you can extremely astonish someone who is not familiar with it.

Different shells

There are a lot of shells. All of them have cryptic names, and most of them end in -sh. Well-known examples are:

  • is the first modern shell, called the Bourne shell. A compatible shell is available on almost every Unix system.
  • is the Bourne Again Shell, a successor to and the standard shell in most Linux distributions, also in the faculty network.
  • is the C shell.
  • is an evolution of the one that some people like more than.

The rest of this article is about that; if you use a different shell, you can read the differences on the man pages.

Examples

This is how you can display the files in the current directory:

ls

And this is how the display becomes more detailed:

ls -l

... and with hidden files (these are files whose names begin with a period):

ls -la

Tip: Can you describe yourself as "list -long, all "notice.

Directory change:

cd # go to your own home directory cd foo # go to directory foo cd .. # go one directory up cd. # go to the current directory (pointless, but possible)

(is the comment mark of the. and everything to the right of it is ignored during the interpretation.)

This snippet replaces an obsolete email address in all html files in the current directory with the help of the vi text editor:

for htmlfile in * .html; do \ vi -c ': 1, $ s / alt @ meinedomain.de / webmaster @ meinedomain.de / g' -c ': wq' "$ htmlfile"; done

Alternatively you can write:

for i in * .html do vi -c ': 1, $ s / alt @ meinedomain.de / webmaster @ meinedomain.de / g' -c ': wq' "$ htmlfile" done

Another alternative (whereby the subdirectories are also processed recursively):

find. -name '* .html' -type f -exec \ vi -c ': 1, $ s / alt @ meinedomain.de / webmaster @ meinedomain.de / g' -c ': wq' '{}' ';'

Principles

The idea of ​​the shell is that you have a relatively simple (and consisting of many abbreviations) programming language that you can enter directly into an interpreter (the shell) - exactly the right toy for computer scientists.

You can also write commands to a file and then have them executed in one piece. These files are called shell scripts. Example:

#! / bin / bash # This is a shell script. The first line tells the operating system # to interpret this file using bash. # # The rest of the file consists of bash commands and in this case # is pointless (and usually harmless). FOO = "foo bar" # a variable (name on the left, content on the right) # there must be no spaces around the equal sign. echo -n '$ FOO =' echo $ FOO echo -n '"$ FOO" =' echo "$ FOO" echo -n "'$ FOO' =" echo '$ FOO'

If you put this text in a text file, e.g. B., saves and makes them executable, e.g. B. with

chmod 700 foo.sh

then you can run it like a normal Unix program.

Commands

There are four types of simple commands:

  • shell-internal commands (for example),
  • Control structures of the shell (for example),
  • Aliases (primitive macros which are then replaced by something else) and
  • External program calls (for example and).

You always enter the command itself followed by any parameters. The parameters are separated from the command and from each other by spaces (typically spaces).

Commands can be summarized in several ways:

  • as a concatenation with a semicolon between two commands:
echo foo; echo bar
  • as a command block delimited by curly brackets:
{echo foo; echo bar; echo qux}
  • as a pipeline, where the instructions are separated by a pipe | be separated:
echo 'pollution' | tr mu nü | cat - test -e foo.bar && echo "File exists." || echo "File does not exist."

Pipe(line)s | cause the output of the first command to become the input of the second command; its output goes to the third command, and so on. Since many Unix programs produce, convert or read text, this is a powerful tool for doing complicated things with simple means, which are at best possible in a GUI if the developer has planned them precisely in the form.

For example produces:

cat foo.txt | sort | uniq> bar.txt

the file that contains the sorted lines with no duplicate lines. The character redirects the output to the file. The whole thing can be more elegant than

{sort | uniq} bar.txt

write. (Logically redirects the input so that it comes from the file.)

If you end a command with an (ampersand) (that is, the space is important), it will be executed in the background and you can do something else in the shell for as long. If you have forgotten that, you can stop a running command with the shortcut -Z and continue with the command in the background.

If you execute a command in the foreground in the shell and close it, the current program is usually (but not necessarily) terminated with the shell. It is better to first exit the program and then the shell. Alternatively, you can use the command to completely decouple the program from the shell.

Each command has its own set of possible and / or required options that are different for each command. However, the notation of the arguments is similar for many commands (there are exceptions):

  • Short form:, arguments consisting of one letter each with a hyphen in front of each argument
  • Abbreviated short form:. This form is not supported by all commands and can sometimes have a very unexpected effect.
  • Long form: etc. Here words written from words (or at least longer strings) are given by two hyphens (but sometimes only one). These arguments cannot be summed up. This form is possible with many GNU programs.
  • Parameterized options (in English 'flags'): Sometimes there are arguments that consist of a switch and a value, e.g. B.,,,,.

There are exceptions to all of these regularities. So it is always a good idea to consult man pages.

What do you see

You always have a prompt first, which by default looks like this in the faculty network:

conde mhaecker 11 (~):
  1. The name of the computer on which you are currently logged in is always in front. At university, for example, it can be (fast), (lame) or one of many others. (Which computer is fast naturally depends on how many hundreds of students are currently trying to use the rather resource-intensive KDE graphical user interface.)
  2. This is followed by the user name of the person who owns the shell, that is, in whose name the entered commands are executed (is Martin Häcker's user name).
  3. The number after that indicates how many commands have already been entered in this shell. (This is mostly not useful.)
  4. The current directory is then shown in brackets (is an abbreviation for your own home directory).

The appearance and the content of the prompt can be adjusted by setting the shell variable. For example, by typing

PS1 = '\ [\ 033 [01; 32m \] \ u @ \ h \ [\ 033 [01; 34m \] \ w \ $ \ [\ 033 [00m \]'

Get the standard Gentoo prompt (it is not necessary to understand the syntax of the prompt description), the form

estar @ fiesta ~ $

has (username,, computer name,), or with

PS1 = '\ $'

an extremely simple prompt.

Commands can be entered at the prompt. After a complete command has been entered, press Enter to let the shell execute the command. When the shell is done, the prompt reappears.

Many things, such as setting your own prompt, you want to do automatically every time you start the shell. This works by adding the appropriate commands to the file in the home directory.

Before editing

  • This file can be so broken that you can no longer open a shell. So all commands should be tested in the shell first.
  • In order for SSH to work properly, the commands in no Produce output.
  • It makes sense to read the documentation of the first.

Important commands

Readers are encouraged to try out interesting commands right away. That makes it easier to remember later. You should also read the manual pages for each command before you begin using them in any serious way.

Since, according to the Unix philosophy, the user always knows best what to do, the programs presented here generally do not ask whether, for example, files should "really" be deleted, but simply do it. (However, this is partially configured differently in the faculty network.)

stands for list and shows the contents of the current directory.

Useful options are:

  • show all files (including "hidden" files that start with ".")
  • long format showing a lot of useful information about the files

stands for change directory and does just that.

CD

always brings you back to your home directory,

cd ..

to the directory that contains the current directory and

cd a directory name

in exactly the specified directory.

stands for move and moves files (including directories). Since renaming is a special case of moving, files can also be renamed.

Call:

mv old_name new_name

stands for copy and copies files (with the parameter also directories).

Call:

cp name1 name2

copies the file to,

cp name directory /

copies the file to the directory and

cp -R directory1 directory2

copies the directory recursively, if it does not yet exist, or into the directory, if it does exist.

See SSH.

See SSH.

shows files comfortably and easily. In you can get detailed help with "h". Most important commands: (space) for next page, (backwards) for previous page, otherwise arrow keys. looking for template and you can jump to the next location with. goes to the very end and goes to the very end and shows new lines if they are added.

Call:

less filename

searches for text in files or, if no files are specified, in input. You can also search for regular expressions.

Call:

grep 'text to search for' filename ... egrep '[rR] normal * expression' filename ...

See manpage.

Packs and unpacks files. In the difference z. B. WinZip works with exactly one file. Packing multiple files is explained below.

Pack:

gzip file

creates a compressed file with the name and clears the file .

Packing without deleting:

cp file file.bck gzip file mv file.bck file

(see also the descriptions of and).

Unpack:

gunzip file.gz

unpacks the data from to and clears.

Extract without deleting:
works in the same way as packing without deleting.

is the tape archiver and was originally used for backups to tape drives. It has now become the standard Unix tool for packing many files into one file.

Call:

tar fvc file.tar filename ...

creates the archive from the specified files (and directories!).

tar fvt file.tar

shows the content of the archive.

tar fvx file.tar

unpacks all files from and

tar fvx file.tar filename ...

only those specified.

Explanation of the options:

  • stands for file, d. i.e., should edit an archive file. (Otherwise the behavior is different depending on the implementation.)
  • stands for verbose. In this mode shows more information than usual.
  • stands for create and says that it should create a new archive.
  • shows the files in an archive.
  • stands for extract and does just that.
  • stands for zip. If you add this parameter, you work with gzip-compressed archives. Otherwise the usage remains the same; instead of always writing, it makes sense.

Of the options, and exactly one should be specified.

When creating an archive that is meant for other people, you should generally put exactly one directory in it, the name of which has something to do with the contents. This makes it easier to keep track of the unzipped files after unpacking.

If you send a lot of files in one email, you should first pack them into a compressed archive. This saves the recipient work (who likes to save a dozen individual attachments?) And is therefore polite. WinZip would do the same thing in principle, but not all Unix users can read WinZip files.

Source code is often found in compressed archives (tarballs) offered.

and

a minimal text editor from the mail program. Its great advantage is that it always shows a menu at the bottom of the screen. "" always stands for. is a standalone version of the editor.

Keyboard shortcuts:

  • to exit the editor

[X] Emacs is a text editor that was originally developed in the context of the GNU project (the origin of the GPL) by Richard Stallman (founder of the Free Software Foundation).

Keyboard shortcuts:

  • (x and c one after the other, hold down the control key) to quit Emacs.

Further information is available here.

or

(visual) is the Unix standard text editor and is available on almost every Unix, so you should at least master the most important commands. (Vi IMproved) is the most popular and powerful Vi clone.

Keyboard shortcuts:

  • to exit. Everything else can be read elsewhere.

stands for change mode and changes the access rights of files, i.e. who is allowed to read and write them and who is not. This is important when you want to / have to work with other people. You can tell that you need it when you get error messages like "Permission Denied". An explanation of the Unix file rights and thus also the options can be found elsewhere.

With (group administrativia) you can register yourself in Unix groups. This is important if you want to do homework in groups. See also http://irb.cs.tu-berlin.de/leitfaden/arbeiten-in-gruppen.html

Note: This command is a specialty of the IRB network! In addition, this often only works if you log into pepita. The changes are then applied on the hour.

With you can, among other things, change the resolution of a monitor connected to a SunRay. This is particularly useful if, for example, the standard resolution has not been restored after a power failure. When entering the command in a terminal ('/ usr / local / bin / utsettings') a small program opens in a window, the operation of which should be self-explanatory.

Other programs

Many interesting programs, e.g. B. Firefox and Eclipse, but also relatively current GNU implementations of the standard Unix tools, are installed and maintained by students.

You can find all these programs in the directory. So it's worth taking a look every now and then. Anyone who uses these programs more often should make the entry

pub

Add. If you add it above

default

then this has the effect that the shell first searches for programs, e.g. B. first finds the GNU tools (the Solaris counterparts are sometimes significantly different), which should be especially good news for Linux users.

Before using the programs or installing something there, you should read the instructions from the IRB (which are no longer 100% true, however).

Outlook, left

It is worth knowing at least a rudimentary knowledge of Shell, precisely because you cannot avoid it at university. Further information can be found in the instructions of the department and everywhere else on the Internet.

It is also worth knowing at least one shell script language. However, I am of the opinion that you should get a real programming language such as Python, Perl or Ruby into your mental boat for this purpose.

Web links