Linux指令基础
UNIX Tutorial for Beginners
UNIX operating system
The UNIX operating system is made up of three parts; the kernel, the shell and the programs.
The kernel
The kernel of UNIX is the hub of the operating system: it allocates time and memory to programs and handles the filestore and communications in response to system calls.
The shell
The shell acts as an interface between the user and the kernel. When a user logs in, the login program checks the username and password, and then starts another program called the shell. The shell is a command line interpreter (CLI). It interprets the commands the user types in and arranges for them to be carried out. The commands are themselves programs: when they terminate, the shell gives the user another prompt (% on our systems).
Files and processes
Everything in UNIX is either a file or a process.
A process is an executing program identified by a unique PID (process identifier).
A file is a collection of data. They are created by users using text editors, running compilers etc.
The Directory Structure
All the files are grouped together in the directory structure. The file-system is arranged in a hierarchical structure, like an inverted tree. The top of the hierarchy is traditionally called root (written as a slash / ) .
ls (list)
The ls command lists the contents of your current working directory.
ls -a lists files that are normally hidden.
mkdir (make directory)
mkdir -1 如上级文件夹不存在则递归创建
cd (change directory)
The command cd directory means change the current working directory to 'directory'. The current working directory may be thought of as the directory you are in, i.e. your current position in the file-system tree.
in the unixstuff directory (and in all other directories), there are two special directories called (.) and (..)
The current directory (.)
In UNIX, (.) means the current directory, so typing
% cd .
means stay where you are (the unixstuff directory).
This may not seem very useful at first, but using (.) as the name of the current directory will save a lot of typing.
The parent directory (..)
(..) means the parent of the current directory, so typing
% cd ..
will take you one directory up the hierarchy (back to your home directory).
Note: typing cd with no argument always returns you to your home directory. This is very useful if you are lost in the file system.
pwd (print working directory)
Pathnames enable you to work out where you are in relation to the whole file-system.
~ (your home directory)
Home directories can also be referred to by the tilde ~ character.
Summary
Command |
Meaning |
ls |
list files and directories |
ls -a |
list all files and directories |
mkdir |
make a directory |
cd directory |
change to named directory |
cd |
change to home-directory |
cd ~ |
change to home-directory |
cd .. |
change to parent directory |
pwd |
display the path of the current directory |
Command |
Meaning |
cp file1 file2 |
copy file1 and call it file2 |
mv file1 file2 |
move or rename file1 to file2 |
rm file |
remove a file |
rmdir directory |
remove a directory |
cat file |
display a file |
less file |
display a file a page at a time |
head file |
display the first few lines of a file |
tail file |
display the last few lines of a file |
grep 'keyword' file |
search a file for keywords |
wc file |
count number of lines/words/characters in file |
Command |
Meaning |
command > file |
redirect standard output to a file (ex. cat > file) |
command >> file |
append standard output to a file |
command < file |
redirect standard input from a file |
command1 | command2 |
pipe the output of command1 to the input of command2 |
cat file1 file2 > file0 |
concatenate file1 and file2 to file0 |
sort |
sort data |
who |
list users currently logged in |
Command |
Meaning |
* |
match any number of characters, wildcard(通配符) |
? |
match one character |
man command |
read the online manual page for a command |
whatis command |
brief description of a command |
apropos keyword |
match commands with keyword in their man pages |
Command |
Meaning |
ls -l |
list access rights for all files |
chmod [options] file |
change access rights for named file |
command & |
run command in background |
^C |
kill the job running in the foreground |
^Z |
suspend the job running in the foreground |
bg |
background the suspended job |
jobs |
list current jobs |
fg %1 |
foreground job number 1 |
kill %1 |
kill job number 1 |
ps |
list current processes |
kill 26152 |
kill process number 26152 |
Ex.chmod go-rwx biglist
Symbol |
Meaning |
u |
user |
g |
group |
o |
other |
a |
all |
r |
read |
w |
write (and delete) |
x |
execute (and access directory) |
+ |
add permission |
- |
take away permission |
Other useful UNIX commands
quota
All students are allocated a certain amount of disk space on the file system for their personal files, usually about 100Mb. If you go over your quota, you are given 7 days to remove excess files.
To check your current quota and how much of it you have used, type
% quota -v
df
The df command reports on the space left on the file system. For example, to find out how much space is left on the fileserver, type
% df .
du
The du command outputs the number of kilobyes used by each subdirectory. Useful if you have gone over quota and you want to find out which directory has the most files. In your home-directory, type
% du -s *
The -s flag will display only a summary (total size) and the * means all files and directories.
gzip
This reduces the size of a file, thus freeing valuable disk space. For example, type
% ls -l science.txt
and note the size of the file using ls -l . Then to compress science.txt, type
% gzip science.txt
This will compress the file and place it in a file called science.txt.gz
To see the change in size, type ls -l again.
To expand the file, use the gunzip command.
% gunzip science.txt.gz
zcat
zcat will read gzipped files without needing to uncompress them first.
% zcat science.txt.gz
If the text scrolls too fast for you, pipe the output though less .
% zcat science.txt.gz | less
file
file classifies the named files according to the type of data they contain, for example ascii (text), pictures, compressed data, etc.. To report on all files in your home directory, type
% file *
diff
This command compares the contents of two files and displays the differences. Suppose you have a file called file1 and you edit some part of it and save it as file2. To see the differences type
% diff file1 file2
Lines beginning with a < denotes file1, while lines beginning with a > denotes file2.
find
This searches through the directories for files and directories with a given name, date, size, or any other attribute you care to specify. It is a simple command but with many options - you can read the manual by typing man find.
To search for all fies with the extention .txt, starting at the current directory (.) and working through all sub-directories, then printing the name of the file to the screen, type
% find . -name "*.txt" -print
To find files over 1Mb in size, and display the result as a long listing, type
% find . -size +1M -ls
history
The C shell keeps an ordered list of all the commands that you have entered. Each command is given a number according to the order it was entered.
% history (show command history list)
If you are using the C shell, you can use the exclamation character (!) to recall commands easily.
% !! (recall last command)
% !-3 (recall third most recent command)
% !5 (recall 5th command in list)
% !grep (recall last command starting with grep)
You can increase the size of the history buffer by typing
% set history=100
The simplest way to compile a package is:
- cd to the directory containing the package's source code.
- Type ./configure to configure the package for your system.
- Type make to compile the package.
- Optionally, type make check to run any self-tests that come with the package.
- Type make install to install the programs and any data files and documentation.
- Optionally, type make clean to remove the program binaries and object files from the source code directory
The configure utility supports a wide variety of options.
The only generic options you are likely to use are the --prefix and --exec-prefix options. These options are used to specify the installation directories.
The directory named by the --prefix option will hold machine independent files such as documentation, data and configuration files.
The directory named by the --exec-prefix option, (which is normally a subdirectory of the --prefix directory), will hold machine dependent files such as executables.
Ex.
% ./configure --prefix=$HOME/units174
% make
% make check
% make install
You are now ready to run the software (assuming everything worked).
bin |
The binary executables |
info |
GNU info formatted documentation |
man |
Man pages |
share |
Shared data files |
If you list the contents of the units directory, you will see a number of subdirectories.
% strip units
To strip all the debug and line numbering information out of the binary file, use the strip command
Sometimes you can use the make command to install pre-stripped copies of all the binary files when you install the package. Instead of typing make install, simply type make install-strip
Standard UNIX variables are split into two categories, environment variables and shell variables. In broad terms, shell variables apply only to the current instance of the shell and are used to set short-term working conditions; environment variables have a farther-reaching significance, and those set at login are valid for the duration of the session. By convention, environment variables have UPPER CASE and shell variables have lower case names.
Using and setting variables
Each time you login to a UNIX host, the system looks in your home directory for initialisation files. Information in these files is used to set up your working environment. The C and TC shells uses two files called .login and .cshrc (note that both file names begin with a dot).
At login the C shell first reads .cshrc followed by .login
.login is to set conditions which will apply to the whole session and to perform actions that are relevant only at login.
.cshrc is used to set conditions and perform actions specific to the shell and to each invocation of it.
The guidelines are to set ENVIRONMENT variables in the .login file and SHELL variables in the .cshrc file.
WARNING: NEVER put commands that run graphical displays (e.g. a web browser) in your .cshrc or .login file.