linux的mail命令含义,Linux下的mail命令

The Linux command line can be very powerful once you know how to

use it. You can parse data, monitor processes, automate backups and

do a lot of other useful and cool things using it. There often

comes a need to generate a report and mail it out. It could be as

simple a requirement as a notification that the day’s backup went

through fine, or did not. I’ll help you get started with sending

mails from the Linux command line and in shell

scripts. We will also cover sending attachments from the command

line. We will begin with the “mail” command.

MAIL

First run a quick test to make sure the “sendmail” application is

installed and working correctly. Execute the following command,

replacing “[email protected]” with your e-mail address.

# mail

-s “Hello world” [email protected]

Hit the return key and you will come to a new line. Enter the text

“This is a test from my server”. Follow up the text by hitting the

return key again. Then hit the key combination

of Control+D to

continue. The command prompt will ask you if you want to mark a

copy of the mail to any other address,

hit Control+D again.

Check your mailbox. This command will send out a mail to the email

id mentioned with the subject, “Hello world”.

To add content to the body of the mail while running the command

you can use the following options. If you want to add text on your

own:

# echo

“This will go into the body of the mail.” | mail -s “Hello world”

[email protected]

And if you want mail to read the content from a file:

# mail

-s “Hello world” [email protected] <

/home/calvin/application.log

Some other useful options in the mail command are:

-s

subject (The subject of the

mail)

-c

email-address (Mark a copy to this

“email-address”, or CC)

-b

email-address (Mark a blind carbon copy

to this “email-address”, or BCC)

Here’s how you might use these options:

# echo

“Welcome to the world of Calvin n Hobbes” | mail -s “Hello world”

[email protected] -c [email protected] -b

[email protected]

MUTT

One of major drawbacks of using the mail command is that it does

not support the sending of attachments. mutt, on the other hand,

does support it. I’ve found this feature particularly useful for

scripts that generate non-textual reports or backups which are

relatively small in size which I’d like to backup elsewhere. Of

course, mutt allows you to do a lot more than just send

attachments. It is a much more complete command line mail client

than the “mail” command. Right now we’ll just explore the basic

stuff we might need often. Here’s how you would attach a file to a

mail:

# echo

“Sending an attachment.” | mutt -a backup.zip -s “attachment”

[email protected]

This command will send a mail to [email protected] with the subject

(-s) “attachment”, the body text “Sending an attachment.”,

containing the attachment (-a) backup.zip. Like with the mail

command you can use the “-c” option to mark a copy to another mail

id.

SENDING MAIL FROM A SHELL SCRIPT

Now, with the basics covered you can send mails from your shell

scripts. Here’s a simple shell script that gives you a reading of

the usage of space on your partitions and mails the data to

you.

#!/bin/bash

df -h | mail -s “disk space report” [email protected]

Save these lines in a file on your Linux server and run it. You

should receive a mail containing the results of the command. If,

however, you need to send more data than just this you will need to

write the data to a text file and enter it into the mail body while

composing the mail. Here’s and example of a shell script that gets

the disk usage as well as the memory usage, writes the data into a

temporary file, and then enters it all into the body of the mail

being sent out:

#!/bin/bash

df -h > /tmp/mail_report.log

free -m >> /tmp/mail_report.log

mail -s “disk and RAM report” [email protected] <

/tmp/mail_report.log

Now here’s a more complicated problem. You have to take a backup of

a few files and mail then out. First the directory to be mailed out

is archived. Then it is sent as an email attachment using mutt.

Here’s a script to do just that:

#!/bin/bash

tar -zcf /tmp/backup.tar.gz /home/calvin/files

echo | mutt -a /tmp/backup.tar.gz -s “daily backup of data”

[email protected]

The echo at the start of the last line adds a blank into the body

of the mail being set out.

This should get you started with sending mails form the Linux

command line and from shell scripts. Read up the “man page” for

both mail and mutt for more options (enter the commands “man mail”

and “man mutt” for the full manual on each).

splite分割文件:

[marco.chan@linuxhobby backup]$ split -b 4000k

skype_backup.tar.gz

skype_backup_20090626.tar.gz.

–verbose

creating file `skype_backup_20090626.tar.gz.aa’

creating file `skype_backup_20090626.tar.gz.ab’

creating file `skype_backup_20090626.tar.gz.ac’

creating file `skype_backup_20090626.tar.gz.ad’

