Shell Tips

#1 查找文件修改时间为某一段时间的文件:

touch -c 0901000007 file.start
touch file.end

find /local/Tomcat55/0/cluster/machine0/webapps/Pcclass -newer file.start ! -newer file.end > app_modified_since01Sep.txt -ls

find /jpmorgan/apps/pcclass -newer file.start ! -newer file.end > pcc_modified_since01Sep.txt -ls

#2 find records which dont contain “up to date” in the log.txt file to compile.log
cat log.txt | grep -v ‘up to date’ >> compile.log

#3 获取文件总行数
wc -l /jpmorgan/apps/pcclass/temp/1.txt | cut -c1-8
获取文件夹下文件个数:
ls /etc | wc -l

(see:http://www.scit.wlv.ac.uk/cgi-bin/mansec?1+wc)

#4 文本替换
echo “a test” | tr t p
echo “a test” | tr aest 1234
echo “a test” | tr -d t
echo “a test” | tr ‘[:lower:]’ ‘[:upper:]’

#5 sed (Stream EDitor) 正则替换(pattern replacement)
(see:http://www.scit.wlv.ac.uk/cgi-bin/mansec?1+sed)
replace “one” with “another”, one occurrence:
echo “Replace one word” | sed -e s/one/another/

replace all the letter in “aeiouy” with “_” – g:
echo “This is a test” | sed -e s/[aeiouy]/_/g
Th_s _s _ t_st

#6 替换文件中的某个字符串
replace all “a” in 1.txt with “b”:
way 1:echo `sed ‘s/a/b/g’ 1.txt` > 2.txt
!!!-this will lose the line separator
way 2: use awk, better way, more flexible and programatical
awk ‘{
if($0==”aaa”) {print “bbb”}
else print $0
}’ 1.txt > 2.txt

#7 tail

分析日志时,把最后1000行输出到另一个文件.使用tail命令
tail -1000 Pcclassind.log > /tmp/dhanush.log
在控制台输出日志文件(文件更新会随时显示到控制台)
tail -f /local/Tomcat55/1/cluster/machine3/logs/catalina.out

#8 date time
Year=`date | cut -f 6 -d ” “`
month=`date | cut -f 2 -d ” “`
day=`date | cut -f 3 -d ” ” `

#9 查看文件夹所占空间
du -s foldername/
(http://www.computerhope.com/unix/udu.htm )

#10查看所有java进程
ps -ef | grep java

Comments are closed.