7
$\begingroup$

When we ls -tlra in bash, we get filename information, size of files, date last modified, etc. Is there a way to create a Mathematica function which generates a Dataset of all of this information?

Some key functions here: FileNames[], FileDate[], FileSize[]. I'm not sure how to get information about file permissions, but that is less important.

$\endgroup$
12
$\begingroup$

Build your own

Here I create a Dataset of files, taking advantage of the "Rules" option for FileDate. You can add your own information relevant for files or directories in the respective sections after DirectoryQ. Just as a playful example I added FileHash which may slows down things considerably.

lstlra[dir_] := Dataset[
  <|(* For all files and directories  *)
     "FileName" -> FileNameTake[#],
     "Type" -> FileType[#],
     FileDate[#, "Rules"],
     If[
      DirectoryQ[#],
      <|(* Directory only *)
       |>,
      <|(* Files only *)
       "FileSize" -> FileSize[#],
       "MD5-Hash" -> FileHash[#]
       |>
      ]
     |> & /@ FileNames[All, dir]
  ][SortBy["Modification"]]

enter image description here


Using File Information

After the answer by @swish, was very nice to learn about Information applied to File. Here I just change the Head to and Association (using Apply @@) to allow you to put your own stuff. Unfortunately it has too much information, more than you possible need.

lstlra2[dir_] := Dataset[
  <|
     Association @@ Information[File[#]],
     If[
      DirectoryQ[#],
      <|(* Directory only *)
       |>,
      <|(* Files only *)
       "MD5-Hash" -> FileHash[#]
       |>
      ]
     |> & /@ FileNames[All, dir]
  ][SortBy["LastModificationDate"]]

Minimal

lstlra3[d_]:=SortBy[Dataset[Information[File[#]][[1]]&/@FileNames[d]],"LastModificationDate"]
$\endgroup$
  • 1
    $\begingroup$ Is there a way to have the final results sorted by date modified (this is also the behavior of ls -tlra I believe). $\endgroup$ – George Jul 19 '19 at 19:40
  • $\begingroup$ @George I added a minimalistic version, and all version sort as requested. Did this answer your question? $\endgroup$ – rhermans Jul 20 '19 at 10:15
11
$\begingroup$

Try something like that

props = {"CreationDate", "UnixPermissionsString", "ByteCount"};
ls[dir_] := 
 Dataset@FileSystemMap[
   AssociationThread[props -> Information[File@#, props]] &, dir]
$\endgroup$
  • 1
    $\begingroup$ Nice one! (+1) Thanks for introducing me to Information applied to File. $\endgroup$ – rhermans Jul 19 '19 at 19:49
  • $\begingroup$ It gives me this error Information::nonopt: Options expected (instead of props) beyond position 1 in Information[File[/usr/local/Wolfram/Mathematica/11.3/Executables/math],props]. An option must be a rule or a list of rules. $\endgroup$ – Fortsaint Jul 20 '19 at 13:10
  • $\begingroup$ @Fortsaint I believe Information was substantially updated in version 12. $\endgroup$ – swish Jul 20 '19 at 20:51
2
$\begingroup$

Why not interpreting the stdout of ls itself

ls`tlra@path_:=ReadString["!ls -tlra "<>path] //
               StringSplit /@ Rest@StringSplit[#,"\n"]& //
               StringRiffle /@ Append[TakeList[#,{1,1,1,1,1,3}],#~Drop~8]& /@ #& //
               AssociationThread[
                  {"Permissions" ,"ID","Owner","Group","Size","CreationDate","Name"}->#
               ]& /@ #& // Dataset

enter image description here

$\endgroup$
  • $\begingroup$ Because then this becomes a platform specific answer... $\endgroup$ – rhermans Jul 21 '19 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.