[marco.chan@linuxhobby backup]$ ls

skype_backup skype_backup_20090626.tar.gz.ac

skype_backup_20090626.tar.gz.aa

skype_backup_20090626.tar.gz.ad

skype_backup_20090626.tar_20090626.gz.ab

skype_backup.tar.gz

-b 指定分割大小,例如k,m等单位。

–verbose 输出分割文件信息,可选。

skype_backup.tar.gz

被分割的文件。

skype_backup_20090626.tar.gz.

分割后的文件名(注意gz后面的”.”),如果不指定的话,默认是xaa,xab,xac。

现在通过tar,split,已经将skype_backup目录文件打包、分割为每个最大为4M大小的压缩包了。

以上是分步执行,如果用管道”|”,将tar,split写成一个执行语句:

使用管道“|”合并两个指令:

[marco.chan@linuxhobby backup]$ tar -czvp

-f – skype_backup |split -b 4000k –

skype_backup_20090626.tar.gz.

–verbose

[marco.chan@linuxhobby backup]$ ls

skype_backup

skype_backup_20090626.tar.gz.aa

skype_backup_20090626.tar.gz.ab

skype_backup_20090626.tar.gz.ac

skype_backup_20090626.tar.gz.ad

注意一下指令中的两个“-”,如果分开执行,就不用”-”。为什么有这个”-”?

man tar

-f, –file [HOSTNAME:]F

Use archive file or device F (default “-”, meaning stdin/stdout).

Note that “/dev/stdout” is not equivalent to “-”.Using

“/dev/stdout” explicitly can lead to corrupted archive, especially

when coupled with “-v”.

如何解压分割文件?

把文件分割了几个包,当然也要能合并解压了,合并文件当然用cat了,cat不仅仅是用来处理合并文本文件的,也可以用来合并压缩文件。

cat合并分割的文件:

[marco.chan@linuxhobby backup]$ cat

skype_backup_20090626.tar.gz.a*

>skype_backup_cat.tar.gz

将之前分割的压缩包合并成skype_backup_cat.tar.gz,然后可以通过tar -zxvf来解压skype_backup_cat.tar.gz,通过管道”|”可写成:

使用管道“|”合并两个指令:

[marco.chan@linuxhobby backup]$ cat

skype_backup_20090626.tar.gz.a*

|tar -zxv

关于更多的tar、split、cat指令应用,建议通过man查询。

在Linux下使用tar来将文件打包并压缩是很通常的用法了。

可是Linux的文件系统对文件大小有限制,也就是说一个文件最大不能超过2G,如果压缩包的的内容很大,最后

的结果就会超过2G,又或者压缩包希望通过光盘来进行备份,而每张光盘的容量只有700M,那么该如何存储呢?

解决办法:

将最后的压 缩包按照指定大小进行分割,需要用到split命令。

举例说明:

要将目录logs打包压缩并分割成多个1M的文件,命令:

代码示例:

tar cjf - logs/ |split -b 1m - logs.tar.bz2.

完成后会产生下列文件:

代码示例:

logs.tar.bz2.aa, logs.tar.bz2.ab, logs.tar.bz2.ac

解压时只要执行命令:

代码示例:

catlogs.tar.bz2.a*

| tar xj

再举例:

要将文件test.pdf分包压缩成500

bytes的文件:

代码示例:

tar czf - test.pdf | split -b 500 - test.tar.gz

最后要提醒但是那两个"-"不要漏了,那是tar的ouput和split的input的参数。

这里对命令做一些讲解。

压缩和分割命令行中红色部分的为命令对应的I/O文件名参数,其中-表示标准输入或输出。那么结合上面的例子,tar命令表示的意思就是将logs目录压缩,生成的结果直接输送到标准输出上;而split命令表示从标准输入获得文件内容进行分割,结果文件前缀为logs.tar.bz2.。二者再通过管道将各自的标准输入输出对接起来。

这里再解释一下split命令的参数含义:

-b SIZE 指定每个文件的大小,其单位可以为b(512字节)、k(1K)、m(1M)

-d 使用数字而不是字母作为后缀名

-a X 指定后缀名的长度,默认为2位

命令可以变化为:

代码示例:

tar cjf - logs/ |split -b 1m -d -a 1- logs.tar.bz2.

你可能感兴趣的:(linux的mail命令含义)