Software Development in Unix Environment
*第一节 常用命令
本课程重点 操作命令 需要权限 i节点
提示:对本课有问题请找man (帮助文档)
命令形式:man name man -k keyword
// 空格:一次一页 回车:一次一行 b:回翻一页 f:前进一页
// q:退出 /string:迅速搜索 n:找下一个
OS
Hardware Kernel shell People
硬件 内核 命令解释器 人
Kernel
// swap space 交换空间
// daemons 服务进程
// VM 虚拟内存 = PM 内存 + SWAP 硬盘
Shell
含义: 物理解释--程序 ; 功能解释-- 命令解释器
分类: Bourne shell (sh) ; Korn shell (ksh) ; C shell (csh) ;
Bourne-again shell (bash) ; tcsh (csh的扩展)
// sh命令 = csh 转到 bsh
// PTS 尾终端号
Ctrl+D 结束终端输入
Ctrl+C 终止一个前台程序
Ctrl+Z 前台程序挂起
// bg %1 放入后台运行 fg %1 放入前台运行 stop %1 后台程序挂起
文件夹
/etc/passwd 用户名 ,UID, 主ID , 主目录, loginshell
more /etc/passwd 管理注册用户信息
例如 zhouzw:x:639:1::/usr/zhouzw:/bin/csh :分隔符 $1-$N被分隔符隔开
占位符 用户ID 组ID 用户主目录 注册shell
clear 清屏
*Clear the terminal screen
pwd 查找工作目录(按绝对路径显示的当前目录)
*Display the absolute pathname of the current directory(print working directory)
cd 改变当前目录
*Change Dirrectories
*Accept both absolute(完全) and relative(关系) pathnames
cd 看当前在那个目录
. Current directory当前目录
.. Parent directory父目录
*cd [dirctory_name]
cd /etc/home 绝对路径
cd /jerry/dir1 相对路径
cd ../.. 回到主目录
cd ~etc 进入etc主目录 (./etc = etc ./可省)
ls 列出制定目录里的内容
*Displaying a Linsting of Files and Directories 放在目录的date block里
ls -a 显示当前目录下所有文件和目录,包含隐含文件(以.开头)
ls -d 把目录像文件那样显示,不显示目录下的文件
ls -i 显示目录和文件占用的i节点号 (i-node number) 文件名和i节点的对应关系
ls -t 按时间排列
ls -l 显示当前目录下详细信息(按文件名排序) 除了文件名以外的关于这个文件的
ls -lu 按照最后访问时间排序 所有属性,以及date block的位置
ls -lt 按照最后修改日期排序
ls -F (* 可执行文件 ; .../ 目录 ; none 文本文件 ; = Socket ; @ 符号连接 ; | 管道名称)
ls -R 显示目录下所有文件,包括子目录(文件管理器中点开所有+号)
ls -ld 列出当前目录的权限
file filename 查看文件详细属性及文件关联
如: 147975 -rw-r--r-- 2 zhouzw other 13 6月27 13:47 dir3
i 节点 权限 用户 用户组 文件大小 文件修改日期 文件夹
// 数字减2表示他下面子目录的数量(2表示.和..)
文件权限
r 读(查看文件内容) ; w 写(修改文件内容) ; x 执行(运行脚本) ; - 没有权限
文件类型
如: - rw- r-- r-- t
拥有者u ;同组人g ;其他人o ;所有人a chmod +t (chmod 1777) 赋予t权限
权限 (仅限/tmp目录在777权限下增设)
touch fileneme
*Create new, empty files or update access and modification times
创建新的空文件,如果文件存在,则更新文件(在用户主目录下无写权限)
// 创建或删除目录 必须有写权限和执行权限
chmod 修改权限 (root和owner才有此权限)
*Change permissions
*Symbolic mode uses combinations of letters and symbols
// file1指文件或目录 g=group ; u=user,owner ; a=all ; o=other
// +(-) 在现有权限上增加(减少)权限 = 赋值
chmod g-r file1
chmod u+x,go+r file1
chmod a=rw file1
// g=group ;u=user owner ;o=other ;a=all ;+- 在现有权限上追加(减少)权限 ;=赋值
权限
Read 4 ; Write 2 ; Execute 1
7 rwx ; 6 rw- ; 5 r-x ; 4 r--
3 -wx ; 2 -w- ; 1 --x ; 0 ---
// rwxrwxrwt (t为粘贴位 权限为 1777 )
mkdir 创建目录
*Create directories using either an absolute or a relative pathname
*Must have the appropriate permissions to create a directory
// -p 创建新目录时,其父目录不存在时先创建父目录
// -m 创建新目录的同时制定其权限
* mkdir [-p] d_name
mkdir dir1/dir2
mkdir -p dir6/dir7/dir8
mkdir dir3 dir4 dir5 创建兄弟目录
mkdir ~/games 主目录下创建目录games
cp (copy) 文件备份 文件拷贝
// -i 当目标文件存在时,系统提示是否拷贝
// -f 当目标文件存在时,系统不给提示直接拷贝
// -r 拷贝这个目录及目录里的所有文件和子目录
*Copy files
* cp [-i] source_file destination_file
* cp [-i] source_file(s) destination_directory
cp beans apple (beans必须存在)
cp beans apple dir2
cp -i beans apple
*Copy a directory and its contents to another directory
* cp -r source_directory(s) destination_directory
cp -r dir3 dir4
cp -r dir3 dir4 /tmp
rm 删除文件(此命令为彻底删除链接) rmdir (只删空目录)
*Remove files
// -f 删除文件时不需确认
// -i 删除文件时需要确认
// -r 删除指定目录及其子目录,和包含的所有文件
* rm [-i] filename(s)
rm file1 file2 file3
rm -i file4
*Remove Directories
// -p 删除指定目录上的所有目录
* rmdir directory_name(s)
* rmdir -r[i] directory_name(s)
rmdir empty_dir
rm -r dir6
rm -ir ~/dir7
mv
*Move or rename files and directory
// -i 移动时需要确认
// -f 覆盖前永不提示
* mv [-i] source target_file
* mv [-i] source target_directory
mv nuts beans
mv /export/home/jerry/brands -
mv dir5 dir6
可以是文件,可以是目录
ln 链接命令(连接文件或目录) 给系统中已有的文件指定另一个名称
*Create alternate names or aliases for files and directories
*Two kinds of links,hard and symbolic (or soft)
// -s 符号链接(软链接) 先找源,确定源的i节点,然后链接
// -d 硬链接
* ln [-s] source_file target_file
// 根的bin是用户的bin的源文件 bin --> ./usr/bin
cat 在屏幕上显示文件,连接两个或多个文件
*Displays the contents of a text file on screen
*Is often used to join two or more files into one large file
cat filename(s)
清空一个文件的内容
cat /dev/null > ~/b.txt
cat /etc/passwd | awk -F: '{print $1 "/t" $6}' /
| sort > ~/userinfo
排序 制表符 续行必须使用/
more 在屏幕上显示文件并分屏
*Displays the contents of a text file one screen at a time
* more filename(s)
// b Move back one screen
// h Display a Help menu of more features
// q Quit and return to the shell prompt
// f Move forward one screen
// n Find next occurrence of string
Spacebar Scroll to the next screen
Return Scroll one line at a time
/string Search forward to the shell prompt
head & tail
*Displays the first or last n lines of one or more files
*Displays first or last 10 lines by default 缺省10行
*head [-n] filename(s)
*tail [-n] filename(s)
tail -30 /usr/dict/words
特殊符号
Asterisk(*)
Represents zero or more characters 代表0或者更多字符
ls d*
Question mark(?)
Represents any single character 任何与此相符的条件
ls d? ls z?.txt
Square Brackets([ ])
Match a set or range of characters to a single character position
ls [b-f]b* ls [fF]f*a?c
Semicolon(;)
Enter multiple commands on a command line 输入很多命令并同时执行
cd;ls date;cal;pwd
Angle Brackets(>,<,>>) 输入重定向
Use(>) to redirect the output of a command to a file rarher than to the screen
Use(<) to redirect the input of a command from a file rather than from the keyboard
Use(>>) to append the output of a command to an existing file
cal 10 2004>a.txt 把一个命令的输出覆盖到已存在的文件里
cal 11 2004>>a.txt 把一个命令的输出添加到已存在的文件里
cal<f1.txt>f2.txt 把f1的内容重定向到f2里去
Piping ( | ) 管道: 把一个命令的输出传给下一个命令当作输入
Takes the output of one command and passes it as input into a following command
cal 7 2003 | lp 打印 分流 追加
ls -l /etc | more
ls -l /etc | tee -a etc.lst 在查看的同时保存
VI - Full Screen Text Editor
Return Escape
底行模式 命令模式 输入模式
Invoking vi
vi filename Open or create a file
vi Open a new file to be named later
Positioning
方向调整 上/下/左/右 k / j / h / l 或者小键盘方向键
行列调整 行尾/行首/文件尾/文件首/到第20行 $ / ^ / G / 1G / 20G
翻滚调整 到屏幕底/到屏幕中/到屏幕顶/往下翻页/往回翻页 L / M / H / ctrl + F / ctrl + B
Input
i 在光标当前位置插入
a 在光标所在位置插入
o 在光标所在行下插入一空行
O 在光标所在行上插入一空行
<Esc> 退出插入状态
Deleting Text
x 删除一个字符
dw 删除当前词
3dw 删除三个词
dd 删除当前行
5dd 删除5行
:5,10d 删除第5行至第10行
Changing Text
r 替换一个字符
cw 替换一个单词
cc 替换一行
C 替换从光标至行尾
Copying and Pasting Text
yw 拷贝词
yy 拷贝行(或Y)
p 当前行下粘贴
:1,2co3 拷贝行1,行2在行3之后
:4,5m6 移动行4,行5在行6之后
Saving and Quitting Files
:w 存盘
:wq 存盘退出VI(或ZZ,或:x)
:q! 强行退出不存盘
:w newfile 存成新文件
Advanced Editing Options
- 改变大小写
J 把当前行和下一行连起来
u 废除刚才的编辑操作(undo)
:set nu 显示行代码(:set nonu)
:21 光标停在指定行
21G 光标停在指定行(G到文件尾,1G到文件头)
/串 从当前行往下查找
?串 从当前行往上查找
n 查找继续
:r file2 在光标所在位置插入另一个文件
:1,$s /旧串/新串/g 替换全文(或 :%s/旧串/新串/g )
替换 全换
Finding Things on Unix
* /usr/bin , /usr/sbin , /usr/ucb , /usr/local/bin , ...
id
*Displays the user name corresponding ro the effective user ID
* id [option(s)]
id
id root
id -a root
user 当前有那些用户登陆
*Display a compact list of users logged in
user
Finding People
*Displays information about all users currently logged on to the local system
who 登陆者详细信息
w
*The who am i command displays information about your real user ID
who am i 我的登陆信息
*The whoami command displays only the login name of the effecctive user
whoami 我的登陆名字
find 在目录下找文件
*Find files based on specific criteria, then execute a command on the matching files
*find path expression [action]
find / -name perl 精确匹配 +400 大于400个block
find .-mtime 10 -print 可省 从此往前第10天 (-10 10天内 )
find /etc -user 0 -size +400 -print 找出owner大小大于200k的文件( 1 block = 512 byte字节)
find ~ -perm 777> ~/holes 找出主目录下权限为777并把结果放入文件里
*find /export/home -type f -atime +365 -exec rm {} / 找出一年内未访问的文件并删除
权限 f 类型 访问时间 此符号为写作要求
rm 无效时可用此命令
grep 在文件里找出指定的文本,并把包含此文本串的所有行打印在屏幕上
*Searches a file for a specified text string and prints all lines contain that pattern to the screen
- case sensitive
*grep [option(s)] string filename -i 忽略大小写
_ -i lgnore case of string when searching
_ -v Search for all lines that do not match string
*grep root /etc/passwd 找root用户的注册信息
ls -la | grep -i 'sep 1' 列出最后修改日期是9月1号的文件
ls file*|xargs grep dd
在文件内容里查找,无则在目录里查找 (局部传输可用,无则整体传输)
等价于grep dd file*
wc 统计字数
*Displays a line , word , or character count of a file
*wc [options] filename(s)
_ -l Counts lines
_ -w Counts words
_ -C Counts characters
grep wang /etc/passwd | wc -l 查找wang从passwd文件中并统计其字数打印到屏幕
du 显示目录或文件大小
*Summarize disk usage
du -sk * 只显示当前目录 (s 仅显示总计 k 以1024bytes为单位)
ps 查看进程
*List the processes currently running on the system
/usr/bin/ps [-options]
-e List every process now running 每一个进程
-f Generates a full listing 全列表形式
-U uidlist Processes whose teal UID ro login names are given
*ps -ef | grep telnet
*/usr/ucb/ps -auwx
// TTY 终端号 demon 精灵进程
// grep -v grep 不包含自身 (-v 不包含)
kill
*Terminate unwanted command processes that you cannot quit in the normal way
*kill [-signal] process-id
kill 12932
kill -9 12418 //需要owner权限 -9 强制杀死进程
sleep 1001 &
pkill sleep
Job Control
*Ability to execute several jobs simultaneously
fg %n Place a job in the foreground 放入前台运行
bg %n Place a job in the background 放入后台运行
stop %n Suspend a background job 后台程序挂起
kill %n Abort the specified background job
Control-c Abort the foreground job
Control-z Suspend the foreground job
fg ctrl+Z stop bg bg stop stop
fg fg
Setup Initialization Files
Environment Variables 环境变量
常用路径 /usr/sbin ; /usr/bin ; /usr/ucb ; /usr/local/bin
Eeatures of Initialization Files
*Initialization files contain commands and variable settings that are executed when a shell is started
*Two levels:
_ System-wide - Teside in the /etc directory 更改cshrc步骤
_ User-specific - Teside in a user's home directory 进入用户主目录
$HOME 表示变量 当前用户主目录 vi .cshrc 进行编辑
source .cshrc 进行激活
Shell System-Wide (Read First) User-Specific (Read Second)
Bourne /etc/profile $HOME/.profile
C /etc/.login $HOME/.login
$HOME/.cshrc 初始化环境变量文件
$HOME/.logout
Templates of The Initialization Files
*./etc/skel/local.profile , local.login , local.cshrc
$HOME/local.profile , local.login , local.cshrc
Contents of The Initialization Files
*Set the default file creation permissions
*Set environment variables
*Set the default prompt
*Set the command path for the default command path
*Set the variable TERM for the default terminal type
*Set up custom commands
Shell Switches
ksh Invoking Korn Shell
sh Invoking Bourne Shell
csh Invoking C Shell
bash Invoking Bourne-again Shell
Environment Variables
setenv variable value 系统缺省建议
setenv OPENWINHOME /user/openwin 用户自定义(包括用户定义以及应用软件设置)
Quoting in Shells
% setenv WTO `uname-n` 当命令解释
% echo $WTO
% echo "$WTO" 变量
% echo '$WTO' 字符串
% echo /$WTO
PATH
set path=(... ... ...)
set path=($path pathname...)
% echo $PATH
% echo $path
*Which
*Display the pathname leading to an accessible command based on your search path
*which vi
*where is
*Displays all directories where desired command is located, including appropriate man page
* /usr/ucb/whereis vi
History
*Displays previously entered commands typed in the C shell to the screen
* set history=30
% history
% history 5
% !n
% !!
Aliases 别名
*Substitute a short command for a long one
*Create a single command for a series of commands
*Create alternate forms of existing commands
*Change a command set
*alias alias-name value
% alias h history
% alias c clear
% alias home 'cd;ls' 特殊字符 空格需加单引号
% alias ls 'ls -l'
% alias copy 'cp -i'
% alias
% unalias copy
Custom Prompts
set prompt= value
set prompt="`hostname`: $cwd %"
An .cshrc Example 变量 系统变量 应用软件变量
# Starting .cshrc ...
umask 022
setenv CC /usr/local/bin/gcc
setenv CCC /usr/local/bin/g++
setenv ORACLE_BASE /tarenaora
setenv ORACLE_HOME ${ORACLE_BASE}/software/9.0.1
setenv ORACLE_SID TARENADB 我要连接的数据库
setenv JAVA_HOME /usr/j2se MAN要用的命令的搜索路径
setenv MANPATH /usr/share/man:/usr/man:/usr/share/catman:/usr/catman
setenv LD_LIBRARY_PATH /usr/lib:/usr/dt/lib:${ORACLE_HOME}/lib
setenv EDITOR vi 动态链接库
set path=(/usr/j2se/bin /usr/bin /usr/local/bin /usr/ucb /usr/ccs/bin /etc.)
set history=100
set prompt="`hostname`!%"
alias || "ls -l | more"
alisa make /usr/bin/make
Shell Scripting
*Script 1:
vi a.sh
#!/bin/sh
pwd
cal
date
chmod +x a.sh
*Script 2: lcf ( lowercase filenames)
#!/bin/sh
# lowercase any filenames with uppercase chars
for oldname in $* 从这里取值 $*是一个位置参数
do
if [ -f $oldname ]
then
newname=`echo $oldname | tr "[A-Z]" "[a-z]"
if [ $oldname != $newname ]
then
mv $oldname $newname
fi
fi
done
# 请注意空格
* crontab -l
* crontab -e 做这个之前先在.cshrc设置中加上editor
- setenv EDITOR vi 调度vi
* crontab -r
* mailx
分钟 小时 日期 月份 星期
0 2 *** a.sh 调度 (在某个特定时间打开某个文件或程序)
13 5 13 * 5 b.sh
0,30 8-17 ** 1-5 c.sh 每隔30分钟
***** d.sh
两种shell设置变量的方法
csh
.cshrc
setenv 变量名 变量值
source .cshrc
这两个步骤等同
bash
.bash-profile
LANG=C export LANG
source .bash-profile
/usr/ucb/ps -auwx | more
输出结果
user pid %cpu %mem sz rss tt s start time command
(cpu 内存)-利用率 状态