awk and cksum

March 11, 2011 at 22:33:08
Specs: Linux, Pentium
I would like to list all my files like the following displaying

filename, size, cksum

displaying each one on a single line.

Using awk, I can do this much so far:

$ find Commands.txt -type f -printf "%f,%s\n" | awk -F "," '
{cmd="'"`cksum`"'" $1; cmd2="'"`awk '// {print $2}'`"'";for (i=1; i<NF; ++i) printf "%s,%d,%s\n", $1,$2, cmd|cmd2 }'

However, it does not produces any output, nor it shows any error!

Could someone help please?


See More: awk and cksum

Report •

March 11, 2011 at 23:20:59
Solved it somewhat:

$ find Commands.txt -type f -printf "%f,%s\n" | awk -F "," '
{for (i=1; i<NF; ++i) printf "%s,%d,", $1,$2; system("cksum " $1 ) }'

Report •

March 13, 2011 at 23:10:34
I take it Commands.txt is a directory?

I don't see how your version will work if you have directories more than one level deep. That is because using the printf in the find command strips off the basename of the file. It needs to be there if the chksum command is to execute properly. Let me know if you have any questions:


find Commands.txt -type f -print|xargs ls -l|while read f1 f2 f3 f4 fsize f6 f7 filename
   # get filename without full path
   fname=$(basename $filename)
   # get check sum using full path filename
   # $1 is the check sum
   set - $(cksum $filename)

   printf "%s %d  %d\n" $fname $fsize $1

Report •

March 13, 2011 at 23:18:01

Hi there, thanks.

Command.txt is just a file, I was testing the commend with it only. Actually the command looks like:

find *.* -type f -printf "%f,%s\n" | awk -F "," ' {for (i=1; i<NF; ++i) printf "%s,%d,%s", $1,$2; system("cksum " $1 ) }'

Report •

Related Solutions

March 13, 2011 at 23:22:56
My answer hasn't changed much. In *nix, when using the find command, to find all files from the current directory, use:

find . -type f .....(rest of command)

Executing this:

find *.* -type ....

says to find files that have at least one period in the name.

I still don't think your version gets the right cksum value.

Report •

March 13, 2011 at 23:32:33
No, that's actually the intention, to limit the find command within present directory and to get only the file names with a period at least. That's why I have not used find . -type etc.

Here follows my output:


Report •

March 13, 2011 at 23:46:39
If I like to do it for all the files, I can think of even an easier and better way of doing it than my previous command line:

for i in `find . -type f`;do cksum $i | awk -F " " '{printf "%s,%d,%d\n", $3,$2,$1}';done

Since cksum returns the checksum, size and filename in that order, just tweaking the order with awk and comma separation can achieve the same.

This produces the following output:

[Full Filename],[size],[checksum value]

Report •

Ask Question