Search

file: classify unknown files on the console

June 29th, 2008 edited by Alexey Beshenov

Article submitted by Caspar Clemens Mierau. Guess what? We still need you to submit good articles about software you like!

Somebody just sent you a mail with attachments that don’t have usable file extensions so you don’t really know how to handle them. Audio file? PDF? What is it? The same problem might occur after a file recovery, on web pages with upload features, etc.

While you can try to give the file an extension and open it with a software you think might be suitable, the better way is to let your computer find out what is all about. As a GNU/Linux user you probably already think “There is surely a command line tool for this”. Of course there is: the file by Ian Darwin.

It often gets automatically installed by dependencies. In any case, aptitude install file will help you. file depends on libmagic which provides patterns for the so called “magic number” detection.

Let’s assume we have the following directory with unknown files:

$ ls -l
total 2152
-rw-r--r-- 1 ccm ccm    4118 2008-03-30 06:32 unknown.0
-rw-r--r-- 1 ccm ccm   10220 2008-05-06 02:23 unknown.1
-rw-r--r-- 1 ccm ccm   12693 2008-05-06 02:23 unknown.2
-rw-r--r-- 1 ccm ccm   25933 2007-10-26 07:41 unknown.3
-rw-r--r-- 1 ccm ccm    2121 2007-10-26 07:41 unknown.4
-rw-r--r-- 1 ccm ccm     185 2007-10-14 20:14 unknown.5
-rw-r--r-- 1 ccm ccm 1189011 2008-05-17 22:37 unknown.6
-rw-r--r-- 1 ccm ccm  824163 2008-02-02 05:02 unknown.7
-rw-r--r-- 1 ccm ccm   82367 2007-09-20 06:18 unknown.8
-rw-r--r-- 1 ccm ccm    8872 2006-04-24 12:43 unknown.9

Now we want to know what’s inside those black boxes. Therefore we just call file * on the console:

$ file *
unknown.0: XML
unknown.1: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
unknown.2: ASCII C program text
unknown.3: PDF document, version 1.4
unknown.4: LaTeX 2e document text
unknown.5: perl script text executable
unknown.6: gzip compressed data, from Unix, last modified: Wed Oct  8 16:27:09 2003
unknown.7: Ogg data, Vorbis audio, stereo, 44100 Hz, ~192003 bps, created by: Xiph.Org libVorbis I (1.0)
unknown.8: PNG image data, 492 x 417, 8-bit/color RGBA, non-interlaced
unknown.9: HTML document text

Hey, that’s all. Pretty impressive, isn’t it? file does even not only distinguishes binaries and text files, it even tries to guess what programming language a text file is written in. And the magic is not that much magic: for example, in case of the ZSH script it just sees a shebang pointing to the zsh in the first line of the file, a PDF file typically starts with “%PDF” and so on. It’s all about patterns.

file provides you with some command line options that make it’s usage even more helpful. The most interesting is -i as it prints out MIME-types instead of verbose file types. If you are a web developer and want to know the exact MIME-type for a file download, this can save you a lot of time:

$ file -i *
unknown.0: text/xml
unknown.1: application/x-object, not stripped
unknown.2: text/x-c; charset=us-ascii
unknown.3: application/pdf
unknown.4: text/x-tex
unknown.5: application/x-perl
unknown.6: application/x-gzip
unknown.7: application/ogg
unknown.8: image/png
unknown.9: text/html

Great, isn’t it? The Apache web server also uses libmagic for this purpose. With file you just use a wrapper for the same task.

file is available in Debian and Ubuntu for a long time.

Posted in Debian, Ubuntu | 8 Comments »

zsh: a REALLY nice alternative for bash

June 18th, 2008 edited by Tincho

Article submitted by Danilo Martins. Guess what? We still need you to submit good articles about software you like!

After years using ZSH instead of BASH, I still don’t understand why isn’t everybody using it. ZSH is a complete shell that will certainly make your life easier. Give it a try.

First of all, you need it installed (duh). APT and its super cow powers will do this for you. You can simply use:

