遇到的Linux的命令小整理(遇到好用的继续加)

相关书籍

http://linuxcommand.org/writing_shell_scripts.php这个网址中有一本书名为

The Linux Command Line

 

http://i.linuxtoy.org/docs/guide/

 

Advanced Bash_Scripting Guide

 

网络资源

关于基本命令的使用,可以使用man gzip这样的命令来查看。

 

Linux学习方向标:

 

Linux大鹏: http://roclinux.cn/

 

使用到的

du -sh . 查看当前目录的大小

du -sh * 或者 ls -lh 查看当前目录下的子目录及其文件的大小

du -sh * | sort -nr | head -3 显示当前3个占用空间最大的文件或者目录

 

find . -size +2048 或者 find . -size 1048576c 在当前目录下查找大于1M的文件
为什么是2048?使用man find找到这个:

-size n[c]    True if the file is n blocks long (512bytes per block).
                 If n is followed by a c, the size is in bytes.

 

 

ls -a 显示当前目录下的所有文件和目录,包括隐藏的

id -a Reports user name, user ID and all the groups to which the user belongs

 

Linux Shell数据重定向(<,<<,>,>>) --> 查看上一部分的网络资源

 

command(你所需要的命令)>/dev/null 2>&1

将标准输出(1)和错误输出(2)全部重定向到/dev/null文件中,也就是将产生的所有信息丢弃

 

`date '+%Y-%m-%d %H:%M:%S'` 结果类似于这样:2012-09-25 03:30:03

echo `date +"%A, %B %d, %Y"` Wednesday, December 12, 2012

 

tail命令:使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename 会把filename里最尾部的内容显示在屏幕上,并且不但刷新(类似于实时监控的效果),使你看到最新的文件内容。

tail [+ / - num ] [参数] 文件

+num 从第num行以后开始显示。
- num 从距文件尾num行处开始显示。如果省略num参数,系统默认值为10。

 

mailx 发送接收邮件

 

基本使用: mailx -s subject email-names < mail.txt

发送附件attach (参考文章
(cat mail.txt; uuencode attach attach) | mailx -s subject email_names

example: mailx -s "Happy birthday" XXX@gmail.com < happy.txt

 

where email-names is a list of electronic mail names of people to whom you wish to send the message and subject is the subject (or title) of the message being sent, and the mail.txt represent the content you want to send.

 

如果使用uuencode,那么attach将作为附件发送 。

 

shell脚本去除文件空行

 

cat filename | sed '/^$/d' > otherfilename 将文件的空行去掉并重新写入新文件

 

有一个这样的sample.txt文件

first line
second line
third line
four line

 读取第 一行的信息:

 echo `sed '1!d' sample.txt`

  结果是:first line

 

搜索历史命令 Ctrl + R ( ctrl + r to search the history command )

更多的内容请参考: 15 Examples To Master Linux Command Line History

 

获取绝对路径的文件名

方法一:

bash-3.2$ my_path=/my/path/to/file
bash-3.2$ echo ${my_path##*/}
file

 方法二:

bash-3.2$ basename $my_path

 还有awk, sed等等都可以。

 

文件file

100
200
300
400
2344
34535
34343
500
342423
23242
8979

awk '$1 ~ /^[0-9][0-9][0-9]$/' file

结果是:

100
200
300
400
500

How do I test if a variable is a number in bash?

其中的一个答案是:

if ! [[ "$yournumber" =~ ^[0-9]+$ ]] ; then
  exec >&2; echo "error: Not a number"; exit 1
fi

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值