News for hackers. Stuff that matters.
Re:Use long flags when scripting (2013) by lukeschlather on Friday September 18, 2020 @04:47PM (#24521746)
tar is one of the worst. I think I know what those do without looking it up but I think most people look at that with bewilderment. Randall Munroe is always helpful in these matters and has this to say: https://xkcd.com/1168/
Re:Use long flags when scripting (2013) by UI_at_80x24 on Friday September 18, 2020 @05:02PM (#24521861)
I remember 90% of the time which flag I want to use, but the other 10% sends me into a rage! =)

I gave up and added this to my .bashrc:

    function extract()      
  {
     if [ -f $1 ] ; then
         case $1 in
             *.tar.bz2)   tar xvjf $1     ;;
             *.tar.gz)    tar xvzf $1     ;;
             *.bz2)       bunzip2 $1      ;;
             *.rar)       unrar x $1      ;;
             *.gz)        gunzip $1       ;;
             *.tar)       tar xvf $1      ;;
             *.tbz2)      tar xvjf $1     ;;
             *.tgz)       tar xvzf $1     ;;
             *.zip)       unzip $1        ;;
             *.Z)         uncompress $1   ;;
             *.7z)        7z x $1         ;;
             *.zst)       zstd -d $1      ;;
             *.xz)        unxz $1         ;;
             *)           echo "'$1' cannot be extracted via >extract<" ;;
         esac
     else
         echo "'$1' is not a valid file"
     fi
}
Re:Use long flags when scripting (2013) by thequux on Friday September 18, 2020 @05:26PM (#24522041)
Recent (as in, within the last ~3-5 years) versions of GNU tar automatically detect the type of compression and apply the appropriate tool, so you can often get away with `tar xvf`.
Re:Use long flags when scripting (2013) by ahartmetz on Friday September 18, 2020 @07:11PM (#24522751)
Actually close to 12 years, GNU tar 1.21 https://www.gnu.org/software/tar/

Another "recent" (much more recent IIRC) change is that you don't need the dot anymore in find to search in the current directory.

Re:Use long flags when scripting (2013) by Hello71 on Friday September 18, 2020 @07:39PM (#24522941)
according to https://git.savannah.gnu.org/cgit/findutils.git/commit/find/..., this functionality has been present since findutils was moved under source control in 1996.
Re:Use long flags when scripting (2013) by jfrunyon on Tuesday September 22, 2020 @12:17AM (#24550788)
I believe that GNU find used to (in some cases?) show a warning if the directory was omitted. Could be wrong.
Re:Use long flags when scripting (2013) by mehrdadn on Friday September 18, 2020 @08:48PM (#24523635)
I think the dot actually gives you issues on Mac? Or something like that. I remember encountering issues with it.
Re:Use long flags when scripting (2013) by taejo on Saturday September 19, 2020 @04:24AM (#24526044)
You have to specify a directory for the find on Mac; it's only a GNUism to omit it.
Re:Use long flags when scripting (2013) by mehrdadn on Saturday September 19, 2020 @06:34AM (#24526455)
Oh I see, I got it backwards somehow. Thanks!
Re:Use long flags when scripting (2013) by Hello71 on Friday September 18, 2020 @07:45PM (#24522991)

  extract() {
    bsdtar -xf "$1"
  }
you can decompress things which are not files, such as stdin or device files. in addition, your code does not cover *.tar.xz (more common than tar.bz2 nowadays), or lzma, or lz4, or tar.zst, or many many other formats. further, it's not even consistent: bunzip2, gunzip, and unxz remove the input file, but tar, unrar, 7z, and zstd do not.
Re:Use long flags when scripting (2013) by rbonvall on Friday September 18, 2020 @07:10PM (#24522743)
There's a utility called dtrx ("do the right extraction") that does basically this, and also makes sure that files are always extracted into a directory.
Re:Use long flags when scripting (2013) by nobody9999 on Saturday September 19, 2020 @09:36AM (#24527326)
I can certainly understand the impulse to something like this, but as other posters pointed out, your case statement is incomplete, and will continue to become more so as new tools become available.

Personally, I consciously avoid using the internal decompression features of tar, both out of habit and to avoid unexpected results.

As such, I generally use command lines like: bunzip2 -c <tar.bz2 file> |tar xvf -

instead of relying on tar xvjf

I don't see it as wrong or bad to use tar's decompression features, it's more about my own preferences and experience. Being able to perform similar actions in multiple ways is one of the things I've always appreciated about the shell and the Unix/GNU userland.

Re:Use long flags when scripting (2013) by maple3142 on Friday September 18, 2020 @08:11PM (#24523228)
You can use atool (https://www.nongnu.org/atool/) too. It is just some perl scripts wrapper around some commom extraction tools.
Re:Use long flags when scripting (2013) by eMSF on Friday September 18, 2020 @08:06PM (#24523182)
I guess you only ever extract punctually named files. But it's still rather amusing that with the exception of 'case', the only places where $1 is properly quoted are the ones where it's not really necessary, and vice versa (but then again, it's not done for the parameter, but rather the single quotes).

Anyway, my advice to you and anyone else having trouble with tar is that "tar caf" and "tar xaf" are the only things most people need to remember about tar, with or without compression.

(In this case, most people means people who use tar, but so rarely that they have trouble remembering how to use it; they probably never use it for anything else other than (un)archiving. Also, xkcd isn't gospel.)

Re:Use long flags when scripting (2013) by stack_underflow on Friday September 18, 2020 @05:34PM (#24522104)
These two commands should fit most of your use cases:

  tar cf dir.tar dir          # mnemonic: 'create file' <tar-file-name> <dir-to-tar>
  tar xf dir.tar(.gz|bz2|...) # mnemonic: 'eXtract file' <tar-file-name>
On systems with "modern" versions of tar `-x` is capable of recognizing which compression format is used and doesn't require the explicit `-j/z` flags you usually see.
Re:Use long flags when scripting (2013) by corpMaverick on Friday September 18, 2020 @07:28PM (#24522874)
Exactly, these are the only ones I know and never get confused. Actually I use cvzf and xvzf.
Re:Use long flags when scripting (2013) by stjohnswarts on Saturday September 19, 2020 @12:06AM (#24524945)
you might want to at -p for preserving permissions in there are well, handy for archiving
Re:Use long flags when scripting (2013) by a1369209993 on Saturday September 19, 2020 @01:59AM (#24525494)
> To disarm the bomb, simply enter a valid tar command on your first try. No Googling. You have ten seconds.

  $ tar --help
Obviously. :)
Re:Use long flags when scripting (2013) by scbrg on Saturday September 19, 2020 @02:43AM (#24525664)

    $ tar --help
    tar: unknown option -- help
    usage: tar [-]{crtux}[-befhjklmopqvwzHJOPSXZ014578] [archive] [blocksize]
               [-C directory] [-T file] [-s replstr] [file ...]
    $
Re:Use long flags when scripting (2013) by a1369209993 on Saturday September 19, 2020 @03:17AM (#24525787)
Okay, so your tar is defective[0], and? I can do that too:

  $ list '#! /bin/sh' 'echo "tar: invalid command" >&2' 'exit 64' >/tmp/tar
  $ chmod a+x /tmp/tar
  $ PATH="/tmp:$PATH"
  $
  $ tar -cf a.tar a/
  tar: invalid command
  $
0: any program that doesn't support --help is defective, which was rather my point.
Re:Use long flags when scripting (2013) by scbrg on Saturday September 19, 2020 @03:56AM (#24525929)
> 0: any program that doesn't support --help is defective, which was rather my point.

Fair enough. My point was that there's a category of operating systems where long options (including --help) isn't really a thing. You may of course consider them defective, though I'm not sure everybody agrees.

Re:Use long flags when scripting (2013) by dev_tty01 on Sunday September 20, 2020 @09:54AM (#24534056)
$ tar -h

Even better. :)