shell 命令的一些技巧(不断更新)

1)使用sed命令快速得到一个文件第一行内容

sed -n '1'p file 这条语句可以实现这个需求,但是这个文件如果有百万条数据,这样性能上会有问题。

解决办法:
代码 sed -n '1p;1q' file
这条命令将只输出第一行,同时退出程序。

linux下经常man command是个好习惯。

2)磁盘使用率偏大,需要察看哪个文件或目录占用空间最大
du -sm * | sort -nr
du参数
-s, --summarize       display only a total for each argument
-m                    like --block-size=1M
sort参数
  -n, --numeric-sort          compare according to string numerical value
  -r, --reverse               reverse the result of comparisons
上面命令就可以看到当前目录下文件或子目录的大小了,在加个head就可以得到最大的了。

3)当前目录下大于10K的文件转移到/tmp目录下

a)for FileName in `ls -l |awk '$5>10240 {print $9}'`
do
mv $FileName /tmp
done

b)find ./ -size +10240 -exec mv {} /tmp \;

posted @ 2013-12-21 17:48  wangjunzi  阅读(316)  评论(0编辑  收藏  举报