Check file properties linux

Beginners Guide for File Command in Linux

The file command in Linux is used to determine the MIME encoding (e.g. ‘image/jpeg; charset=binary’) or file type (e.g. ‘ASCII text’) for the target file.

Tutorial Details

Description Determine file type
Difficulty Level Low
Root or Sudo Privileges Yes (for one command)
Host System and Architecture Ubuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Prerequisites file
Internet Required No
Discussed Tools in this Article

The result (stdout data) comes from the three sets of tests as follows:

  • The filesystem test is used to determine the type of the file or whether the file is empty using the stat command result.
  • Than magic test is performed to check the file for data in particular fixed formats.
  • Lastly, the language test is used to search for particular strings that can appear anywhere in the first few blocks of a file.

Syntax of the File Command

The file command requires two arguments: one is the option, and the other is the filename.

$ file [OPTION] [FILENAME]

Find the Type of the File

The following command will return the property or description of the referenced file (it will also work for files without extensions).

$ file file.txt

Checking the type of file

Printing only the File Type

From the above command, you can remove “file.txt:” from the output by just printing the “ASCII text” in brief mode using the “ -b ” flag.

$ file -b file.txt 

Removing the filename from file command output

Printing the Type of Multiple Files

You can pass multiple files as an argument to the file command to print their properties.

$ file file.txt file.zip 

Checking the property of multiple files

Listing the Files Type in the Current Working Directory

Replace “file.txt” with a “*” wildcard to print the property for all files and directories in the current working directory.

Checking the properties of all files in the current working directory

Listing the Files Type for the Target Directory

Following the previous command, you can specify the directory path and use the “*” wildcard to list all the file properties in the target directory.

$ file Documents/*

Checking the file property in a different location

Listing the Files Type for Specific Range

Specify the range of files within the square bracket “[RANGE]” to find the file property.

Checking the property for the range of files

Align the Output

As you can see above, the padding makes the command unaligned, which can be aligned using the “ -N ” flag.

Читайте также:  Вставить файл через терминал linux

Removing padding from output

Look Inside the Compressed File

The following command will look into the compressed “.zip” or “.tar.xz” files and determine the file property inside.

$ file -z file.zip $ file -z file.tar.xz 

Determine the file property inside the compressed file

Reading the Special Files

The “ -s ” flag allows you to read special files like “/dev/sda” or “/dev/null” files, as shown.

$ sudo file -s /dev/sda $ file -s /dev/null

Checking the properties of special files

Reading the File MIME Encoding

The “ -i ” flag can be used to determine the MIME encoding for the target file.

$ file -i file.tar.xz $ file -i file.txt $ file -i photo.jpg 

Checking the MIME encoding of the referenced file

That was the end of the final example.

Bye bye! We’ll talk again in the next article.

Источник

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.

Читайте также:  Books on linux command line

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

Источник

Linux Windows Install Setup Configuration Project

Using stat command to display file or filesystem status on Linux system.

Using stat command to display file or filesystem status on Linux system.

The example of using Linux stat command on bash shell to display file or filesystem status on Linux Fedora Core machine.

Linux command name: stat

The stat command can be use to check the single file status ( like file properties on Windows) or check the filesystem status on Linux machine. The example below show the step by step on using the stat command to check file status and check filesystem status.

Display file or filesystem status.

[root@fedora ~]# type -t stat

[root@fedora ~]# type -p stat

The stat command will gather the information about a file or filesystem status. This command is useful for checking file permission, inode and symbolic link, for example:

[root@fedora ~]# stat install.log

Size: 61891 Blocks: 144 IO Block: 4096 regular file

Device: 900h/2304d Inode: 910338 Links: 1

Access: (0644/-rw-r—r—) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2005-05-05 12:50:11.000000000 +0800

Modify: 2005-05-05 15:44:16.000000000 +0800

Change: 2005-05-05 15:44:16.000000000 +0800

The output of this command show size, filetype, permission, user id, group id and so on… for the install.log file.

The following are some of the flags and arguments that can be used for the stat command:

-f, —filesystem display filesystem status instead of file status

-c —format=FORMAT use the specified FORMAT instead of the default

-L, —dereference follow links

-Z, —context print the security context

-t, —terse print the information in terse form

—help display this help and exit

—version output version information and exit

The step by step example using stat command on Linux Fedora Core machine.

Type in ‘ stat ‘ on the Linux shell prompt and press [Enter] key to execute the command.

[root@fedora ~]# stat

Try `stat —help’ for more information.

Читайте также:  Tp link driver wifi adapter linux

From the result above, the ‘ stat ‘ command that we issue on the command prompt is not complete.

The output of the above command suggest that we try to issue ‘ stat —help ‘ command to get more information on how to use the stat command.

[root@fedora ~]# stat —help

Display file or filesystem status.

-f, —filesystem display filesystem status instead of file status

-c —format=FORMAT use the specified FORMAT instead of the default

-L, —dereference follow links

-Z, —context print the security context

-t, —terse print the information in terse form

—help display this help and exit

—version output version information and exit

The valid format sequences for files (without —filesystem):

%A Access rights in human readable form

%B The size in bytes of each block reported by `%b’

%b Number of blocks allocated (see %B)

%d Device number in decimal

%N Quoted File name with dereference if symbolic link

%T Minor device type in hex

%t Major device type in hex

%X Time of last access as seconds since Epoch

%Y Time of last modification as seconds since Epoch

%y Time of last modification

%Z Time of last change as seconds since Epoch

Valid format sequences for file systems:

%a Free blocks available to non-superuser

%b Total data blocks in file system

%c Total file nodes in file system

%d Free file nodes in file system

%f Free blocks in file system

%C — Security context in SELinux

%l Maximum length of filenames

%s Optimal transfer block size

%T Type in human readable form

From the help file for the command stat above , there is some options to use with the stat command.

Check file status.

Try to issue ‘ stat ‘ command and then put name of the file we wish to check the status.

[root@fedora ~]# stat install.log

Size: 71590 Blocks: 160 IO Block: 4096 regular file

Device: fd00h/64768d Inode: 940994 Links: 1

Access: (0644/-rw-r—r—) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2005-10-22 22:41:22.000000000 -0400

Modify: 2005-10-23 03:22:34.000000000 -0400

Change: 2005-10-23 03:22:34.000000000 -0400

Then try to issue the same command and now with the -f option to display filesystem status instead of file status.

[root@fedora ~]# stat -f install.log

ID: 0 Namelen: 255 Type: ext2/ext3

Blocks: Total: 2388558 Free: 593795 Available: 470506 Size: 4096

Inodes: Total: 2466048 Free: 2126726

From the example above the command stat can be use to check the file or filesystem status, this quite useful is you want to check the size of file, file IO block, file Inode number, number of link and access permission granted to the file.

stat — display file or filesystem status

for more information on stat command:

Step-by-step how to procedure above tested on:

Operating System: GNU/Linux Fedora Core 4

Kernel Release: 2.6.11-1.1369_FC4

Kernel Version: #1 Thu Jun 2 22:55:56 EDT 2005

Machine Hardware: i686

Machine Processor: i686

Hardware Platform: i386

Shell: GNU bash, version 3.00.16(1)-release (i386-redhat-linux-gnu)

Installation Type: Full Installation (Custom)

Keywords: using stat command, stat command, linux stat command, display file status, filesystem status, check inode, check file ststus, check file permission, check symbolic link, check inode number.

Источник

Оцените статью
Adblock
detector