How to use the command 'mdls' (with examples)
- Osx
- December 17, 2024
The mdls
command in macOS is a powerful tool that provides the ability to display metadata attributes for files. These attributes can include information such as the file’s creation date, modification date, size, type, and a variety of other file-specific metadata that macOS can track. By utilizing the mdls
command, users can delve deeply into what information is stored about their files beyond the simple file data itself.
Use case 1: Display the list of metadata attributes for a file
Code:
mdls path/to/file
Motivation:
The motivation behind using this command is to gain insights about all the metadata attributes that are available for a specific file. Understanding these attributes can be crucial for various tasks such as organizing files, performing audits, or even debugging applications that interact with file systems. When you need comprehensive information about a file, listing all its metadata attributes can give you a complete picture of what is tracked by the system.
Explanation:
mdls
: This is the core command used to interact with the metadata of a file.path/to/file
: This argument specifies the exact path to the file whose metadata attributes you want to display. By providing the correct path, you allow the command to access the file and retrieve its metadata information.
The command works by extracting every available piece of metadata for the specified file. This could include standard attributes like file size and date modified, as well as macOS-specific metadata like “kMDItemKind” (often the file type).
Example output:
_kMDItemDisplayNameWithExtensions = "document.txt"
kMDItemContentCreationDate = 2023-01-14 15:23:42 +0000
kMDItemContentModificationDate = 2023-09-22 09:12:50 +0000
kMDItemContentType = "public.plain-text"
kMDItemFSName = "document.txt"
kMDItemFSSize = 2048
This output lists each attribute available for the file document.txt
, along with its corresponding value, providing a wealth of information about the file’s properties and how it is recognized by the system.
Use case 2: Display a specific metadata attribute
Code:
mdls -name attribute path/to/file
Motivation:
Sometimes a user or developer may only be interested in a specific piece of metadata rather than a full listing. For example, you might be conducting a search for files created on a specific date, or you may need to know just the type of content for processing or reporting purposes. By extracting a particular attribute, it becomes possible to script or automate tasks that require specific metadata without excess information, making it more efficient and focused.
Explanation:
mdls
: This is again the primary command used to extract metadata from files.-name attribute
: The-name
flag denotes that you seek a specific attribute. “attribute” should be replaced with the actual metadata attribute you are interested in. This filters the output to show only the specified attribute.path/to/file
: This tells the command which specific file you want to examine for the given attribute. It’s crucial to provide the exact path to ensure the retrieval of accurate information.
This precise targeting reduces data noise and allows one to focus on relevant metadata, simplifying tasks such as automation scripting where only certain file properties are relevant.
Example output:
kMDItemFSSize = 2048
This output indicates that only the file system size (kMDItemFSSize) of the file is of interest, and thus only this particular attribute is displayed, showing a file size of 2048 bytes.
Conclusion:
The mdls
command in macOS is an invaluable tool for anyone needing to access and utilize metadata from files. By understanding how to leverage mdls
to display all available metadata attributes or to focus on specific ones, users can enhance their ability to manage, audit, and organize their files efficiently. Whether you’re a developer creating scripts and applications or simply looking to better organize your files, mastering mdls
can significantly enhance your file management capabilities.