Skip to content

Instantly share code, notes, and snippets.

@soltrinox
Last active February 21, 2024 12:02
Show Gist options
  • Save soltrinox/bb873f212d6d71156d6c7ee5d34375f2 to your computer and use it in GitHub Desktop.
Save soltrinox/bb873f212d6d71156d6c7ee5d34375f2 to your computer and use it in GitHub Desktop.
BASH: Read file contents
# Example using xxd to read a byte at a specific offset from a binary file
xxd -p -l1 -s 100 file.bin  # Read one byte (-l1) at offset 100 (-s) from file.bin

# Using xxd to handle hex offsets and output in decimal format
echo $((16#$(xxd -p -l1 -s $((16#FC)) file.bin)))  # Convert the output of xxd from hex to decimal

# Example using dd to dump raw data
dd if=file.bin seek=$((16#FC)) bs=1 count=5 status=none  # Dump 5 bytes from file.bin starting at hex offset FC

# Using dd with file descriptor to read bytes one by one
exec 3< input_file  # Open input_file and assign file descriptor 3
while true; do
    dd bs=1 count=1 <&3 > this_byte  # Read one byte from file descriptor 3
    if ! [ -s this_byte ]; then      # Check if this_byte file size is zero
        break                        # Exit the loop if file size is zero
    fi
    # Code using the this_byte file goes here
done

# Using od to process each byte of the file
od -bv input_file | while read a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16; do
    # Skip $a0 as it's the address, and process each byte
    for byte_val in "$a1" "$a2" "$a3" "$a4" "$a5" "$a6" "$a7" "$a8" "$a9" "$a10" "$a11" "$a12" "$a13" "$a14" "$a15" "$a16"; do
        if [ "$byte_val" = "" ]; then
            break  # Exit the loop if no more bytes
        fi
        # Code using the $byte_val value goes here
        echo -e "\0$byte_val\c"  # Example usage of $byte_val
    done
done

# Example using Perl for byte-wise processing
printf '%s%s%s' 'ăâé' | LC_ALL=C.UTF-8 perl -Mopen=locale -ne '
    BEGIN { $/ = \1 }  # Set input record separator to 1 byte
    printf "%s\n", $_; # Print each byte
'
# Extracting byte range using dd
dd if=yourfile ibs=1 skip=200 count=100

# Using od to read bytes and output in ASCII
od -j 1024 -N 16 -a /bin/sh

# Piping with head and tail to extract byte ranges
head ... file | tail ...

# Python script to extract a byte range
python -c 'f=open("myfile.txt","rb");f.seek(100);print(f.read(100));f.close()'

# Using a compiled C program (fastcat.c) to extract a byte range
./fastcat -f=100 -c=10000 /root/document

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment