edit this article

Using the file command

This tip demonstrates the use of the file command. This command has been in UNIX since at least 1973 but is often overlooked. However, it's an extremely useful command for classifying files.

The file command is provided by the package sys-apps/file, so if you don't already have it installed, you'll need to type emerge file. Chances are you've already got it on your system though. To use file simply type file filename and you'll see information about the filename you passed in. Simple right?

Code Listing 1: Using file

% file css current.xml tips-20031117.xml tips-20031117.html screenlog.0 
css: directory
current.xml: symbolic link to `tips-20031117.xml'
tips-20031117.xml: ASCII English text
tips-20031117.html: UTF-8 Unicode HTML document text, with very long lines
screenlog.0: ASCII English text, with CRLF, CR line terminators,
with escape sequences, with overstriking

// You can also use file to look at shell scripts
% file bin/fix bin/update_dat.sh
bin/fix: a /usr/bin/perl -w script text executable
bin/update_dat.sh: a /bin/sh script text executable

// Or you can check binary files
% file /bin/cp
/bin/cp: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for
GNU/Linux 2.4.1, dynamically linked (uses shared libs), stripped

// As the last example, you can use it on 'core' files
% file core
core: ELF 32-bit LSB core file Intel 80386, version 1 (SYSV), SVR4-style,
SVR4-style, from 'a.out'

This should get you started but there are many other uses of file, see the man page (man 1 file) for more information.

From http://www.gentoo.org/news/en/gwn/20031117-newsletter.xml

rate this article:
current rating: average rating: 1.1 (31 votes) (1=very good 6=terrible)
Your rating:
Very good (1) Good (2) ok (3) average (4) bad (5) terrible (6)