$ sudo apt-get install zsh

and you should be all set. Alternatively, you can install the package “zsh-beta”, but it tends to crash once in a while… ZSH is available on any repository of Debian and Ubuntu.

To try it out, you can simply type zsh, but you wouldn’t be very happy with the result. Let’s personalize it first. First of all, create a file named .zshrc on your home directory. Inside, you should put the text listed here (note: see the commented lines —be sure to try each of them out sometime)

Editor’s note: I’ve moved the sample script to a separate link, to ease cut&paste. Also, note that if you run zsh from Debian testing/unstable for the first time without a .zshrc file, it will greet you with an initial configuration dialog.

Now do run zsh. If you see a different prompt (specially if you did not comment the prompt lines on the .zshrc example above), it’s working. Now, it’s time you see what you can do with this.

First example. Lets suppose you’re doing a long command (cat /etc/mailcap | grep "^audio" | sed s/mplayer/gmplayer) and forget the command syntax for the sed program. You simply move your cursor to the command “sed” and press ALT-H. This should open the man page for sed. After you close the man page, you will automatically be returned with the command you were typing.

Second example. You’re connecting to a remote host using SSH. You remember that you want to use the X11 forwarding, but you don’t remember how to do this. Then, you type “ssh -” and press TAB. Yes, ZSH auto-completes not only files, but also command parameters. Be sure to try it out with whatever command you want to use.

Third example. You have a SSH key to a remote host, and you wish to copy a file from there using SCP. But you do not remember exactly where the file is. You simply type “scp user@remotehost:/home/myuser/myf”, press TAB and watch ZSH doing its amazing trick. Not only ZSH auto-completes files and parameters, but also remote files (remember that for this to work you need to have the RSA/DSA key on the remote host).

Fourth example. You want to install a Debian package (I love this one), but you don’t remember (or are too lazy to) type the whole name of the package. Instead, you simply type “apt-get install mysql-client” and press TAB, and watch ZSH magically auto-completing the package name. If you have more than one, it will list all available below, and if you keep pressing TAB it will complete through each one of them, one at a time. And yes, not only ZSH auto-completes files, parameters and remote files, but it also auto-completes package names. Well, I think we had enough of the “auto-complete” examples, didn’t we?

Fifth example. Yesterday you used a loooooong command and you are too lazy to type it all again. You also do not want to press UP until it appears. Instead of it, you simply type the beginning of the command and try the ALT-P combination. ZSH will auto-complete your command from the history. You can keep using ALT-P and swapping to the previous entries. If you missed it, ALT-N gets you to the next.

Last example. You typed apt-gey install foo bar foo2 bar2 foo3 bar3 foo4 bar4 ... foo239 bar239 and pressed ENTER. Oh, shit, apt-gey does not exist. Instead of pressing UP, HOME, going to “gey” and changing it to “get”, you can simply use ^gey^get. This is a shortcut to “repeat the last command, but all occurrences of “gey” are now “get”.

If you liked the way ZSH works and want to use it as your default shell, you can always use the chsh command, and put /usr/bin/zsh there. It’s now your default shell.

Enjoy!

Posted in Debian, Ubuntu | 34 Comments »

Ghextris: tetris clone with a hexagonal makeover

June 8th, 2008 edited by Tincho

Article submitted by Alex Drachmann. Guess what? We still need you to submit good articles about software you like!

The object of the game is much the same as with tetris, only the frame and the falling objects are hexagons. You have to slot the coloured pieces together making rows, which disappear once they are complete, the game ends if the stack of pieces reaches the celling.

Ghextris

Hexagons are hard to stack, so the game is more challenging than other games of its type, but it gets easier with practice. The game play gets rapidly challenging, as you make mistakes or run out of room and to make it worse, the objects you get are often the most useless for the job, plus it seems as if they fall at an increasingly faster rate. A score is kept based on how many lines you complete. Your current score and the highest score can be viewed on the bottom of the window, so you can try to match or beat your best score or that of a friend.

