I have a bash script to find out all non empty files under some directory. currently it only print the file name if found. I would like to add one more line to print first 32 bytes in hex format.
#!/bin/sh
files=$(find /data/ -type f ! -empty)
for f in $files;
do
if [ -f "$f" ]; then
tr -d '\000' <$f | tr -c '\000' '\n' | grep -q -m 1 ^ || echo $f
fi
done
I try to add one more "&& xxd -g 1 -l 32 $f" at the end but it doesn't work!
CodePudding user response:
Get the first 32 chars from the file:
dd if=so.bash ibs=32 count=1 2>/dev/null | od -h
dd
gets the first 32 charsod -h
prints them in hex format
You could do it with xxd as well
xxd -l 32 $f
- where
$f
is the file
#!/bin/bash
files=$(find /data/ -type f ! -empty)
for f in $files
do
if [ -f "$f" ]; then
tr -d '\000' <"$f" | tr -c '\000' '\n' | grep -q -m 1 ^ || echo $f
xxd -l 32 "$f"
echo ""
fi
done
- the
echo ""
is to have an empty line between each file to split the output.
CodePudding user response:
Suggesting one line gawk
script:
gawk 'BEGINFILE{print FILENAME;system("xxd -l 32 "FILENAME)}' $(find /data/ -type f ! -empty)
Sample output from my test:
./input.1.txt
00000000: 7870 746f 2d31 302e 3231 2e33 302e 7461 xpto-10.21.30.ta
00000010: 722e 787a 0d0a 7870 746f 2d31 312e 3230 r.xz..xpto-11.20
./input.2.txt
00000000: 3132 3334 3536 3738 3930 0d0a 3132 3334 1234567890..1234
00000010: 3536 3738 3930 0d0a 3132 3334 3536 3738 567890..12345678
./input.3.txt
00000000: 3030 3030 3030 3030 3a20 3331 3332 2033 00000000: 3132 3
00000010: 3333 3420 3335 3336 2033 3733 3820 3339 334 3536 3738 39
./input.4.txt
00000000: 776f 7264 330d 0a77 6f72 6433 0d0a 776f word3..word3..wo
00000010: 7264 370d 0a77 6f72 6438 0d0a 776f 7264 rd7..word8..word
./junk.txt
00000000: 7072 7c7c 7c48 454c 4c53 5445 4e7c 7c41 pr|||HELLSTEN||A
00000010: 5249 7c7c 7c7c 4143 5449 5645 7c32 3030 RI||||ACTIVE|200