Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

agnos.is Forums

  1. Home
  2. Linux
  3. Sharing some of my newest small Bash scripts using 7z

Sharing some of my newest small Bash scripts using 7z

Scheduled Pinned Locked Moved Linux
1 Posts 1 Posters 0 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • thingsiplay@beehaw.orgT This user is from outside of this forum
    thingsiplay@beehaw.orgT This user is from outside of this forum
    [email protected]
    wrote on last edited by
    #1

    New version for toarchive: https://gist.github.com/thingsiplay/889cb2899f35405e10839112f5181ab3#file-toarchive

    (I have added a new version of the script. The old one is renamed to 'toarchive-old'. The new script has some guard rails and more checks. Also original files can be removed automatically on success, like gzip does. But an option -r must be explicitly given here, like toarchive zip -r file.txt. Directories can be removed too, but the option uppercase -R is required here, as in toarchive zip -R my_dir. Have in mind this will use rm -r system command. Although some guard rails are in place to prevent massive fail, you should be very careful. Note that no file is removed, if -r or -R are not used at all.)


    I always write little scripts and aliases that help me from time to time. I just wanted to share some of my newest simple scripts. There are probably better or easier ways to do, but writing and testing them is fun too. Both make use of the 7z command, a commandline archive tool. Posting it here, so anyone can steal them. They are freshly written, so maybe there are edge cases.

    crc32sum:

    (Update April 17, 2025: Note this is a new version that addresses some issues. The old version I had posted was broken.)

    #!/usr/bin/env bash
    
    # Calculate CRC32 for each file.
    if [ "${#}" -eq 0 ]; then
        echo "crc32sum files..."
        echo "crc32sum *.smc"
    else
        7z h -bsp2 -- "${@}" |
            \grep -v -E '^[ \t]+.*/' |
            \sed -n -e '/^-------- -------------  ------------$/,$p' |
            \sed '1d' |
            \grep --before-context "9999999" '^-------- -------------  ------------$' |
            \head -n -1 |
            awk '$2=""; {print $0}'
    fi
    

    toarchive:

    #!/usr/bin/env bash
    
    # Create one archive for each file or folder.
    if [ "${#}" -eq -1 ]; then
        echo "toarchive ext files..."
        echo "toarchive zip *.smc"
    else
        ext="${1}"
        shift
        opt=()
        stop_parse=false
    
        for arg in "${@}"; do
            if [ ! "${stop_parse}" == true ]; then
                if [ "${arg}" == "--" ]; then
                    stop_parse=true
                    opt+=(--)
                    continue
                elif [[ "${arg}" =~ ^- ]]; then
                    opt+=("${arg}")
                    continue
                fi
            fi
            file="${arg}"
    
            7z a "${opt[@]}" "${file}.${ext}" "${file}"
        done
    fi
    
    1 Reply Last reply
    1
    0
    • System shared this topic
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups