Linux view file attributes

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 Check and Set the Linux File Attributes on the Command Line

In Linux, file attributes are additional metadata associated with a file or directory that provides additional information or functionality. You can view and change these attributes by utilizing the lsattr and chattr commands. Understanding and properly utilizing the file attributes can improve the security and performance of a Linux system by checking and setting the Linux file attributes, but many beginners don’t know how to do it in Linux. In this short guide, we will explain the simple ways to check and set the Linux file attributes on the command line.

How to Check and Set the Linux File Attributes on the Command Line

Here, we will give you a brief discussion on the two commands that you can try to check and set the file attributes from the terminal.

Lsattr Command

The lsattr command is used to display the file attributes of a given file or directory. The command output shows a series of letters corresponding to specific attributes. For instance, the “i” attribute makes the file immutable. It means that the file cannot be modified or deleted. The “d” attribute signifies that the file is a directory.

To check the attributes of a file or directory, simply type “lsattr” followed by its name in the terminal. Here’s an example:

This command outputs the attributes of the “TestFile.txt” file. The output looks something like the following image. We can see that the “e” attribute is set, indicating that the file will use extents to map the blocks on the disk.

Each letter in the output corresponds to a specific attribute:

Attribute Full Form Explanation
a Append only Using this mode, you can add more content to the file, but you cannot remove the content from the file.
c Compressed The data that is written to the file is compressed automatically by the kernel, and it is uncompressed back at the time of reading.
d No dump This attribute ensures that the file backup is not made in the backups where the dump is used.
e Extent format For block mapping on the disk, the file uses extent.
i Immutable Using this attribute, the file becomes immutable, which means that you are restricted from performing operations on the file.
s Secure deletion You cannot recover the file after deletion using this option.
t No tail-merge In this process, small data pieces that don’t fill the complete block are merged with similar data pieces from other files.
u Undeletable The contents of the deleted files are saved.
A No atime update The access timestamp of the Linux file is not updated when you access the file.
D Synchronous directory updates Modifications made to the file are promptly stored on the disk before they reach the cache.
S Synchronous updates The modifications made to the file are immediately written to the disk.
Читайте также:  Перенос папки home linux

Chattr Command

You can use the chattr command to alter the attributes of a specified file or directory. This command takes a + or – flag, followed by the attribute to be added or removed. For example, this command makes the “TestFile.txt” file immutable and can no longer be deleted or modified.

For example, to remove the immutable attribute, use the following command:

File Immutability
One common use case for the chattr +i command is to make a file or directory immutable to prevent accidental deletion or modification. This can be useful for important system files or for files that are frequently backed up.

Improves Performance
Another use case for the chattr +a command is to improve the performance of your file system. The “append only” attribute (a) can be added to the files to improve the performance on the filesystems that do not support the append-only mode. This attribute causes the filesystem to write the data to the end of the file only rather than changing the existing data, which can improve the performance in certain situations. To set a file to append-only mode, use the following command:

Secure Deletion
The chattr +s command can securely erase the file when it is deleted. This attribute overwrites the file with zeros before unlinking it, making it much more difficult for someone to recover the deleted data. To set a file for secure deletion, use this command:

Conclusion

File attributes are essential to Linux systems and can provide additional functionality or security. Understanding and properly utilizing the lsattr and chattr commands can help improve your Linux system’s overall performance and security. File immutability, append-only mode, and secure deletion are examples of how the file attributes can achieve specific goals. It is recommended to use these commands with caution and after correctly understanding the file attributes and their impact on the system.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.

Источник

Stat Command: Display Attributes of Files and Directories

You can get file permissions, size, mtime, ctime, atime, ownership and several other file attribute information using the stat command in Linux.

What if I told you there is a better way to check file attributes? It’s a simple but useful command called stat.

stat command in Linux

Stat Command

The stat command lists important attributes of files and directories. It can also display information on the filesystem, instead of the files.

Читайте также:  Посмотреть количество соединений linux

There are only a few options with stat command:

  • -f : Show the information for the filesystem instead of the file
  • -L : Follow symbolic links and show information on the file they point to
  • -t : Terse mode prints the information in a single line

Using stat command to get file information

Here’s a sample output if you use the stat command without any options:

stat sample.txt File: sample.txt Size: 426 Blocks: 8 IO Block: 4096 regular file Device: 10302h/66306d Inode: 8259907 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/abhishek) Gid: ( 1000/abhishek) Access: 2020-01-06 09:48:02.908946552 +0530 Modify: 2020-01-05 10:16:33.225203223 +0530 Change: 2020-01-06 19:51:28.737207649 +0530 Birth: -

Let me explain the output to you:

  • File: Name of the file.
  • Size: Size of the files in bytes.
  • Blocks: Number of filesystem blocks the file needs on the disk.
  • IO Block: Size of a filesystem block. It is usually 4 KB.
  • File type: It could be file, directory, link, sockets.
  • Device: The device number (ID of the hard drive) in hexadecimal and decimal.
  • Inode: The inode number. Read more about inodes in Linux.
  • Links: Number of hard links to the file.
  • Access: File permissions in absolute mode and symbolic mode.
  • Uid: User id and the user name of the file owner
  • Gid: Group id and group name of the file owner.
  • Access: This access is about access timestamp (last time the file was accessed), also called atime.
  • Modify: The modify timestamp (last time the file was modified), also called mtime.
  • Change: The change timestamp (last time the file was called), also called ctime. Read this article to know more about atime, mtime and ctime.
  • Birth: Original creation date of the file. This is not used in Linux.

If you use the terse mode with option -t, you’ll see pretty much the same information (some orders could be different) in a single line but without any description of which field is what.

[email protected]:$ stat -t sample.txt sample.txt 426 8 81b4 1000 1000 10302 8259907 1 0 0 1578284282 1578199593 1578320488 0 4096

Get filesystem information with stat command

You can use the -f option to display information of the filesystem instead of the file itself.

[email protected]:$ stat -f sample.txt File: "sample.txt" ID: 65205b688c8b079c Namelen: 255 Type: ext2/ext3 Block size: 4096 Fundamental block size: 4096 Blocks: Total: 61142095 Free: 41522899 Available: 38399635 Inodes: Total: 15597568 Free: 15279217

As you can see in the output, it gives you the following information:

  • File: Name of the file.
  • ID: Filesystem ID in hexadecimal.
  • Namelen: Maximum length for file names.
  • Type: Filesystem type.
  • Block size: The amount of data to request read requests for optimum data transfer rates.
  • Fundamental block size: Size of a block on filesystem
  • Total blocks, free blocks and available blocks (for non-root users)
  • Number of total inodes and free inodes

Use stat command with multiple files

You can run the stat command with multiple files.

You can provide their names individually like this:

You can also provide a file name pattern:

I hope you liked this handy little command. Stay tuned for more Linux command learning.

Источник

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