- Linux file Command | Explained
- What is the file Command in Linux?
- How to Use the file Command in Linux?
- Check the Status of an Empty File
- Check the Status of a Non-Empty File
- Check the Status By Providing the Path of the File
- Check the Status of a Directory Using file Command
- Check the Status Only Without the Path/Name
- Check the Status of All files and Folders in the Directory
- Check the Status of the files/Directories in Sub-Directories
- Conclusion
- Essential Examples of the File Command in Linux
- What is the file command in Linux and Unix?
- Example of file command in Linux
- Remove filename from the output
- Get the mime type of the file
- Get file type info of a file inside an archive file
- Use file command with multiple files
- Use file command with regex
- Other options you may use with file command
- File command in Linux
- Get a Brief Description
- List the Type for All the Files
- File Command using Directory Names
- Listing File Types using Regex Style Ranges
- Using -c option
- Custom Separator For File Type Output
- Find File MIME Type Using File Command
- Display Compressed File Types
- Display Types for Multiple Files
- Help Command Option
- Conclusion
Linux file Command | Explained
In Linux, the file command is used to retrieve the file type and the data associated with the file. The operations performed by the file command are irrespective of the file extension. This post will demonstrate the brief working and usage of the file command in Linux. The content that supports this is as follows:
- What is the file Command in Linux?
- How to Use the file Command in Linux?
- Check the Status of an Empty File
- Check the Status of a Non-Empty File
- Check the Status By Providing the Path of the File
- Check the Status of a Directory Using file Command
- Check the Status Only Without the Path/Name
- Check the Status of All files and Folders in the Directory
- Check the Status of the files/Directories in Sub-Directories
What is the file Command in Linux?
Whenever you are trying to learn about a new command, you need to understand the syntax and the relevant options of the command. The syntax of the file command is provided below:
The syntax of the file command is mentioned below, which will help you understand.
For details on options and their application, you can type the following command, whereas the file could be any file on which you are applying this command:
Doing this, you will be able to understand its basic syntax and the flags information associated with it.
How to Use the file Command in Linux?
This section provides a list of examples representing the possible usages of the file command in Linux. Let’s dig into them one by one:
Check the Status of an Empty File
We have an empty file; let’s check how the file command behaves in the case of an empty file:
As you can see, it shows that there is nothing inside the file, and the text file is empty.
Check the Status of a Non-Empty File
Now, the same file is equipped with some content. Let’s execute the file command on the file (a nonempty file):
It is observed that the file format is “ASCII. ”
Check the Status By Providing the Path of the File
The above command will execute only if you are in the same directory, so if you want to check the status of any file in a different directory, then you can do that by typing:
Check the Status of a Directory Using file Command
As you have checked the status of the files above, in the same way, you can check the status of any directory as well by typing:
$ file /home/itslinux/Downloads
In the above command, you need to provide the path of any directory for which you want to check the status.
Check the Status Only Without the Path/Name
If you are only interested in finding the status of a file or a directory, then you need to use the ‘-b’ flag as we did in the following command:
Similarly, the same you can do this with the directory as well by typing:
$ file -b /home/itslinux/Downloads
Check the Status of All files and Folders in the Directory
Checking the status of files and folders individually is time-consuming. Let’s suppose you want to check the status of the home directory then you can do that by typing:
You can see in the above image that most of them are the directories, whereas there is one empty file and one file with the ASCII text on it.
Check the Status of the files/Directories in Sub-Directories
Similarly, you can check the status of the files/sub-directories of any directory using the file command. For instance, in the case of the “Documents” directory, the command would be:
Conclusion
As a Linux user, you might come across different types of files in your daily life, which can confuse you. If you want to figure out the type of file and its operation, then you can do that using a file command that can either be an ASCII file or a MIME. So the details about the file command and how you can utilize it in a better way have been discussed in this article.
Essential Examples of the File Command in Linux
Here are various examples of the file command in Linux to determine the actual type of file and gather related information.
You have seen commands to create file in Linux. You have also seen the command to delete file in Linux. Let’s talk about the file command in this article.
What is the file command in Linux and Unix?
How do you recognize the type of a file? Let me guess, by its extension, right? I mean if you see a file named MyData.csv, you guess that the file is in CSV format. You can make sure of that by viewing the file content.
But the file extensions in Linux and Unix have no real meaning. You can name a csv file, a zip file. You can choose to not use any extension at all.
The file command comes handy in such situations. The file command in Linux determines the actual type of a file, no matter what its extension is.
It has a simple syntax with only a few options:
Now that you know the syntax let’s see how to use the file command.
Example of file command in Linux
In its simplest form, you can use the file command with filename or path to file and it will show the type of the file.
[email protected]:~/$ file cpluplus.cpp cpluplus.cpp: C++ source, ASCII text
Let’s see some other ways you can use it with its options.
Remove filename from the output
You can use the option -b and the output will show only the file type omitting the filename. It could be useful in scripting.
Have a look at the same example you saw earlier:
[email protected]:~/$ file -b cpluplus.cpp C++ source, ASCII text
Get the mime type of the file
You can also display the MIME type of the file thanks to the -i option.
Here’s an example of the command on a video file, with and without MIME type info:
[email protected]:~/$ file my_video.mp4 my_video.mp4: ISO Media, MP4 v2 [ISO 14496-14] [email protected]:~/$ file -i my_video.mp4 my_video.mp4: video/mp4; charset=binary
You can combine -b and -i options.
Get file type info of a file inside an archive file
If you gzip a directory and now you have a compressed file. You can examine the uncompressed contents to decide the file type with -z option.
Let me show you an example with and without the -z option:
[email protected]:~/$ file author-pro.zip author-pro.zip: Zip archive data, at least v2.0 to extract [email protected]:~/$ file -z author-pro.zip author-pro.zip: PHP script, ASCII text (Zip archive data, at least v2.0 to extract)
Use file command with multiple files
File command can be run on multiple files simultaneously.
Here’s an example for you so that you can see it in action:
[email protected]:~/$ file cpluplus.cpp agatha.txt bash_script.sh cpluplus.cpp: C++ source, ASCII textagatha.txt: ASCII text bash_script.sh: Bourne-Again shell script, ASCII text executable
Use file command with regex
If you want to use the file command on multiple files, you don’t always have to provide all the filenames. You can use regex instead.
It’s really up to your requirement and imagination how you could use it. I’ll show some examples nevertheless.
If you want to display file type of all the files in the current directory, simply use this:
If you want to display file type of all the files with a certain extension, you can do that as well:
The possibilities are endless. Want to display file type of files with name starting with ‘a’? Use this:
Other options you may use with file command
Here are a few other options with file command that you may use:
- -L : Follow symbolic links and report the type of the destination file
- -f file_name : Read filenames line by line from the given file_name and report their file type
There are a few other options as well but I believe you have learned all the essential examples of the Linux file command. If you have questions or suggestions, do let me know in the comment section.
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.
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
- /*: to get the filetypes in a specific directory
- : 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
- : 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.
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 Command using Directory Names
This command is used to display all file filetypes in a particular directory.
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.
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.
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
Find File MIME Type Using File Command
This command is used to view the mime type of file.
Display Compressed File Types
The -z option for the file command will give us the details of an archive file.
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.
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.
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.