In Linux, the “stat” command is used to display detailed information about a file or a file system. It provides various metadata about a file, including access permissions, file size, timestamps, and more. Here’s how to use the “stat” command:
- Open a Terminal:
Launch a terminal emulator on your Linux system.
- Type the “stat” command:
Simply type “stat” followed by the path to the file or directory you want to get information about. For example, to display information about a file named “myfile.txt” in the current directory, run:
If the file or directory does not exist or if you don’t have the necessary permissions, you’ll receive an error message.
- Display Information for Multiple Files:
You can display information for multiple files by specifying their paths separated by spaces. For example, to get information about three files named “file1.txt”, “file2.txt”, and “file3.txt”, run:
stat file1.txt file2.txt file3.txt
- Display Information in a Specific Format:
By default, the “stat” command displays a comprehensive set of information about the file or directory. However, you can format the output to display specific details using format specifiers. For example, to display only the file size and timestamp information for a file named “myfile.txt”, run:
stat -c '%s %x %y %z' myfile.txt
In the above command, the “-c” option is used to specify the format string. The “%s” specifier represents file size, “%x” represents the last access time, “%y” represents the last modification time, and “%z” represents the last status change time.
- Display Information for File System:
If you want to display information about the file system containing a specific file or directory, use the “-f” option followed by the file/directory path. For example, to display information about the file system containing the current directory, run:
stat -f .
- Verifying the Output:
After running the “stat” command, you will see detailed information about the specified file or directory displayed in the terminal. The output will include details such as access permissions, file size, timestamps, file type, and more.
- Exiting the Command:
Once you have obtained the desired information using the “stat” command, you can continue executing other commands or exit the terminal as needed.
The “stat” command is a useful tool for obtaining detailed information about files and directories in Linux. It allows you to view various metadata associated with a file, such as timestamps, file size, and access permissions. By using different options and format specifiers, you can customize the output to display specific information based on your requirements.