Professional Documents
Culture Documents
In Unix most operations are carried out by typing commands at a command prompt,
usually called a terminal or shell. Many of these commands have optional features, and
these are usually activated by switches. A switch is specified by a hyphen "-" (or
occasionally two hyphens "--") followed by a letter or word. Every command has its own
set of switches, but we've included the most common in this list.
1. Listing files
2. These switches can be combined, for example ls -al would list all files in the
current directory, including hidden files, in a long format. You can also give a
directory or file name, e.g. ls /home/pjh503 lists files in the directory
"/home/pjh503".
3. Special characters
In Unix some characters have special meanings that you can use. For example
the asterisk "*" means "any string", so the command ls a*isk will list all the files in
the current directory that start with "a" and end "isk". These special characters
are not allowed to be used in the actual names of files or directories.
4. Files
6. Text files
writes the whole of file "file1" to the terminal, also useful for
cat file1
concatenating files
more file1 displays the file "file1" a page at a time
less file1 a more versatile version of "more", but less common
head -30 file1 show the first 30 lines
tail -25 file1 show the last 25 lines
tail -f file1 show the last few lines and keep updating as the file grows
wc file1 counts lines, words and characters in a file
7. Help
8. Useful keypresses
In Unix only the administrator (called "root") can modify everything. Each file and
directory is owned by one of the users, and (like users) also belongs to a "group".
As a user you can set whether your files can be read, written or executed by just
you, only members of your group, or everyone. You can see the permissions of a
file using ls -l. E.g.,
The important part is the string of 10 characters at the start. The first character
says what type of thing this entry is, usually either "-" for a normal file or "d" for a
directory. You can see in the example that "fortran.html" is a file, and "Linux" is a
directory.
The next nine characters are actually grouped as three sets of three. The first set
of three refers to the permissions the owner, in this case "pjh503" (me). The "r"
means I can read the file, the "w" means I can write to the file, and the "-" means
that I cannot execute it. If I could execute it, it would have an "x" in the third
permission. For directories "execute permission" has a different meaning -- it
means I can list its contents.
The next set of three refers to users who are not the owner (pjh503 in this case)
but are in the same group as the owner (phys in the example above). In the
example above users in the group phys can read the file "fortran.html" but not
write to it.
The final three characters refer to anyone who is neither the owner, nor in the
same group as the file.
u user
g group
o others
a all (equivalent to ugo)
+ gain
- lose
r read permission
w write permission
x execute permission
These can be combined, so for example to grant read and write permission to the
user who owns a file and anyone in the same group as the file you could
usechmod ug+rw.
11. Calculator
bc -l command-line calculator
12. Processes
list my processes (programs) that are running, along with their process ID
ps
(PID)
kill pid kill (stop) my process with the given PID
show the top few processes sorted according to CPU usage
top Once top is running, type M to sort by memory usage instead, and q to
quit
You can use the up and down arrows on the keyboard to scroll through previous
commands. Alternatively:
history 15 lists your last 15 (variable) commands and numbers them
!! repeats your last unix command
!23 repeats the command numbered 23
!f90 repeats the last command beginning with e.g. "f90"
!!addtext appends "addtext" to previous command line
^string1^string2 substitutes "string2" for "string1" in previous command
15. Redirection
Many commands take their input from the keyboard and write their output to the
terminal window, but this isn't always what you want. You can redirect the input
and output of commands.
gzip file1 compress file "file1". The compressed file will be called "file1.gz"
uncompress file "file1.gz". The uncompressed file will be called
gunzip file1.gz
"file1"
# 1: cd
The cd command is THE most important command there is in linux i think. As the command
suggests, it enables the user to change / jump to a directory.
Example: cd Downloads
Example: cd /etc/
Example: cd .. (Directory up!)
note: Afther you have typed cd and entering the first or two letters of the directory you can press
the TAB key to autocomplete the directory! so... cd Do (press tabkey) will autocomplete it to
Downloads. i use this a lot <--
#2: man
The man command shows the users the "manual" of the command. In some situation you might
need to get more information about the command you are using. The man command shows you
this information about the command.
Example: man cp
This will open up the "cp" manual document for us in the shell. The manual shows us the
parameters available for the commands.
#3: ls
In the absolute top 15 there's no way the ls command is missing. On the third place, the ls
command. The ls command is used to list the files/directories within a directory.
Example: ls
#4: cp
The cp command is available for us to "Copy" things. This might be usefull for duplicating files
f.e.
#5 mv
The mv command is used for "Move" operations. The mv commands enables the users to move
a file/directory to a specified location.
note: the first part of the command is the file that has to be moved. The second part (after the
whitespace) is the target directory. Make sure you type in the full path using this command!
#6 mkdir
This command is used to "make" directories, NOT Files.. (thit is possible with another command
which i will bring up later in this tutorial)
note: The name of the directory is case sensitive which means that Testdirectory is a complete
different directory as testdirectory.
#7 rmdir
When you are able to make directories, you also want to know how to remove them. Removing
directories is done by the rmdir command and belongs to the absolute basic commands in shell
bashing.
note: When the directory is not empty the command will prompt an error message:
#8 touch
Now we know how to make directories and deleting them, i now want to use the touch
command. The touch command is used to make files.
#9 rm
The rm command stand for remove. The rm command is used in order the delete files instead of
directories.
Example: rm vogel
note: To remove files you must have the right permission bits set on the specific file.
#10 tar
Sometimes you have to archive files. Archiving files is a way to pack a set of files to one single
file. The operation is done by the tar command.
Example: tar -cvf test.tar test (Creating a tar file from directory or file test)
Example: tar -xvf test.tar myexctractfolder (Extracts the particular tar file in the current working
directory)
In the example i used parameters. These parameters are telling the tar command how to
behave and how to execute. After the parameters i entered the name of the file which is
test.tar. The second part is the source directory/file of the tar file. In this situation a directory
called test.
#11 pwd
Sometimes you really wonder where you are in the system. PWD is the solution for that
problem.
Example: pwd
#12 ifconfig
ifconfig is a command showing you information about the ethernet adapters on your system. It
contains very usefull information like gateway, ip, and packet statistics. For the average user
this command is rarely used, but i think it's worth knowing it.
Example: ifconfig
note: To gain information about the wireless adapters on your system type iwconfig.
#13 locate
Locate is an extremely fast seaching command. It shows the directories or files each on a new
line.
Example: locate syslog | more (Piping structure used to invert the data from locate to the more
command)
note: Some keywords returns enormous ammounts of hits. Use MORE to (see example) clear
things up a bit.
#14 ping
Ping is used as a network diagnostic command by professionals. Ping offers information about
the network we are on and if the other system responds to us. In cases of troubleshooting
network related problems, ping can do a great job to determine the domain of the problem.
The command returns the interval and % of loss during the test.
note: You can stop ping bij pressing crtl-c at the same time.
#15 chmod
The chmod command. The chmod command comes from "Change Mode" back to the unix
times. It's a great command to restrict access to directories or files. But before i show you an
example on how to use it, some theory.
Chmod is qiet an advanced command to use. So therefore you really need to understand how it
works.
chmod works with so called persmission bits. These bits can be set to a certain level of
restrictions.
7 full
6 read and write
5 read and execute
4 read only
3 write and execute
2 write only
1 execute only
0 none