V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
qingfeng
V2EX  ›  Linux

请教如何将tar.gz和tar.bz2的提取和创建alias成一个命令

  •  
  •   qingfeng · 2013-10-15 18:27:47 +08:00 · 3555 次点击
    这是一个创建于 3818 天前的主题,其中的信息可能已经有所发展或是发生改变。
    请教该如何将tar.gz和tar.bz2的提取和创建alias成一个命令,比如:

    解压和压缩tar.gz文件
    tg -x test.tar.gz
    tg -c test.tar.gz dir_test

    解压和压缩tar.bz2文件
    tb -x test.tar.bz2
    tb -c test.tar.bz2 dir_test

    这样的alias可以实现吗?求教!
    8 条回复    1970-01-01 08:00:00 +08:00
    amyangfei
        2
    amyangfei  
       2013-10-15 19:48:16 +08:00
    zsh 对alias 有更多的支持
    rrfeng
        3
    rrfeng  
       2013-10-15 20:00:58 +08:00
    其实不用 alias 也行啊
    自己用 shell script 写出想要的功能来,丢到 PATH 里不就行了~~
    efi
        4
    efi  
       2013-10-15 22:25:15 +08:00
    apt-get install atool
    apack test.{tar.gz,zip,xz,anything} dir
    aunpack test.*
    likuku
        5
    likuku  
       2013-10-16 00:41:02 +08:00
    现代的 tar 直接支持自动解压 gnuzip 和 bzip2 格式的 tar包:

    tar xf file.tar.bz2
    tar xf file.tar.gz

    都是可以直接解压成最终的目录/文件的。
    msg7086
        6
    msg7086  
       2013-10-16 07:18:23 +08:00
    解压会自动识别。创建的话需要手动指定。

    alias的话也很简单,比如把
    tar czf file.tgz dir
    简化成
    tg c file.tgz dir
    可以写
    alias tg='tar zf'
    leecade
        7
    leecade  
       2013-10-16 11:27:03 +08:00
    # Extract archives - use: extract <file>
    # Credits to http://dotfiles.org/~pseup/.bashrc
    function extract () {
    if [ -f $1 ] ; then
    case $1 in
    *.tar.bz2) tar xjf $1 ;;
    *.tar.gz) tar xzf $1 ;;
    *.bz2) bunzip2 $1 ;;
    *.rar) unrar e $1 ;;
    *.gz) gunzip $1 ;;
    *.tar) tar xf $1 ;;
    *.tbz2) tar xjf $1 ;;
    *.tgz) tar xzf $1 ;;
    *.zip) unzip $1 ;;
    *.Z) uncompress $1 ;;
    *.7z) 7z x $1 ;;
    *) echo "'$1' cannot be extracted via extract()" ;;
    esac
    else
    echo "'$1' is not a valid file"
    fi
    }
    qingfeng
        8
    qingfeng  
    OP
       2013-10-16 13:35:40 +08:00
    @leecade 谢谢!请问如何将该extract函数改成一个正常打包和提取的函数:
    提取出文件: extract -x <file> 或者 extract x <file>
    打包成文件:extract -c <file> <dirname> 或者 extract c <file> <dirname>

    BTW,我是linux新手,刚接触shell,多谢各位的帮助!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2839 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 14:37 · PVG 22:37 · LAX 07:37 · JFK 10:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.