A file does not store only data, it comes with a lot of information related to the file called metadata. This metadata includes information about the file, including its creation and modification timestamp, file size, ownership, and so on. If you’re working with bash, you need to know how to control the file system. To manage these files, you’ll need some bash commands like ls, stat, file, touch, find, chmod, and rsync. Moreover, there’s a special tool called exiftool that can help you to view and edit file information and metadata.
This article will provide a comprehensive overview of file information and metadata. In addition to that, I will also show you how to use those specified bash commands to manage file information and metadata.
What are File Information and Metadata?
File information and metadata are the property of a file that describes the file type, name, size, file owner, creation & modification date, file location, and tags. Depending on the file format, file properties can be visible, hidden, and read-only. You can use the file metadata such as file name, size, date, etc to sort or find files. Moreover, they are essential to manage a file’s access and permissions. That’s why knowing about file metadata is important to manage the file system effectively in bash.
Contents of File Information and Metadata
To see the contents of file metadata, you can simply use the stat command in bash. For that, run the command followed by the filename whose metadata you want to check from your terminal. Checkout the following image where I have shown the metadata of one of my system files file.txt:
Upon the execution of the stat file.txt command, you can see the following information about the file.txt:
- File Information:
- File: The name of the file (or directory).
- Size: The size of the file in bytes.
- Blocks: The number of 512-byte blocks allocated for the file.
- IO Block: The block size for I/O operations.
- Device and Inode Information:
- Device: The device identifier (major/minor).
- Inode: The inode number, a unique identifier for the file or directory.
- Links: The number of hard links to the file.
- Permission and Ownership Information:
- Access: The access permissions are in octal and symbolic format.
- Uid: The user ID (numeric and name) of the file owner.
- Gid: The group ID (numeric and name) of the filegroup.
- Timestamps:
- Access: The last access time.
- Modify: The last modification time.
- Change: The last change time (metadata change).
- Birth: The creation or birth time (if supported by the file system).
This is just a basic example, and the output may vary based on the file system and the options you use with the stat command.
How to Manage File Information and Metadata in Bash?
Managing file information and metadata is one of the most common tasks in bash. You can view file metadata, view hidden file information, modify metadata, look for a file according to metadata, change time stamps, permissions, and so on using various bash commands in bash. Let’s know how these bash commands work in detail:
A. Viewing Hidden File Information and Metadata
To view hidden file information and metadata in bash, use the ls -la
command. It gives you the details of all files and directories of the current directory including hidden files.
The information on all files, including hidden files, can be seen in this image. The files that start with a dot (.)
are the hidden files.
B. Displaying File Information and Metadata Using Exiftool
ExifTool is a powerful and versatile command-line utility for reading, writing, and editing metadata in various file types, including images, audio files, and documents. You can use Exiftool to view the metadata of a file. However, you have to install it first with the apt package manager. So to install exiftool run the following command:
sudo apt install libimage-exiftool-perl
This command installs the Exiftool with the apt package manager. Here, sudo apt install allows you to install the libimage-exiftool-perl which is basically exiftool to view file metadata.
Now run the following command to view the metadata of the file ‘file.txt’:
exiftool file.txt
You can see all the information of file.txt in this image after running the file with ExifTool.
Note: To know only the file type, you can also run file file.txt
in your terminal.
C. Adding and Removing Creator Name to File Metadata
Another modification you can do using the exiftool
is, you can edit file information like adding the creator name. To add the creator name to an image file named ‘image.png’, you can run this command:
exiftool -Creator="mou" image.png
The -Creator="mou"
command along with exiftool modifies the image.png file’s metadata tag named “Creator” and sets its value to “mou.”.
After running this command, with exiftool image.png
, you can see the output as:
As you can notice, the creator name “mou” is added successfully in the image.png metadata.
Note: You can remove the creator tag with exiftool too, for that use exiftool -Creator= image.png
command.
D. Changing File Timestamps
To change the access and modification timestamp of a file, you can execute the touch command using the -t
option. This command will modify the current time and date to your desired time and date. Now, execute the following command to modify the file access and modification timestamp:
touch -t 202211201530.00 image.png
The -t
(timestamp) option with the touch command sets the timestamp of the image.png file to November 20, 2022, 15:30:00. You can adjust the timestamp according to your needs.
This image illustrates that the modification time and access time are changed successfully.
Note: To restore the altered access and modification time to the current time, you can run touch image.png
.
E. Altering File Permissions
To change read (r), write (w), and execution (x) permissions among the user, group, and other owner groups you can use the chmod command. Now to give the execution permission of the image.png file to all (owner, group, and others), write the following command:
chmod a+x image.png
In this image, you can observe that each group obtains the execution privileges for the Image.png file.
F. Searching Files Based on Metadata
You can use the find command to search files based on metadata like file extension, name, or timestamp. For instance, finding all .txt files modified within the last 3 days:
find -name "*.txt" -mtime -3
- find: Searches files and directories.
- -name “*.txt”: Specifies that you are looking for files with names ending in “.txt”. The asterisk
(*)
is a wildcard character that searches for matched characters written after it. - -mtime -3: Sets the modification time criteria. In this case, it finds files that were modified within the last 3 days. The minus sign
(-)
before the number indicates “less than.”.
You can notice in this picture that the command shows all the modified (within the last 3 days) .txt files in the home directory.
G. Copying File Information and Metadata
The rsync command preserves file metadata by using a technique called delta-copying. This means that it only copies the differences between the source and destination files, rather than copying the entire file again. So, to copy a file named “file.txt” from the current “Home” directory to the “Downloads” directory preserving all information, metadata, and attributes, you can run:
rsync -a file.txt Downloads/
Here, the rsync
command will create the directory if the “Downloads” directory doesn’t exist. The -a
option stands for “archive” and is a combination of several other options, including -r
(recursive), -l
(copy symlinks as symlinks), -p
(preserve permissions), -t
(preserve modification times), and others. These options ensure that files preserving all metadata are copied to the correct location.
Conclusion
In conclusion, I have outlined the fundamentals of file information and metadata and its significance in bash. I also showed you how to manipulate file info and metadata with bash commands like how to display file info, modify metadata, modification and access time, permissions, look for a file, and so on. Learning how to do these things helps you to manage the bash file system better. Good luck to you!
People Also Ask
How to view, edit, or remove Metadata in macOS?
To view, select the image file first, then use Command+I
to get information. In the More Info section, you will see basic metadata. To edit, use additional software such as Adobe Lightroom or Bridge, or a command line tool such as ExifTool. To delete metadata, use the ‘Remove Location Info’ option under the ‘Image’ menu in the ‘Preview’ app.
How to view, edit, or remove Metadata in Linux?
To view metadata in Linux, you can just use the exif
or file
commands in the terminal. To edit or delete metadata, you’ll need to use extra software like ‘ExifTool’, a great command-line tool.
How do I view file properties in Linux?
To view file properties, you can use the ls command along with the -l
(long listing) option. It also shows all the permissions you have set for the file.
How do I see the properties of a directory in Linux?
To see the properties of a directory, type ls -ld
in the terminal. Moreover, you can run stat directory_name
to see the metadata of that specified directory.
What is the command to check metadata in Linux?
In Linux, you can use various commands to check metadata such as stat, file, exiftool, etc.
What’s the difference between ls and ls*?
The basic difference is that the ls
command lists files and directories of the current directory without extra details. Whereas, the ls*
lists files and directories with additional details (such as permissions, owner, size, and modification time) for each file or directory.
Related Articles
<< Go Back to Bash Files and Directories | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners
thank you, the instructions are simple to understand and follow.