Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
qingfeng
V2EX  ›  Linux

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

  •  
  •   qingfeng · Oct 15, 2013 · 4420 views
    This topic created in 4600 days ago, the information mentioned may be changed or developed.
    请教该如何将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 replies    1970-01-01 08:00:00 +08:00
    amyangfei
        2
    amyangfei  
       Oct 15, 2013
    zsh 对alias 有更多的支持
    rrfeng
        3
    rrfeng  
       Oct 15, 2013
    其实不用 alias 也行啊
    自己用 shell script 写出想要的功能来,丢到 PATH 里不就行了~~
    efi
        4
    efi  
       Oct 15, 2013
    apt-get install atool
    apack test.{tar.gz,zip,xz,anything} dir
    aunpack test.*
    likuku
        5
    likuku  
       Oct 16, 2013
    现代的 tar 直接支持自动解压 gnuzip 和 bzip2 格式的 tar包:

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

    都是可以直接解压成最终的目录/文件的。
    msg7086
        6
    msg7086  
       Oct 16, 2013
    解压会自动识别。创建的话需要手动指定。

    alias的话也很简单,比如把
    tar czf file.tgz dir
    简化成
    tg c file.tgz dir
    可以写
    alias tg='tar zf'
    leecade
        7
    leecade  
       Oct 16, 2013
    # 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
       Oct 16, 2013
    @leecade 谢谢!请问如何将该extract函数改成一个正常打包和提取的函数:
    提取出文件: extract -x <file> 或者 extract x <file>
    打包成文件:extract -c <file> <dirname> 或者 extract c <file> <dirname>

    BTW,我是linux新手,刚接触shell,多谢各位的帮助!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5885 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 57ms · UTC 02:31 · PVG 10:31 · LAX 19:31 · JFK 22:31
    ♥ Do have faith in what you're doing.