UNIX常用命令

UNIX常用命令

 

Unix Development Environment

l  The Basic Knowleage

l  The General Command

l  Unix Shell

l  MakeFile

 

The Basic Knowledge

    Unix/Linux systems are multi-user and multi-tasking

l  Have multiple users on the same system

l  Run multiple programs, seemingly simultaneously

l  Maintain permissions that determine which users have access to whick files and programs

l  Regulate the amount of disk space each user can use

l  Store user-specific settings in “hidden” file

 

Logging In

l When you first connect to one of the Unix computers you will see the prompt:

l Login: 

At the login : prompt, type in your username Password

Once you have typed in your username you will be prompted to type in your password

 

 

Initialization Files

When you log in the Unix/Linux login program finally, starts up a command “shell”

 

Users do not deal with the operating system directly. Most of your interaction with a Unix system takes place in a shell, a program that is run every time you log in, and displays the “$” prompt. The shell is known as command interpreter, you give it commands, and it runs them.

 

Shell

l Sh

l  Bourne Shell

l Ksh

l  Korn Shell

l Csh

l  C shell based on C language

l Bash

l  Born Again Shell

l Tcsh

l  another version of C shell

 

Using the System

Finally you are logged in! You will see a prompt like one of the following:

$/#

Just waiting fo you to type something. We will user $ to indicate the computer’s “ready” prompt.

 

Okey, let’s try a simple command.

 

ls

ls is the program to list files in a directory. Just plain ls won’t list hidden files(files whose names start with ``.’’, like .login). Now try typing:

ls -a

 

pwd(print working directory)

To find out which directory you are in, type:

pwd

This will show you the full path to your current working directory, relative to the root directory(/).

 

Rm(remove files or directories)

The command rm file will remove the specified file.

Some useful options for rm include

rm -i ( interactive mode - you will be prompted to comfirm file deletion)

rm -f ( force remove - overrides interactive mode and removes file without a prompt)

rm -r (recursive - remove the contents of directories recursively)

 

cp(copy files and directories)

To copy a file, do:

cp oldfilename newfilename

 

Or to copy a file to a new directory, while keeping the old filename do:

cp filename /directoryname/ 

 

To ensure your don’t overwrite any existing files you can use the ‘-i’ option:

cp -i oldfilename newfilename

 

This command will prompt you to confirm if there is already a file with the name newfilename.

 

mv (move files)

This command allows you to rename a file as in:

mv oldfilename newfilename

Or to move a file to a new directory:

mv filename directoryname/

As with cp, it is a good idea to use the ‘-i’ option with mv, so you don’t mistakenly overwrite your existing files.

 

The special directory

You may have noticed when you typed ls –a that there were listings dot(.) and dot-dot(..). These represent your current working directory(.) and it’s parent directory(..).

For example , typing 

cd ..

cd .

cp /vol/examples/ tutorial/science.txt .

cd ~ will always bring you back to your home directory.

cd ~/homework will bring you to the ‘homework’ directory.

 

The character * is called a wildcard and will match against none of more character(s) in a file or directory name.

For example, if you type 

ls *h

You should get a list of all files and directories starting with the letter h.

 

Similarly if you type:

ls *.cpp

 

The ? character is similar to the * character, except that ? will match exactly one character, For example:

ls ?an will match ‘man’ but not ‘stan’!

 

Display the contents of a file on the screen

clear (clear screen)

Before you start the next section, you may like to clear the terminal window of the previous commands so the output of the following commands can be clearly understood.

At the prompt, type

clear

This will clear all text and leave you with the % prompt at the top of the window.

 

cat (concatenate)

The command cat can be used to display the contents of a file on the screen. Type:

cat science.txt

 

as you can see, the file is longer than the size of the window, so it scrolls past making it unreadable.

less

The command less writes the contents of a file noto the screen a page at a time, type:

less science.txt

Press the [space-bar] if you want to see another page, type [q] if you want to quit reading.

 

head

The head command writes the first ten lines of a file to the screen.

First clear the screen then type

head science.txt

 

tail

the tail command writes the last ten lines of a file to the screen. Clear the screen and type:

tail science.txt

 

Searching the contents of a file

Simple searching using less

Using less, you can search though a text file for a keyword(pattern). For example, to search through science.txt for the word ‘science’, type

less science.txt

Then still in less(i.e. don’t press [q] to quit), type a forward slash [/] followed by the word to search.

As you can see, less finds and highlights the keyword. Type [n] to search for the next occurrence of the word.

 

grep

It searches files for specified words or patterns. First clear the screen, then type

grep science science.txt

As you can see, grep has printed out each line containing the word science.

 

Try typing

grep Science science.txt

The grep commands is case sensitive; It distinguishes between Science and science.

 

To ignore upper/lower case distinctions, use the -i option, i.e. Type

grep -i science science.txt

 

To search for a pharse or pattern, you must enclose it in single quotes. For example to search for spinning top. Type

grep -i ‘spinning top’ science.txt

 

Some of the other options of grep are:

-v

 display those lines that do NOT match

-n

 precede each maching line with the line number

-c

 print only the total count of matched line

 

Don’t Forget , you can use more than one option at a time, for example, the number of lines without the words science of Science is:

grep -ivc science science.txt

 

 

wc (word count)

A handy little utility is the wc command, short for word count. To do a word count on science.txt, type 

wc -w science.txt

To find out how many lines the file has, type

wc -l science.txt

 

Redirecting the output

We use the > symbol to redirect the output of a command. For example, to create a file called list 1 containing a list of fruit, type

cat > list1

The type in names of some fruit. Press [Return] after each one.

Pear

Banana

Apple

^D(Control D to stop)

 

What happens is the cat command reads the standard input (the keyboard) and the > redirects the output, which normally goes to the screen, into a file called list1

 

To read the contents of the file, type

cat list1

 

The form >> appends standard ouput to a file, So to add more items to the file list1, type

cat >> list1 

Then type in names of more fruit

Peach

Grape

Orange

^D(Control D to stop)

To read the contents of the file, type

cat list1

 

Create another file called list2 containing the following fruit: orange, plum, mango,grapefruit.

 

We will now use the cat command to join (concatenate ) list1 and list2 into a new file called biglist. Type

cat list1 list2 > biglist

 

What this is doing is reading the contents of list1 and list2 in turn, then outputing the next to the file biglist

 

To read the contents of the new file, type

cat biglist

 

File system security

In your unix stuff directory, type

ls -l (l for long listing!)

 

You will see that you now get lots of details about the contents of your directory, similar to the example below.

 

Each file(and directory) has associated access rights, which may be found by typing ls -l

 

In the left-hand column is a 10 symbol string consisting of the symbols d,r,w,x,-. If d is present , it will be at the left hand end of the string, and indicates a directory: otherwise - will be the starting symbol of the string.

 

The 9 remaining symbols indicate the permissions for the user that owns the file( or directory)

 

The middle group gives the permissions for the group of people to whom the file(or directory) belongs.

 

The rightmost group gives the permissions for all others.

 

For examples

-rwxrwxrwx a file that everyone can read, write and execute (and delete).

-rw------- a file that only the owner can read and write: no-one else can read or write and no-noe has execution rights(e.g. Your mailbox file).

 

Change access right

Chmod (changing a file mode)

Only the owner of a file can use chmod to change the permissions of a file, the options of chmod are as follows:

u

user

g

group

o

other

a

all

r

read

w

write(and delete)

x

execute(and access directory)

+

add permission

-

take away permission

For example, to remove read write and execute permissions on the file biglist for the group and others, type

Chmod go-rwx biglist

 

This will leave the other permissions unaffected.

To give read and write permissions on the file biglist to all

Chmod a+rw biglist

 

 

The protection bits

u

g

o

rw+

r--

---

6

4

0

The file has mode 640. The first bits, set to r+w(4+2) in our example, specify the protection for the user who owns the files(u). The user who owns the file can read or write(which includes delete) the file.

 

The next trio of bits, set to 4, or r in our example, specify access to the file for other users in the same group as the group of the file.

 

Finally, all other users are given no access to the file.

 

Process

A process is an executing program identified by a unique PID(process identifier). To see information about your processes, with their associated PID and status, type

Ps

To kill off the process, type

Kill PID_number

And then type ps again to see if it has been removed from the list.

 

Other command

find . -name “circle.h” -print

who am i

which which

finger

date

history 3

 

Text Editor

vi

The vi editor has powerful features to aid programmers.

The vi editor has two modescommand and insert.

The command mode allows the entry of commands to manipulate text.

The insert mode puts anything typed on the keyboard into the current file.

Vi initially starts in command mode.

 

Open a file in vi by typing either of :

Vi filename(opens specified file) here are some basic vi commands:

Command mode

i, a

Enter insert mode

:w

Save the file without exiting

:q!

Quit without saving

:wq

Quit and save

:x, ZZ

Quit and save the file

dd

Delete the line where the cursor is located

x

Delete the character

Set number

Set the line number

Set nonumber

Cancel line number

ESC

Enter command mode

 

Unix Shell

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 system).

 

As an illustration of the way that the shell and the kernel work together, suppose a user types rm myfiles(which has the effect of removing the file myfile).

 

The shell searches the filestore for the file containing the program rm, and then requests the kernel, through system calls, to execute the program rm on myfile.

 

When the process rm myfile has finished running, the shell then returns the UNIX prompt % to the user, indicating that it is waiting for further commands.

 

Bash

For our first shell script, well just write a script which says Hello World.

 

Create a file(firsh.sh) as follows: firsh.sh

 

#!/bin/bash

#This is a comment!

echo Hello World # this is comment, too!

 

The first line tells Unix that file is to be executed by /bin/bash.

 

The second line begins with a special symbol:#. This marks the line as a comment, and it is ignored completely by the shell.

 

The third line runs a command : echo, with two parameters, or arguments - the first is Hello; the second is World.

 

The # symbol still marks a comment; the # and anything following it is ignored by the shell.

 

Now run chmod 755 first.sh to make the text file executable, and run ./first.sh.

 

Your screen should then look like this:

% chmod 755 first.sh

% ./first.sh

Hello World

%

 

You could even just run:

% echo Hello World

Hello World

%

 

Variable

There must be no spaces around the = sign: VAR=value works; VAR = value doesnt work.

 

var.sh

#!/bin/bash

MY_MESSAGE=hello world

Echo $MY_MESSAGE

 

Markfile

To simplify compiling your code, we will be using a Makefile to compile our code.

 

Make is a UNIX program that helps programmers efficiently build projects.

 

For example:

$make

$make clean

$make install

 

 

原文地址:https://www.cnblogs.com/zfc2201/p/4084956.html