0

Time stamp example:
20211018_14:54:54.0596445490_Mon

Ubuntu 20.04.3 command below works, displaying

  • Directories and
  • Files and
  • Hidden files with:
    Permissions, Time_Day, f or d, Path/fileName with
find . -printf "%M %TY%Tm%Td_%TT_%Ta %Y%p\n" |sort -k2 ;

Sort by time, column 2.
Most recent files at bottom, 4 examples:

-rw-r--r-- 20211001_13:02:16.0000000000_Fri f./Bash/awkCommnads.txt   
-rw-r--r-- 20211013_06:22:12.0000000000_Wed f./.HiddenFile_1.txt   
drwxr-xr-x 20211018_14:51:42.1712136500_Mon d.   
drwxr-xr-x 20211018_14:54:54.0596445490_Mon d./Bash   

Said differently,
How to get 32 byte md5sum checksum for each file,
on Left side of above List that is sorted by time?
Example:

123456789T123456789w123456789Y12 -rw-r--r-- 20211001_13:02:16.0000000000_Fri f./Bash/awkCommnads.txt   

Once md5 works then sha512sum.

Tip for testing:

setterm -linewrap off ; find . -printf "%M %TY%Tm%Td_%TT_%Ta %Y%p\n" |sort -k2 ; tput smam ;  

For testing, one line per record, No Line wrap:

setterm -linewrap off ; Commands... ; tput smam ;

tput smam ; = linewrap on

Again,
With a time sorted List, How to insert a checksum for each file?

--


joseph
  • 1
  • 1
  • find . -printf "%M %TY%Tm%Td_%TT_%Ta %Y%p\n" -type f -exec md5sum + | sort -k2 ; Untested, untried but hopefuly not unloved. – Silbee Oct 18 '21 at 21:44
  • The command __ find . -printf "%M %TY%Tm%Td_%TT_%Ta %Y%p\n" -type f -exec md5sum + | sort -k2 ; __ Gave error = find: missing argument to `-exec' __ on Ubuntu 20.04.3 – joseph Oct 18 '21 at 22:47

1 Answers1

1

You need to run md5sum for each file (or rather for each regular file). You run arbitrary commands from find with -exec. The problem is find . -type f -exec md5sum {} \; will print (i.e. md5sum will print) also the pathname and a trailing newline, and sometimes a leading backslash. You need to get rid of them before you proceed to -printf.

A straightforward way is with cut and tr. To execute md5sum … | cut … | tr … inside find, you need a shell there. Executing many processes per file is costly. You cannot use -exec … {} + because you need md5sum and -printf to take turns. We will save few processes if we manage to make the shell do the job of cut and tr.

For a single file (e.g. /etc/fstab) you can print its md5sum in the desired format, still without cut or tr, like this:

sh -c '
   exec 2>/dev/null
   sum="$(md5sum <"$1")" || sum="????????????????????????????????"
   printf "%s " "${sum%% *}"
' sh /etc/fstab

With performance in mind we hope printf is a builtin in your sh. The above command is designed to show question marks if md5sum fails. Useful links:

Now let's build the command into your find. Like this:

find . -exec sh -c '
   exec 2>/dev/null
   sum="$(md5sum <"$1")" || sum="????????????????????????????????"
   printf "%s " "${sum%% *}"
' find-sh {} \; -printf '%M %TY%Tm%Td_%TT_%Ta %Y%p\n' | sort -k3

Notes:

  • sort -k2 became sort -k3.

  • The command will try to run md5sum for files of any type. E.g. for a directory md5sum will fail and you will get question marks, this is acceptable. On the other hand, for a fifo md5sum may endlessly wait for data, this you don't want. Consider restricting find to regular files (simply add -type f as the first test) or fixing the command, so our -exec happens for regular files only:

    find . \( -type f -exec sh -c '
        exec 2>/dev/null
        sum="$(md5sum <"$1")" || sum="????????????????????????????????"
        printf "%s " "${sum%% *}"
    ' find-sh {} \; -o -printf '-------------------------------- ' \) \
    -printf '%M %TY%Tm%Td_%TT_%Ta %Y%p\n' | sort -k3
    

    The sequences of ? or - characters are of the length of any md5sum, so they align nicely.

  • Newlines in pathnames will confuse your sort. If you can, use null-terminated strings. E.g. with GNU sort in my Kubuntu:

    find … -printf '…\0' | sort -z … | tr '\0' '\n'
    
Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202
  • Above answer = 262 characters from _find . \( -type_to_sort -k3_ Above answer: 1. Detects if file modified, time changed. Good. 2. Detects if hidden file was modified, time changed. Good. 3. Sorts files, latest time to List bottom. Good. 4. Inserts a Hash, a checksum md5sum for Files & Hidden. Good. 5. Gives no Dir. hash. Gives 32 minus signs like --- for Directories. Disrelish. Ideal is to also have a Hash, a checksum md5sum, for Directories. For clarity, No Hash for Directory means __ -------------------------------- drwxr-xr-x 20211018_14:51:42.1712136500_Mon d./Comm __ – joseph Oct 19 '21 at 19:15
  • @joseph In Linux [you cannot read a directory as a byte stream](https://unix.stackexchange.com/a/156020/108618), so you cannot compute md5sum from it. You can compute a checksum from *something else*. Your now deleted "answer" suggests that for directories you want a checksum from the output of `ls` or so, but *your question does not specify this*. How to compute a sum differently for directories and non-directories is an issue orthogonal to what you originally asked. IMO asking a new question is better than [creating a chameleon](https://meta.stackexchange.com/q/43478/355310). – Kamil Maciorowski Oct 19 '21 at 20:11
  • How to run answer as a 1 Line command? Above answer = 262 characters. – joseph Oct 19 '21 at 20:23
  • @joseph What difference does it make? Written in one line it still will be `find … | sort …`, only far less readable; and it will take about the same number of characters. – Kamil Maciorowski Oct 19 '21 at 20:28
  • The difference a 1 Line makes is Less keystrokes. Answer has 6 Lines. A one Line answer would save 5 keystrokes. Less effort, Less keystrokes, to move 1 Line about relative to other commands. The 6 Line answer broke when converting it to 1 Line. – joseph Oct 19 '21 at 21:09
  • @joseph Are you telling me you *typed* my code? To really save keystrokes, learn how to copy and paste. – Kamil Maciorowski Oct 20 '21 at 20:55