Intersting shell scripts

By rbosaz, 28 March, 2024

Byobu is a GPLv3 open source text-based window manager and terminal multiplexer.

byobu

For loop to unzip a bunch of zips files in the same directory.

#!/bin/bash

#Stop shell splitting on spaces
ORIGINAL_IFS=$IFS #internal field separator
IFS=$(echo -n "\n")
for FILENAME in $(ls *.zip)
do
    echo "Unzipping $FILENAME..."
    unzip -o $FILENAME
done
IFS=$ORIGINAL_IFS

Determine size on disk of directories.

for FOLDER in $(ls); do du -sh --apparent-size $FOLDER; done 

The following will sort it by size.

for FOLDER in $(ls); do du -sh –apparent-size $FOLDER; done | sort -h

Determine size of specefic folder.

du -sh --apparent-size <folder_name>

Make list of all files in a directory, recursively.

find . -type f > ../filenames.txt

Given text document containing a listing of directories and filenames (paths). One can use the following commands to search for filename types. This will output a count of files with filenames ending with .docx.

cat ../filenames.txt | grep -i .docx$ | wc -l

Give the same document as the preceding command. One can use the following commands to search for filename types. This will output a count of files with filenames ending with .docx where the path begins with <directory_name. 

cat ../filenames.txt | grep ^./<directory_name>/ | grep -i .docx$ | wc -l

To search CSV files containing "find this,"  use the below command. The -i argument in this command makes the search case insensitive. The */*.csv argument is the path to search tell grep to open every folder, then each file within those folders that ends in .csv, and search for the "find this" keyword.

grep -i "find this" */*.csv

If you have docker and docker-compose installed, start an Ubuntu container as follows.

sudo docker run -it ubuntu:latest bash

Remove all stopped containers at once.

sudo docker container prune

Display all running Docker containers.

sudo docker ps

Kill a running docker container.

docker kill <container_name>

Start an Ubuntu docker container by mount a volume for persistent data. Mount type will be volume.

sudo docker run -it --mount type=volume,src=test-data,dst=/mnt
ubuntu:latest bash

Display docker volumes.

sudo docker volume ls

Remove docker volumes. Note volumes can only be removed if containers have bee completely removed.

sudo docker volume rm <volume_name>

Start an Kali docker container by mount a volume for persistent data. Mount type will be bind.

sudo docker run -it --mount type=bind,src=/host/folder,dst=/container/folder
kalilinux/kali-rolling bash

Start a docker container passing database login environment variables to be used by the container. 

docker run -it -e DB_USER=root -e DB_PASSWORD=yourefired ubuntu:latest bash

Verify environmental variables in container.

echo $DB_PASSWORD

Start jupyter docker container with forwarded ports to host. On the host use localhost/127.0.01 with host port to connect to container port.

docker run -p host_port:container_port jupyter/scipy-notebook:latest

Docker can create a lot of files. To free up space quickly run the following command. Since this command doesn’t delete volumes, it won’t delete any of your important data.

docker system prune -a

Services defined in the same docker-compose.yaml file will be part of the same docker network.

To drop into a container's bash, run the following command.

docker-compose run --rm <name_of_container> bash