您的位置:首页 > 运维架构 > Shell

shell脚本使用笔录(ing)

2012-12-11 21:44 155 查看

常用命令

#基本操作
cd, ls, pwd, mv, cp, date

tr, rm

xargs

time
set, env
history
nohup
&

 

#文件查看及编辑
cat, less, more, head, tail
vi, gedit
sed, awk
sort, uniq
grep
find
wc
tar, zip

 

#用户及权限管理
touch, chmod, chown, chgrp
useradd, userdel, passwd
su
id, w, who, whoami

 

#查看 系统资源/使用情况 命令
top, free
fdisk, df, du
uname, hostname, ifconfig
lsof
service, chkconfig
ulimit
netstat, ps
kill

 

#网络相关
ping, telnet
wget
ssh
rsync, scp, lrzsz

 

参考资料:

1 http://zhanjia.iteye.com/blog/1797788

2 UNIX高手的10个习惯:
http://www.ibm.com/developerworks/cn/aix/library/au-badunixhabits.html
还有续篇,也附上连接:
https://www.ibm.com/developerworks/cn/aix/library/au-unixtips/

 

使用案例

cat /proc/meminfo
more /etc/redhat-release

 

查看端口是否被使用 # netstat -nlp | grep 端口号   如查找tomcat进程一般可以这么写 # ps -ef | grep tomcat   查看进程,根据第二列按默认进行排序 # ps -aux | sort -k 2   查看进程,按内存使用率升序排序 # ps -aux | sort -nk 4   置空文件(如果该文件正在使用,不能直接删除!如果直接删除,会导致新的数据丢失) # > file.txt   按修改时间降序排序 # ls -lt # ls -lrt   切换到当前用户的主/HOME目录 # cd ~   返回最近访问的目录,这个经常用得到,不需要自己输入目录 # cd -


lsof abc.txt 显示开启文件abc.txt的进程
lsof -i :22 知道22端口现在运行什么程序
lsof -c abc 显示abc进程现在打开的文件

 

参考:

1 lsof的使用(http://linux.ccidnet.com/art/305/20070829/1194715_1.html)

 

具体场景

1 ## 不以数字或字母开头的行

grep -n -G ^[^0-9a-z] file

 

2 window的文件转成unix的文件格式

#!/bin/sh

file=$1
tmp=$1.tmp
cat $file | tr -d '\r' > $tmp
rm $file
mv $tmp $file

 

 还有更好的方式来转换:

unix2dos

dos2unix

cat -A

 

http://space.itpub.net/?uid-8107207-action-viewspace-itemid-474791

 

3 linux定时任务注意点:

    原来就知道在crontab文件中不能直接写shell的相对PATH的路径,要使用绝对路径。没想到在被调用的shell中的命令也需要使用绝对路径!!最好就是把环境变量在脚本里再定义一遍.

* * * * * jps >> jps.log

 

* * * * * /home/hadoop/java7/bin/jps >> jps.ab.log

如果是一段很长的代码的话,最好在shell脚本开始处重新定义JAVA_HOME, PATH。

 

4 把一段很长的字符处理成java的一个字符串

想在java代码里面直接拷贝一段很长的字符串,但是java不像groovy那样可以多行。处理如下:

cat test.txt | sed -e "s/\"/\\\\&/g" -e "s/.*/\"&\"+/"

 

5 递归列出目录下的所有文件(包括目录)

在cygwin下使用rsync同步时,使用windows的软件,不能打开同步的文件(没有权限)。可以通过chmod来修改,但是文件很多,使用下面的命令一次进行修改:

ls -R -A  -1 | awk '/^\.[^:]*:/{sub(":$","");current=$0;}{if($0!="" && $0!=current){print current "/" $0;}}' | while read s; do chmod 777 "$s"; done

 

-->有一种超级简单的方式来打印全路径:

find .

 要执行命令的话,有两种方式:

find /tmp -name core -type f -print | xargs /bin/rm -f

 

OR

find . -type f -exec file '{}' \;

 

   

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: