This is a short blog post to give you an idea how you can define your own custom date and time format for “ls -l” command in ASMCMD. If you use ASMCMD quite often, you might have been (at least once) in a situation where you wanted to format the date and time of “ls -l” command, but you did not find a way to do it.
If you did not know, in ASMCMD utility the date and time format for “ls -l” command is hardcoded. Possible formats are “MON DD YYYY” or “MON DD HH24:MI:SS”, based on file age. With RMAN we can use NLS_DATE_FORMAT environment variable to define our desired format, but that’s not the case with ASMCMD.
ASMCMD is a Perl utility that provides navigation of directories/files within ASM disk groups. Its modules are stored in $CRS_HOME/lib directory.
ASMCMD utility base module is asmcmdbase.pm.
The subroutine for listing files located in the base module is asmcmdbase_ls_process_file. You can see the logic how file dates are printed. We can notice that if the file is older than 6 months the format used is “MON DD YYYY”. If the file is newer, the format used is “MON DD HH24:MI:SS”. This subroutine is called (executed) for each file that needs to be printed in ASMCMD.
List entries $entry_info_ref->{‘mod_date’} and $entry_info_ref->{‘mod_time’} contain the data for both hardcoded formats. MOD_DATE if the files is older than 6 months and MOD_TIME if the file is newer.
In order to understand how $entry_info_ref->{‘mod_date’} and $entry_info_ref->{‘mod_date’} are defined we need to look at asmcmdshare.pm module which is the ASM command line interface (Shared Functionality Module).
Within this module, in subroutine asmcmdshare_get_file we can find the actual query statement used to get file information from V$ASM_FILE. We see that date format is hardcoded within the select statement.
Control ASMCMD date time format with environment variable.
By now, you’ve probably figure it out what you need to do in order to get your custom format in place. E.g. if you want to control ASMCMD date format with OS environment variable you just need very few lines of code.
Let’s assume we want to control the format with NLS_DATE_FORMAT_XX environemnt variable.
Add condition with default format to be used if environment variable is not set. In our case that would be ‘DD.MM.YYYY HH24:MI:SS’
Add additional column in the query to get the date formatted based on the environment variable definition.
Below the select statement you need to fetch the column into $file_info_ref under your own key.
As final step, in the main ASMCMD module, subroutine asmcmdbase_ls_process_file you need to overwrite $entry_info_ref->{‘date_print’} to be populated with previously defined custom key which holds the data formatted as per ‘NLS_DATE_FORMAT_XX’.