- 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
- how to view all details or metadata of a file in linux command line
- ls Command
- stat Command
- Image Files
- how to find the meta information of a file using BASH [closed]
- 4 Answers 4
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.
This post and this website contains affiliate links. See my disclosure about affiliate links.
how to view all details or metadata of a file in linux command line
When viewing a particular file in Linux, you might want to see all the relevant file metadata associated with it. The file metadata details includes information regarding its size, permissions, creation date, access date, inode number, uid/gid, file type etc.
There are mainly two different commands that you can use for this purpose, ls and stat. Both will print out almost the same information but in different format.
ls Command
The most useful of the two commands is ls, (at least in my opinion) which lists the file details. Using some command line options you can print out all the details and metadata information of the particular file.
The various command line options above prints out various information as detailed below
l : This uses the long listing format while printing out. This is much more informative than the default format.
i : Prints out the inode number of the file
s : Prints the file size in blocks
a : Prints out all entries and does not ignore any files
n : Prints out the numeric user id and group id
h : Print the sizes in human readable format.
The above command will print out all relevant metadata information about the file, but usually is not very human readable in its format. As long as you know what information is printed in each column it should work just fine.
If you prefer a much more human readable format, then you can use the stat command instead.
stat Command
The basic stat command works without any command line arguments, other than the file name…
[root 17:03:19] ~ # stat world_bkp
File: ‘world_bkp’
Size: 2434 Blocks: 8 IO Block: 4096 regular file
Device: 804h/2052d Inode: 262149 Links: 1
Access: (0644/-rw-r—r—) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-02-13 05:06:57.883217273 -0600
Modify: 2014-02-13 05:06:57.893217273 -0600
Change: 2014-02-13 05:06:57.893217273 -0600
Birth: —
This prints out almost the same information as the ls command, but prints it out in a much more human readable format. There is also a basic description for each value in the format.
Both the ls and stat commands can be used for any file descriptor, which means both the file as well as directories. stat can be used on file systems as well.
The -f command line option specifies to print out the file system status instead of the file status. You can see the difference by running the command with and without the -f option.
If you are using another command such as find or locate to print out files, then you can pipe (|) the output of that command to either ls or stat to print out more meaningful information. A simple example is
bash$ locate world_bkp | xargs stat
The above command will print out the file details exactly as before, but is useful if you didn’t know the exact location of the world_bkp file. Another example of piping to ls command is
bash$ locate world_bkp | xargs ls -lisan
Again, as with most Linux commands, you can input multiple files in the command line to print the details of multiple files or use the pipe to output details of multiple files.
Image Files
There are in fact a couple of more commands that shows you specific information of a file depending on the type of the file. If you want to see what the file type is, then the file command can help you out.
Also in the case of image files, the identify command that is part of the imagemagick package is a very good option. The identify command will print out the image specific properties such as the format, colorspace, channel information etc.
bash$ identify -verbose myimage.jpg
how to find the meta information of a file using BASH [closed]
How to find the meta-information of a file in BASH? And how to extract and print it separately using cut and grep commands?
I’ve Found it Its » ls -l » Information About A File.. Like File Name, Permission, File Owner, Date etc..
find . -name «filename.txt» -print | xargs ls -iSl Will give you all files named filename.txt in the current directory (the dot). Then xargs is used to let ls get the standard input from the piped command. Then -iSl sorts and give you size and date. 6710 -rw-rw-r— 1 userA GroupB 2346 Nov 26 00:55 ./somefolder/filename.txt
4 Answers 4
Instead of parsing the output of ls using cut / grep , you should just use stat which takes a -c argument to specify the output format.
anthony@Zia:~$ stat -c '%n : %A : %U : %s' afiedt.buf .XCompose afiedt.buf : -rw-r--r-- : anthony : 178 .XCompose : lrwxrwxrwx : anthony : 38
You can change the output format however you’d like; check the stat(1) manpage for details.
I’m just guessing here, but have you tried the command file ? It will try to identify what kind of file it is.
Actually, My Output Should Be Like This: File Name: File Type: File Owner: File Permission: File Size: I’ve Used ls -l To Find The Meta-Info Of a File. I Need To Extract And Print It Like Above.
@meandyxtreme Have a look at the cut command that is very good at picking values from columns. And, please, stop with the «Capital Letter At The Start Of Every Word» thing. It makes it very hard to read.
Sorry about the capitalizing of every word. Look what i typed [ur10cs164@linux lab]$ ls -l lab2.txt -rw-rw-r-- 1 ur10cs164 ur10cs164 801 Jul 19 16:40 lab2.txt
so the above line shows all the meta-info. so now how to extract them seperately. i’ve tried ‘cut’. Maybe the format i’ve typed may wrong. Lemme know the correct method to extract from it