The interface and graphics are simple and integrates nicely in with the gnome environment. The colouring of the pieces is plain, with no texture or shading, so it doesn’t distract away from the game play. The top bar has two entries: play and hepl. The play menu has three actions, «play» (keyboard shortcut: ctrl+n), «pause» (ctrl+p or just p) and «quit» (ctrl+q). The help menu only has the infamous «about» option, with details about the version and author available.

The keys for playing the game are the direction keys, left and right on the keyboard, which moves the pieces left or right. The up and down keys turn the pieces 90 degrees in one direction or the other. Pressing the space bar makes the current piece crash into the slot directly beneath it.

The version I played was version 0.9.0, which is the current version in the universe repositories of Ubuntu Hardy Heron 8.04 and in the Debian repositories. Ghextris is available in all current releases of Debian and Ubuntu.

The official site of the game is: http://mjr.iki.fi/software/ghextris.

The author of the game and lone developer of it is Mikko Rauhala, who admits to being inspired by a similar game called Xhextris by David Markley.

Posted in Debian, Ubuntu | 2 Comments »

JOE Joe’s own editor: a really usable text editor

June 2nd, 2008 edited by Adelie

Article submitted by Neil Stewart. Guess what? We still need you to submit good articles about software you like!

Back in the day, when I was new to Linux, joe was the first text editor that I managed to quit without having to reboot my machine. That I am still using it today, many years later, goes to show just how simple yet powerful joe is. If you don’t want to use emacs (because what’s the point of running two operating systems?) and still can’t work out how to use vi, then joe is for you.

joe is started from the command line with joe or joe filename to edit filename. When you start joe you get a single header bar with information about where the cursor is, what time it is and, crucially, the message «Crtl-K H for help» (^KH in joe shorthand). So if you press Ctrl and K together, release them, and then press and release H, the top half of the screen is given over to a brief list of commands. And it’s here I learnt how to quit (without rebooting), how to open a new file, how to save a file. And how to move the cursor around, how to find and replace, how to skip over words and lines, how to marks blocks of text and move them about. And you can carry on working on your file —you just type— with the help up. So within a really short amount of time you’ll be up to speed.

JOE

But joe is powerful too. For example, you can process any block of text through any shell command. joe’s help file gives a good example. If you mark the block of text by moving the cursor to the beginning and ^KB, then move to the end and ^KK, then pipe the block of text through sort with ^K/ sort. joe then runs the block of text through sort and replaces the block with the result. Powerful, especially in conjunction with old faithfuls like tr and sed.

Search and replace is also easy. ^KF and enter a search term. That’s it. But search and replace is powerful too. You can reuse old search terms by pressing the up cursor key, including terms from previous sessions. You can use tab completion to complete from any of the words in your current document after you’ve typed the first few letters. Then you can choose what to do when you find it. To replace press R and enter the replace term. You can replace one by one, or replace all. ^L finds the next entry. And you can use regular expressions —this is unix after all. Again the help file has a neat example. If you search for \*,\*,\*$ (\* matches zero or more characters and $ matches the end of a line) and replace with \2,\1,\0 (\X matches Xth pattern found) the string «one,two,three» becomes «three,two,one». Bring up help with ^KH and skip a few screens with ^[. a few times to remind yourself of the regular expressions.

Macros are easy too. Start recording one of ten macros with ^K[ and then a digit. Finish the macro with ^K]. Then use ^Kdigit to play the macro.

joe has many more features. joe remembers where it was in the file when you next edit it. joe colours your C, Java, LaTeX, HTML, and many other formats by default. joe indents your source code and justifies your text. joe supports multiple windows. joe can set bookmarks. You can compile your source code from within joe (^[C) and then skip to the errors the compiler identifies (^C= next error, ^C- previous error).

Finally, joe is old, stable, and just works. It’s in version 3.5 in etch and lenny. I use it for everything, all day every day, and it never fails. Find out much more with info joe or at the Sourceforge home page.

Posted in Debian, Ubuntu | 9 Comments »