Special File Types   «Prev  Next»
Lesson 5 Determining file types
Objective Use the ls -F and the File Commands to identify File Types.

Use ls -F and the File Commands to identify File Types

Question: How do I use the ls -F and the file commands to identify file types in Unix
In Unix-based systems, two essential commands used to identify file types are
  1. ls -F and
  2. file.
These commands are key tools for system administrators, developers, and users alike who need to quickly ascertain the type of content present within a file or directory.
  1. Using ls -F Command:
    The ls command is used to list directory contents, and the -F flag can be appended to provide additional context to the listing, such as denoting the file type.
    Here's how to use ls -F:
    ls -F /path/to/directory
    

    When you use the ls -F command, it appends a character to the end of each listed item, indicating its file type:
    1. / denotes a directory.
    2. * denotes an executable file.
    3. @ denotes a symbolic link.
    4. = denotes a socket.
    5. | denotes a named pipe (FIFO).
    6. > denotes a door (only available in Solaris).
  2. Using 'file' Command:
    While ls -F provides a simplistic representation of file types, the file command delves deeper. It performs tests on a given file, such as filesystem tests, magic tests, and language tests to ascertain the file's true type.
    Here is how to use the file command:
    file /path/to/file
    

    This will output a description of the file based on the tests performed. It can recognize many types of files, including text, binary, executable, and more.
    Here's an example:
    file myscript.sh
    

    Output:
    myscript.sh: Bourne-Again shell script text executable
    

    In this case, the file command has identified myscript.sh as an executable shell script text file.
Together, ls -F and file provide you with an effective toolset for identifying file types in Unix-based systems. However, it's crucial to note that these commands don't guarantee the identification of all file types. For instance, a Python script without execution permissions will appear as an ordinary text file. Therefore, understanding the context and potential contents of the files you're working with remains essential.
UNIX systems have two commands for conveniently identifying the types of files you may encounter.

ls -F
The first useful command is the -F option to the ls command. This option lists the files in a directory, with a different flag for each type of file. For example:
$ ls -F $HOME
nmap@  readme*  subdir/  temp/

Notice that ls -F flags the different types of files:
  1. Directory (/)
  2. Executable (*)
  3. Symbolic link (@)
Many people find this option to the ls command so convenient that they use it automatically, in conjunction with other options. For example:
$ ls -lF
total 3
drwx-wx-wx  3 user1  user1  810 Jun 7 19:46 nmap
-rwx-wx--x  1 user1  user1    0 Jun 7 19:32 readme*
dr-xr-xr-x  2 user1  user1 1024 Jun 7 19:40 subdir/
drwx------  3 user1  user1 1024 Jun 7 19:49 temp/

The file command

The second useful command is the file command, which provides even more information about file types. The syntax for the file command is
file file1 [file2 ....]

Here is an example of using the file command on the same directory we used the ls –F command:
$ file $HOME/*
/home/user1/nmap:   symbolic link to /dev/nmap
/home/user1/readme: empty
/home/user1/subdir: directory
/home/user1/temp:   directory

The file command applies a series of rules to determine the file type. Some of the more common types of files detected by the file command are text files, source code files, executable files, and directories. Here is another example, showing information about several other file types:

$ file /dev/*
/dev/MAKEDEV:    Bourne shell script text
/dev/audio:      character special (14/4)
/dev/bpcd:       block special (41/0)
/dev/cdrom:      symbolic link to hdb
/dev/console:    character special (4/0)
/dev/log:        socket
/dev/addftinfo:  ELF 32-bit LSB executable,
Intel 80386, version 1, dynamically
linked, stripped
/dev/apache_1.3.3.tar.gz: gzip compressed data,
deflated, original filename, last modified:
Wed Oct  7 02:50:59 1998, max compression,
os: Unix