File command in Linux

File Command In Linux

The file command is used to determine the type of file i.e. ASCII or MIME type. It doesn’t care about the extension used for the file. The command simply is used to identify the file type. There are various other options used with the file command.

$ file filename
Filename

Here’s a quick list of the commonly used file command options in Linux:

  • -b or –brief: to get the short description of the file
  • file *: to list all the files in the system
  • <directory>/*: to get the filetypes in a specific directory
  • <regex range>: to get file types in a specific range
  • -c: to get the parsed form of any file
  • -F: file separator between file and its type
  • -i: to get the mime type
  • -z: to view details of compressed files
  • <space separated filenames>: to get the filetypes of multiple files
  • –help: to get the complete manual

Get a Brief Description

-b or –brief option is used with file command to get brief description of any file. It would only display the file type without its name.

$ file -b hello.py
Fileb

List the Type for All the Files

This command is used to display the types of all the files or directories. The file command uses '*' to list the types of all the files in the system.

$ file *
Filestar.
File star

File Command using Directory Names

This command is used to display all file filetypes in a particular directory.

$ file B2/*
Filedirectory

Listing File Types using Regex Style Ranges

This command is used to display file types of files in a specific range. Select the range i.e from A to Z or A to F, it can be anything. It will display only those names which are in range.

$ file [a-e]*
Filerange

Using -c option

This command option is used to print the parsed form of any file i.e type, opcode, value, mask and so on.

$ file -c
Filec

Custom Separator For File Type Output

As you may have noticed above, the file name and file type is separated with a colon :. To change this separator in the output, you can use the -F option. The -F option followed by the separator will change the default separator as shown in the image below.

$ file -F -  hello.py 
$ file -F + hellp.py
Filef

Find File MIME Type Using File Command

This command is used to view the mime type of file.

$ file -i hello.py 
Filei

Display Compressed File Types

The -z option for the file command will give us the details of an archive file.

$ file -z {compressed file name} 
Filez

Display Types for Multiple Files

We can enter multiple file names separated by a space to display the file types for them at once. Have a look at the example below.

$ file f.txt .local Desktop
Filelocal

Here, f.txt, .local, and Desktop are file names.

Help Command Option

This command is used to get a list of all the options that are available for the file command.

$ file --help
Filehelp

Alternatively, you can also use the man command in Linux to get detailed help on the file command.

Conclusion

In this tutorial, we’ve gone over all the options for the file command in Linux. We hope that you now know how to use the command efficiently. If you have any questions, do let us know in the comments.

Leave a Comment

Your email address will not be published. Required fields are marked *