Skip to content

Instantly share code, notes, and snippets.

@d4z3x
Forked from pmarreck/split_file.bash
Created December 5, 2023 02:00
Show Gist options
  • Save d4z3x/fd96fa19eba3483f6842a937146bcb91 to your computer and use it in GitHub Desktop.
Save d4z3x/fd96fa19eba3483f6842a937146bcb91 to your computer and use it in GitHub Desktop.

Revisions

  1. @pmarreck pmarreck revised this gist May 12, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion split_file.bash
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ mkdir -p "$dir_name/$file_name"
    while IFS= read -r line
    do
    # Hash the line with sha256 and use it as the file name
    file_hash=$(echo -n "$line" | openssl dgst -sha256 | awk '{print $2}')
    file_hash=$(echo -n "$line" | cksum | awk '{print $1}')

    # Write the line to the new file
    echo "$line" > "$dir_name/$file_name/$file_hash.txt"
  2. @pmarreck pmarreck revised this gist May 12, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion split_file.bash
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    # Ensure a file name is provided
    if [ -z "$1" ]
    then
    echo "No file name provided. Usage: ./split_file.sh <filename>"
    echo "No file name provided. Usage: ./split_file.bash <filename>"
    exit 1
    fi

  3. @pmarreck pmarreck created this gist May 12, 2023.
    26 changes: 26 additions & 0 deletions split_file.bash
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    #!/usr/bin/env bash

    # Ensure a file name is provided
    if [ -z "$1" ]
    then
    echo "No file name provided. Usage: ./split_file.sh <filename>"
    exit 1
    fi

    # Extract the directory and base name from the file
    dir_name=$(dirname "$1")
    base_name=$(basename "$1")
    file_name="${base_name%.*}"

    # Create directory to store the split files
    mkdir -p "$dir_name/$file_name"

    # Read the file line by line
    while IFS= read -r line
    do
    # Hash the line with sha256 and use it as the file name
    file_hash=$(echo -n "$line" | openssl dgst -sha256 | awk '{print $2}')

    # Write the line to the new file
    echo "$line" > "$dir_name/$file_name/$file_hash.txt"
    done < "$1"