UNIX常用命令,linux常用命令


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

 

 


linux,unix常用基本命令

登录时(当看到login:时)你必须是在系统安装时创建的用户或root超级用户.(在FreeBSD系统安装时就已经创建了root用户了 ,root用户遍历到系统的任何一个目录并且可以做任何事情,包括删除系统文件,所以一定要千万小心!)符号%和符号# 代表提示符(你的可能不一样),%表示普通用户,#表示超级用户root要退出系统(并且回到login:提示符),打命令:
# exit
对,打完命令后按回车,记住UNIX对命令是区分大小写的--也就是说,是exit,而不是EXIT.
要关闭机器,打命令:
# /sbin/shutdown -h now
要重启机器,打命令:
# /sbin/shutdown -r now
或者
# /sbin/reboot
你也可以按Ctrl-Alt-Delete来重启机器.
花少许时间练习一下.在最近发行的FreeBSD版本中这和/sbin/reboot是相等的,而且这比按reset按钮要好多了.你也不想重装东西,难道不是吗?
2.用Root权限添加用户
当你第一次运行adduser时,它也许会创建一些缺省设置.在它建议你把sh作为缺省shell的时候,你可能想把csh作为缺省shell 而不是sh.否则直接按回车接受默认值.这些默认设置保存在/etc/adduser.conf中,一个可编辑文件.
# adduser
假如你创建了一个新用户jack全名为Jack Benimble.出于安全因素,给jack一个口令(即使周围的孩子也可能敲击键盘).当它问你是否想jack成为某个组的成员时,回答w heel
Login group is ``jack''. Invite jack into other groups: wheel
这样就可以用户jack登录系统,再用su命令成为root超级用户.然后你就再也不会因为以root超级用户登录而受到责备了.
你可以在adduser中,通过按Ctrl-C退出随时退出.在创建结束时你可以批准该用户的生成或打n来取消创建该用户.你也许想创建第二个用户(jill?)这样当你编辑jack的登录文件时,就有一个热备份以免出错.一旦创建完用户,exit用exit回到login:提示符以jack登录.通常情况下,最好不要用root用户而是用普通用户完成大部分的工作.如果你已经创建了一个用户而且想使该用户能够用su命令成为root用户,你可以root登录然后编辑文件/etc/group,把jack加入第一行(wheel组),但是你首先要练习使用vi,文本编辑器--或简单些的编辑器,安装在最近发行的FreeBSD中的ee.
要删除一个用户使用rmuser命令.
修改密码 passwd命令,这个命令和linux上修改用户密码的命令是一样。输入passwd命令以后,会提示输入oldpassword,再输入新密码,确认新密码!丢人
3. 环顾四周
以普通用户登录,四处浏览一下再使用一些命令试着访问帮助资源和FreeBSD的别的信息.以下是一些命令和它们的功能:
id 告诉你你是谁!
pwd 显示你在哪个目录--当前工作目录.
ls 显示当前目录的文件.
ls -F 显示当前目录的文件.执行文件的文件名后加*,目录名后加/,符号链接后加@.
ls -l 以长格式显示文件.
ls -a 列出隐藏点文件和其它文件.如果你是root用户,无须加-a选项,点文件将自动显示.
cd 改变目录.
cd......余下全文>>
 

说说常用的UNIX命令?

UNIX系统常用命令
UNIX系统常用命令格式:
command [flags] [argument1] [argument2] ...
其中flags以-开始,多个flags可用一个-连起来,如ls -l -a 与ls -la相同。

根据命令的不同,参数分为可选的或必须的;所有的命令从标准输入接受输入,输出
结果显示在标准输出,而错误信息则显示在标准错误输出设备。可使用重定向功能对
这些设备进行重定向。

命令在正常执行结果后返回一个0值,如果命令出错可未完全完成,则返回一个
非零值(在shell中可用变量$?查看). 在shell script中可用此返回值作为控制逻辑
的一部分。

注:不同的UNIX版本的flags可能有所不同。

1、与用户相关的命令
1.1 login
(在LINUX Redhat下此命令功能与Solaris/BSD不同,执行login会退出当前任务).

login:
Password:

相关文件:
在下面的这些文件中设定shell运行时必要的路径,终端类型,其他变量或特殊程序.

$HOME/.profile (Bourne shell, sh, bash)
$HOME/.cshrc (csh, tcsh)
$HOME/.tcshrc (tcsh)
/etc/passwd文件中列出每个用户的shell
/etc/csh.cshrc
/etc/csh.login
/etc/profile (Bourne shell, bash)
/etc/login (Bourne shell, bash)

csh: /etc/csh.cshrc和$HOME/.cshrc每次执行都会读取,
而/etc/csh.login和$HOME/.login只有注册shell才执行
修改相应文件后使用 source .cshrc使能相关修改,如果修改了path则
还需使用rehash刷新可执行文件hash表。

tcsh: $HOME/.tcshrc, 没有些文件读取.cshrc

sh: /etc/profile和$HOME/.profile注册shell
bash: /etc/profile和$HOME/.bash_profile注册shell读取
.bashrc交互式非注册shell才读取。

在sh/bash下手工执行相关文件:
. /etc/profile

相关文件执行顺序
sh: /etc/profile -> $HOME/.profile
csh/tcsh: /etc/csh.cshrc -> /etc/csh.login -> $HOME/.cshrc
-> $HOME/.login

变量的设置:
sh/bash: TERM=vt100; export TERM
OR: export TERM=vt100 (bash)
csh: setenv TERM vt100

常用变量:
(1)Backspace $HOME/.profile $HOME/.cshrc
stty......余下全文>>
 

相关内容