红联Linux门户
Linux帮助

linux一些命令细节

发布时间:2008-04-25 16:50:55来源:红联作者:lznljyjc
1.dd命令:

[root@linux ~]# dd if="input_file" of="output_file" bs="block_size" \

count="number"

参数:
if:就是 input file ?~也可以是装置喔!
of:就是 output file 喔~也可以是装置;
bs:规划的一个 block 的大小,如果没有设定时,预设是 512 bytes

count:多少个 bs 的意思。

范例:
范例一:将 /etc/passwd 备份到 /tmp/passwd.back 当中

[root@linux ~]# dd if=/etc/passwd of=/tmp/passwd.back

3+1 records in

3+1 records out

[root@linux ~]# ll /etc/passwd /tmp/passwd.back

-rw-r--r-- 1 root root 1746 Aug 25 14:16 /etc/passwd

-rw-r--r-- 1 root root 1746 Aug 29 16:57 /tmp/passwd.back

# 仔细的看一下,我的 /etc/passwd 档案大小为 1746 bytes,因为我没有设定 bs

# 所以预设是 512 bytes 为一个单位,因此,上面那个 3+1 表示有 3 个完整的

# 512 bytes,以及未满 512 bytes 的另一个 block 的意思啦!

# 事实上,感觉好像是 cp 这个指令啦

范例二:备份 /dev/hda 的 MBR

[root@linux ~]# dd if=/dev/hda of=/tmp/mbr.back bs=512 count=1

1+0 records in

1+0 records out

# 这就得好好了解一下?,我们知道整颗硬盘的 MBR 为 512 bytes

# 就是放在硬盘的第一个 sector 啦,因此,我可以利用这个方式来将

# MBR 内的所有数据都纪录下来,真的很厉害吧!

范例三:将整个 /dev/hda1 partition 备份下来。

[root@linux ~]# dd if=/dev/hda1 of=/some/path/filename

# 这个指令很厉害啊!将整个 partition 的内容全部备份下来

# 后面接的 of 必须要不是在 /dev/hda1 的目录内啊,否则,怎么读也读不完

# 这个动作是很有效用的,如果改天你必须要完整的将整个 partition 的内容填回去

# 则可以利用 dd if=/some/file of=/dev/hda1 来将数据写入到硬盘当中。

# 如果想要整个硬盘备份的话,就类似 Norton 的 ghost 软件一般

# 由 disk 到 disk ,利用 dd 就可以啦,厉害厉害!

你可以说, tar 可以用来备份关键数据,而 dd 则可以用来备份整颗 partition 或 整颗 disk ,很不错啊~不过,如果要将数据填回到 filesystem 当中,可能需要考虑到原本的 filesystem 才能成功啊!

notrunc = no O_TRUNC
O_TRUNC意思是如果此文件存在,且为只读或只写成功打开,就将其长度截短为0,就是说如果文件有内容的话就丢弃重新打开。

seek=blocks

从输出文件开头跳过 blocks 个块后再开始复制。

2. 1> /dev/null 2>/dev/null的意义
1> /dev/null 表示将命令的标准输出重定向到 /dev/null
2>/dev/null 表示将命令的错误输出重定向到 /dev/null

1 - denotes stdout ( standard output )

2 - denotes stderr ( standard error )

/dev/null就相当与windows里的回收站,只是进去了不能再出来了。

>/dev/null 就是将标准输出和标准出错的信息屏蔽不显示

B.>/dev/null 2>&1 also can write as 1>/dev/null 2>&1
- stdout redirect to /dev/null (no stdout) ,and redirect stderr to stdout (stderr gone as well) . end up it turns both stderr and stdout off

C.a little practice may help to undstand above .

#ls /usr /nothing

#ls /usr /nothing 2>/dev/null

#ls /usr /nothing >/dev/null 2>&1
文章评论

共有 1 条评论

  1. 奶茶dsk 于 2008-04-25 17:52:56发表:

    的确是应该注意的一些细节...