Chinaunix

标题: 求助,tar工具中的C参数的作用是什么? [打印本页]

作者: donggua0713    时间: 2008-01-28 12:08
标题: 求助,tar工具中的C参数的作用是什么?
如题,我不太清楚tar中,C这个参数是干什么的,自己做测试半天,也没看出到底是干什么的,到是用C了以后,少打包一个文件,很奇怪啊,网上找了一下,还是没什么帮助,希望大家帮帮我。

大写的C啊,不是小写的c
作者: sakulagi    时间: 2008-01-28 12:46
不是指定解压的目录的么?
作者: donggua0713    时间: 2008-01-28 12:51
解压这一块我能理解,就是压缩时,这个C起什么作用?一直没看懂
作者: east2008    时间: 2008-01-28 15:58
路过,学习了..感谢LZ..
作者: east2008    时间: 2008-01-28 15:59
路过,学习了..感谢LZ..
作者: east2008    时间: 2008-01-28 16:00
路过,学习了..感谢LZ..
作者: east2008    时间: 2008-01-28 16:01
路过,学习了..感谢LZ..
作者: swordfish.cn    时间: 2008-01-28 19:52
man tar      
-C, --directory DIR
change to directory DIR

楼主倒说说你是怎么操作的啊。
作者: 7717060    时间: 2008-01-28 21:42
指定解压的目录用的!!!!!!
作者: donggua0713    时间: 2008-01-29 08:27
解压这一块我能理解,压缩我就不理解了,我是这样操作的(freebsd 6.2):
tar -cvf /dev/sa0 -C /home/*
还有
tar -cvf /dev/sa0 /home/*
除了-C少打包一个文件外,没发现有什么不同。

[ 本帖最后由 donggua0713 于 2008-1-29 11:37 编辑 ]
作者: mcumsigscr    时间: 2008-01-29 08:58
原帖由 east2008 于 2008-1-28 15:58 发表
路过,学习了..感谢LZ..



east2008  请不要重复发帖。
作者: guffaw    时间: 2008-01-29 13:40
在man手册中是这样定义的:
-C, --directory DIR.
change to directory DIR
意思是说把该文件解压到选定的DIR目录中。
作者: donggua0713    时间: 2008-01-29 13:52
解压我明白,压缩时C是干什么的呢?
作者: swordfish.cn    时间: 2008-01-29 14:53
原帖由 donggua0713 于 2008-1-29 08:27 发表
解压这一块我能理解,压缩我就不理解了,我是这样操作的(freebsd 6.2):
tar -cvf /dev/sa0 -C /home/*
还有
tar -cvf /dev/sa0 /home/*
除了-C少打包一个文件外,没发现有什么不同。


-C 就是把工作目录转到你指定的目录去。
不能打包的那个估计是regular file吧,因为tar不能chdir过去。
作者: donggua0713    时间: 2008-01-29 15:07
14楼,
-C 就是把工作目录转到你指定的目录去。

我不加-C,也有这个效果啊。

不能打包的哪个文件是FREEBSD的ISO文件,DISK 1,和DISK 2在权限等方面没有任何不同,DISK2可以打包,1就不打包,奇怪

[ 本帖最后由 donggua0713 于 2008-1-29 15:11 编辑 ]
作者: swordfish.cn    时间: 2008-01-29 15:43
看来你不知道“工作目录”这个概念呢。
假设/home/123里面有一个1.c文件。
现在你的pwd是/root,请试用
tar -cvf test.tar -C /home/123 1.c
tar -cvf test.tar 1.c
tar -cvf test.tar /home/123/*
作者: swordfish.cn    时间: 2008-01-29 15:44
而且我觉得很奇怪的是,你要打包生成一个/dev/sa0吗?
作者: donggua0713    时间: 2008-01-29 15:49
我是要打包到磁带里面去,/dev/sa0是磁带机,我是在freebsd下啊
我做上面的命令时,全都是在/home目录下的,加不加-C,感觉没什么不同,就是加-C时,少一个文件,怎么解释呢?

而且我用的是root,不存在说那个文件不能读取啊

[ 本帖最后由 donggua0713 于 2008-1-29 15:53 编辑 ]
作者: swordfish.cn    时间: 2008-01-29 16:18
什么出错信息都没有吗?
作者: donggua0713    时间: 2008-01-29 16:26
如下,

[root@freebsd /home]# tar -cvf test3.test -C /home/donggua/*
tar: Removing leading '/' from member names

[ 本帖最后由 donggua0713 于 2008-1-29 16:30 编辑 ]
作者: swordfish.cn    时间: 2008-01-29 16:37
ls -l /home/donggua
输出的是什么呢?
假如第一个是目录,那很有可能*被解释成了那个目录了。所以没有出错。
作者: donggua0713    时间: 2008-01-29 16:41
[root@freebsd /home/donggua]# ls -l
total 1242816
lrwxr-xr-x  1 donggua  donggua         36 Jan 29 14:42 .DCOPserver_freebsd_:0 -> /home/donggua/.DCOPserver_freebsd__0
-rw-r--r--  1 donggua  donggua         54 Jan 29 14:42 .DCOPserver_freebsd__0
-rw-------  1 donggua  donggua        191 Jan 29 14:42 .ICEauthority
-rw-------  1 donggua  donggua        215 Jan 29 14:38 .Xauthority
-rw-------  1 donggua  donggua        173 Jan 29 15:55 .bash_history
-rw-rw-rw-  1 donggua  donggua        767 Jan 24 03:40 .cshrc
-rw-------  1 donggua  donggua         26 Jan 29 14:38 .dmrc
drwx------  4 donggua  donggua        512 Jan 29 14:42 .kde
-rw-------  1 donggua  donggua        154 Jan 29 14:41 .kderc
drwx------  3 donggua  donggua        512 Jan 29 14:42 .local
-rw-rw-rw-  1 donggua  donggua        248 Jan 24 03:40 .login
-rw-rw-rw-  1 donggua  donggua        158 Jan 24 03:40 .login_conf
-rw--w--w-  1 donggua  donggua        373 Jan 24 03:40 .mail_aliases
-rw-rw-rw-  1 donggua  donggua        331 Jan 24 03:40 .mailrc
drwxr-xr-x  3 donggua  donggua        512 Jan 29 14:42 .mcop
-rw-rw-rw-  1 donggua  donggua        797 Jan 24 03:40 .profile
drwxr-xr-x  2 donggua  donggua        512 Jan 29 14:42 .qt
-rw--w--w-  1 donggua  donggua        276 Jan 24 03:40 .rhosts
-rw-rw-rw-  1 donggua  donggua        975 Jan 24 03:40 .shrc
drwxr-xr-x  2 donggua  donggua        512 Jan 29 14:57 .vnc
-rw-------  1 donggua  donggua      18546 Jan 29 16:12 .xsession-errors
-rw-r--r--  1 root     donggua       1864 Jan 29 16:40 1.txt
-rwxr--r--  1 nobody   donggua  601229312 Jan 18 12:56 6.2-RELEASE-i386-disc1.iso
-rwxr--r--  1 nobody   donggua  670107648 Jan 18 14:44 6.2-RELEASE-i386-disc2.iso
drwx------  2 donggua  donggua        512 Jan 29 14:42 Desktop
drwxr-xr-x  4 502      502            512 May 16  2006 vnc-4_1_2-unixsrc
-rwxr--r--  1 nobody   donggua     537713 Jan 29 14:50 vnc-4_1_2-unixsrc.tar.gz
作者: swordfish.cn    时间: 2008-01-29 16:44
改天找个FreeBSD测试一下,现在只有Linux。
作者: donggua0713    时间: 2008-01-29 16:50
你的linux不会这样吗?
作者: donggua0713    时间: 2008-01-29 16:51
-C在压缩的时候,到底是什么作用?
作者: swordfish.cn    时间: 2008-01-29 16:56
原帖由 donggua0713 于 2008-1-29 16:50 发表
你的linux不会这样吗?


我用的Fedora 8,成功与否就在于*是否被解释成一个目录了。
其实-C就是改变工作目录,我已经说得很清楚了。
作者: donggua0713    时间: 2008-01-29 16:58
[root@freebsd /home]# tar -cvf test4.test -C /home/donggua/
tar: no files or directories specified

不加就报错

[root@freebsd /home]# tar -cvf test4.test /home/donggua/
tar: Removing leading '/' from member names


不加*是一样的哦
作者: donggua0713    时间: 2008-01-29 17:00
-C是改变目录,但压缩的时候,不用-C也可以达到同样的效果啊
作者: donggua0713    时间: 2008-01-29 17:02
还是不明白,有谁明白的?写几个命令,让我看看到底有什么用?谢谢了
作者: anakinsun    时间: 2008-01-29 17:27
asdadfsdfasdff
作者: swordfish.cn    时间: 2008-01-29 17:48
原帖由 donggua0713 于 2008-1-29 16:58 发表
[root@freebsd /home]# tar -cvf test4.test -C /home/donggua/
tar: no files or directories specified

不加就报错

[root@freebsd /home]# tar -cvf test4.test /home/donggua/
tar: Removing leadin ...


你的第一个加了-C之后,/home/donggua就是-C的参数了。
第二个是直接把/home/donggua作为打包的目标。

[ 本帖最后由 swordfish.cn 于 2008-1-29 17:58 编辑 ]
作者: swordfish.cn    时间: 2008-01-29 18:06
最后,还是送你一些手册上的东西
http://www.gnu.org/software/tar/manual/tar.html

‘-C dir’
    When this option is specified, tar will change its current directory to dir before performing any operations. When this option is used during archive creation, it is order sensitive. See section Changing the Working Directory.


  1. 6.10.1 Changing the Working Directory

  2. To change the working directory in the middle of a list of file names, either on the command line or in a file specified using ‘--files-from’ (‘-T’), use ‘--directory’ (‘-C’). This will change the working directory to the specified directory after that point in the list.

  3. ‘--directory=directory’
  4. ‘-C directory’

  5.     Changes the working directory in the middle of a command line.

  6. For example,
  7.        

  8. $ tar -c -f jams.tar grape prune -C food cherry

  9. will place the files ‘grape’ and ‘prune’ from the current directory into the archive ‘jams.tar’, followed by the file ‘cherry’ from the directory ‘food’. This option is especially useful when you have several widely separated files that you want to store in the same archive.

  10. Note that the file ‘cherry’ is recorded in the archive under the precise name ‘cherry’, not ‘food/cherry’. Thus, the archive will contain three files that all appear to have come from the same directory; if the archive is extracted with plain ‘tar --extract’, all three files will be written in the current directory.

  11. Contrast this with the command,
  12.        

  13. $ tar -c -f jams.tar grape prune -C food red/cherry

  14. which records the third file in the archive under the name ‘red/cherry’ so that, if the archive is extracted using ‘tar --extract’, the third file will be written in a subdirectory named ‘orange-colored’.

  15. You can use the ‘--directory’ option to make the archive independent of the original name of the directory holding the files. The following command places the files ‘/etc/passwd’, ‘/etc/hosts’, and ‘/lib/libc.a’ into the archive ‘foo.tar’:
  16.        

  17. $ tar -c -f foo.tar -C /etc passwd hosts -C /lib libc.a

  18. However, the names of the archive members will be exactly what they were on the command line: ‘passwd’, ‘hosts’, and ‘libc.a’. They will not appear to be related by file name to the original directories where those files were located.

  19. Note that ‘--directory’ options are interpreted consecutively. If ‘--directory’ specifies a relative file name, it is interpreted relative to the then current directory, which might not be the same as the original current working directory of tar, due to a previous ‘--directory’ option.

  20. When using ‘--files-from’ (see section Reading Names from a File), you can put various tar options (including ‘-C’) in the file list. Notice, however, that in this case the option and its argument may not be separated by whitespace. If you use short option, its argument must either follow the option letter immediately, without any intervening whitespace, or occupy the next line. Otherwise, if you use long option, separate its argument by an equal sign.

  21. For instance, the file list for the above example will be:
  22.        

  23. -C/etc
  24. passwd
  25. hosts
  26. --directory=/lib
  27. libc.a

  28. To use it, you would invoke tar as follows:
  29.        

  30. $ tar -c -f foo.tar --files-from list

  31. The interpretation of ‘--directory’ is disabled by ‘--null’ option.
复制代码

作者: swordfish.cn    时间: 2008-01-29 18:09
唉,说到底,还不就是改变工作目录么。
不过这个是我见过的第一个参数顺序敏感的gnu工具。
作者: lrz    时间: 2008-01-29 20:18
1、备份/home/abc目录:
tar  cjvf  abc.tar.bz2  -C  /home   abc
注意写法是/home abc而不是/home/abc
当用
tar  -xjvf  abc.tar.bz2
解开会在当前目录生成abc

2、用
tar  cjvf  abc.tar.bz2  /home/abc
命令备份时,用
tar  -xjvf  abc.tar.bz2
解开时在当前目录生成的好象是home/abc
不知有否记错。自己实践。
作者: donggua0713    时间: 2008-01-30 08:29


[ 本帖最后由 donggua0713 于 2008-1-30 09:27 编辑 ]
作者: dingning239    时间: 2008-01-30 08:37
原帖由 swordfish.cn 于 2008-1-28 19:52 发表
man tar      
-C, --directory DIR
change to directory DIR

楼主倒说说你是怎么操作的啊。





是啊,通过man完全可以看出来参数的作用啊
作者: donggua0713    时间: 2008-01-30 08:50
首先在此谢谢楼上各位,尤其要谢谢swordfish.cn  的那篇文档,在此总结一下我的理解,请大家指正:

-C的作用,改变工作目录,怎么理解?如下:
压缩时:
      假如有这样一种情况,有1和2两个文件需要压到同一个包里面,但1和2不在一个目录,此时,-C派上用场了,比如1在/home,2在/usr,则我首先可以跑到/home目录,执行下面命令tar -cvf 12.tar 1 -C /usr 2  如此,1和2 就给压到一个包里面了。

解压时:
      拿上面那个12.tar来说,此时我在/home目录里面,但我想把12.tar解压出来的东西直接放到/usr目录里面,执行下面命令 tar -xvf 12.tar -C /usr   如此,1和2就给解压到/usr里面了.


不过还是有点不清楚的地方,tar -cvf 12.tar 1 -C /usr 2   这个命令,用tar -cvf 12.tar 1 /usr/2也可以做到相同的效果啊,

[ 本帖最后由 donggua0713 于 2008-1-30 09:31 编辑 ]
作者: donggua0713    时间: 2008-01-30 08:58
还想问一下大家,比如我要找mt这个命令的用法,除了man外,有没有某个网站有更详细的介绍呢?
作者: swordfish.cn    时间: 2008-01-30 11:40
tar -cvf 12.tar 1 -C /usr 2   这个命令,用tar -cvf 12.tar 1 /usr/2也可以做到相同的效果啊

这个的区别,文档里面已经说得很清楚了,看仔细点。
两种不同的方式打包,2在包里面的路径是不一样的。
作者: swordfish.cn    时间: 2008-01-30 11:42
原帖由 donggua0713 于 2008-1-30 08:58 发表
还想问一下大家,比如我要找mt这个命令的用法,除了man外,有没有某个网站有更详细的介绍呢?


man mt
info mt(假如有的话)
google mt

作者: coolf4ce    时间: 2008-01-31 12:08
[root@localhost oracle]# ls
bak.bash_profile  kmdb_init         oraInventory  user.tar.gz
jre               kmdb_init.tar.gz  oui           xx
[root@localhost oracle]# tar -xzvf user.tar.gz xx/
tar: xx: Not found in archive
tar: Error exit delayed from previous errors

[root@localhost oracle]# tar -xzvf user.tar.gz -C xx/
home/user/
home/user/.kde/
home/user/.kde/Autostart/
.
.
.
home/user/.bash_history
home/user/.viminfo
home/user/kmreboot.sh
[root@localhost oracle]#

解的时候不加C指定不了路径

[ 本帖最后由 coolf4ce 于 2008-1-31 12:10 编辑 ]
作者: cliffordbear    时间: 2008-01-31 12:53
没搞明白楼主想干嘛。
作者: 鬼才小科    时间: 2008-02-01 12:19
大C的用法是
-C<目的目录>或--directory=<目的目录>   
切换到指定的目录。
作者: lrz    时间: 2008-02-01 14:54
原帖由 donggua0713 于 2008-1-30 08:50 发表
首先在此谢谢楼上各位,尤其要谢谢swordfish.cn  的那篇文档,在此总结一下我的理解,请大家指正:

-C的作用,改变工作目录,怎么理解?如下:
压缩时:
      假如有这样一种情况,有1和2两个文件需要压到 ...


感觉你理解的还是有点问题(因为我和理解的不一样?),我理解的是:
1、tar  cjvf  abc.tar.bz2  -C  /home   abc
这很好解释,跟windows下用右键菜单的rar压缩文件没什么分别,只简单地把该文件压缩了。所以解压缩的时候若不用-C指定路径的话,就只在当前目录下生成abc。


2、tar  cjvf  abc.tar.bz2  /home/abc
tar会在某个临时的地方建个home目录,再把abc复制到这个它自己建的home目录,再把这个home目录压缩,所以压缩包里保留了abc的完整路径。所以解压缩的时候若不用-C指定路径的话,在根目录下解开abc.tar.bz2的话,abc就会放在/home目录下了。至于在其它目录下解开,abc的位置是/home/abc还是当前目录下的home/abc就记不清楚了。我不是什么linux高级用户,命令行用得很少很。
最简单的方法是用tar打包后,再用kde或gnome等图形界面的压缩管理程序打开压缩包就一清二楚了。根本不用这什么麻烦。


这样理解不知道是否有错。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2