Dump all AWS IAM users/roles to a Terraform file for editing / reusing in another environment$ echo iamg iamgm iamgp iamip iamp iampa iamr iamrp iamu iamup | AWS_PROFILE=myprofile xargs -n1 terraforming ExplanationAmazon Web Services (AWS) use a collection "IAM" resources to create Users and related objects in the system. This oneliner scrapes all the relevant info and puts it into Terraform. This lets us audit our users-groups. And, it lets us re-use them in another environment! |
Organise image by portrait and landscape$ mkdir "portraits"; mkdir "landscapes"; for f in ./*.jpg; do WIDTH=$(identify -format "%w" "$f")> /dev/null; HEIGHT=$(identify -format "%h" "$f")> /dev/null; if [[ "$HEIGHT" > "$WIDTH" ]]; then mv "$f" portraits/ ; else mv "$f" landscapes/ ; fi; done Explanation
LimitationsThis relies on the This does not check for square images, although it could be easily extended to see if |
Create a txt files with 10000 rows$ for FILE in *.full ; do split -l 100000 $FILE; mv -f xaa `echo "$FILE" | cut -d'.' -f1`.txt; rm -f x*; done Explanationfor loop will go trough every file that is finished with ".full" split that in file in files of 100000 rows. Rename the first one as input name but deleting the extension and adding ".txt" as extension. As last stem it will delete the rest of split files. |
List open processes ordered by it's number of open files$ ps -ef |awk '{ print $2 }' \ |tail -n +2 \ |while read pid; do echo "$pid $(lsof -p $pid |wc -l)"; done \ |sort -r -n -k 2 \ |while read pid count; do echo "$pid $count $(ps -o command= -p $pid)"; done ExplanationCombines ps, lsof, and sort in the ways you might expect to produce the intended outcome. |
Remove all container from an specific network (docker)$ docker ps -a -f network=$NETWORK --format='{{.ID}}' | xargs docker rm -f Explanation
|
Up all docker services as detached mode over all immediate subdirectories$ for dir in $(ls -d */); do eval $(cd $PWD/$dir && docker-compose up -d && cd ..); done; ExplanationSupposing that you are in a directory that contains many subdirectories with a docker-compose file each one and instead of up one by one manually you want run all at time, well this is a helpful command for this purpose |
Find and replace string inside specific files$ grep -ril '$SEARCH_PATTERN' src | sed -i 's/$FIND_PATTERN/$REPLACE_PATTERN/g' ExplanationThis command search for files that contain and an specific string and then find a pattern on those files and replace it |
Puppet/Bash: test compare json objects.$ unless => "client_remote=\"$(curl localhost:9200/_cluster/settings | python -c \"import json,sys;obj=json.load(sys.stdin);print(obj['persistent']['search']['remote'])\")\"; new_remote=\"$( echo $persistent_json | python -c \"import json,sys;obj=json.load(sys.stdin);print(obj['persistent']['search']['remote'])\")\"; [ \"$client_remote\" = \"$new_remote\" ]", ExplanationOne json object provided by puppet dictionary the other grabbed from Elasticsearch rest API. Only run command if these don't match. Had issues getting jq to sort properly so used python. |
Print wifi access points sorted by signal$ iw dev IFACE scan | egrep "SSID|signal" | awk -F ":" '{print $2}' | sed 'N;s/\n/:/' | sort Explanation
|
Kill a process running on port 8080$ lsof -i :8080 | awk '{l=$2} END {print l}' | xargs kill ExplanationAs before, we're using The only notable diffirence from the command listed above is the use of awk to also complete the |
Delete all untagged Docker images$ docker images -q -f dangling=true | xargs --no-run-if-empty --delim='\n' docker rmi ExplanationIt does not return a failing exit code if there are no images removed. It should always succeed unless there was an actual problem removing a Docker image. LimitationsThis only works in the GNU version of |
Take values from a list (file) and search them on another file$ for ITEM in `cat values_to_search.txt`; do (egrep $ITEM full_values_list.txt && echo $ITEM found) | grep "found" >> exit_FOUND.txt; done ExplanationThis line :) searches values taken from a file (values_to_search.txt) by scanning a full file values list . If value found, it is added on a new file exit_FOUND.txt. Alternatively, we can search for values from the list 1 which does NOT exists on the list 2, as bellow: for ITEM in LimitationsNo limitations |
Delete all untagged Docker images$ docker rmi $(docker images -f "dangling=true" -q) Explanation
|
Have script run itself in a virtual terminal$ tty >/dev/null || { urxvt -e /bin/sh -c "tty >/tmp/proc$$; while test x; do sleep 1; done" & while test ! -f /tmp/proc$$; do sleep .1; done; FN=$(cat /tmp/proc$$); rm /tmp/proc$$; exec >$FN 2>$FN <$FN; } Explanation
|
Big CSV > batches > JSON array > CURL POST data with sleep$ cat post-list.csv | split -l 30 - --filter='jq -R . | jq --slurp -c .' | xargs -d "\n" -I % sh -c 'curl -H "Content-Type: application/json" -X POST -d '"'"'{"type":1,"entries":%}'"'"' http://127.0.0.1:8080/purge-something && sleep 30' Explanationpost-list.csv contains list of URLs in my example.
LimitationsYou need
See also https://stedolan.github.io/jq/manual/ I suspect the input file ( |
List all packages with at least a class defined in a JAR file$ jar tf "$1" | grep '/.*\.class$' | xargs dirname | sort -u | tr / . ExplanationThe From the output, we get only the paths that contain a classfile ( LimitationsWill only exhaustively list the packages with a defined class for languages that require packages to map to the directory structure (e.g.: Java does, Scala doesn't). If this convention is respected, the command will output an exhaustive list of packages nonetheless. |
Output an arbitrary number of open TCP or UDP ports in an arbitrary range$ comm -23 <(seq "$FROM" "$TO") <(ss -tan | awk '{print $4}' | cut -d':' -f2 | grep "[0-9]\{1,5\}" | sort | uniq) | shuf | head -n "$HOWMANY" ExplanationOriginally published (by me) on unix.stackexchange.com.
The first file is the range of ports that we can select from. The second file is the sorted list of ports, that we obtain by calling the Now we have a sorted list of open ports, that we can ExampleGrab the three random open ports in the private range (49152-65535)
could return for example
Notes
|
Source without circular reference$ [ ! "${LIB}" ] && ( readonly LIB; . "${ $( cd $( dirname $0 ) && pwd ) }/<path_to>/LIB.sh" ) ExplanationSource |
Ternary conditional clause$ [ test_statement ] && ( then_statement ) || ( else_statement ); ExplanationThe |
Get executed script's current working directory$ CWD=$(cd "$(dirname "$0")" && pwd) ExplanationWill return excuting script's current working directory, wherever Bash executes the script containing this line. |
Random Git Commit$ git commit -m "$(w3m whatthecommit.com | head -n 1)" ExplanationThis will commit a message pulled from What the Commit.
LimitationsThis requires you to have |
Blackhole ru zone$ echo "address=/ru/0.0.0.0" | sudo tee /etc/NetworkManager/dnsmasq.d/dnsmasq-ru-blackhole.conf && sudo systemctl restart network-manager ExplanationIt creates You might use |
Remove new lines from files and folders$ rename 's/[\r\n]//g' * ExplanationThis will search all files and folders in the current directory for any with a new line character in them and remove the new line out of the file/folder. |
Retrieve dropped connections from firewalld journaling$ sudo journalctl -b | grep -o "PROTO=.*" | sed -r 's/(PROTO|SPT|DPT|LEN)=//g' | awk '{print $1, $3}' | sort | uniq -c ExplanationWe take the output of Limitations
|
Kill a process running on port 8080$ lsof -i :8080 | awk 'NR > 1 {print $2}' | xargs --no-run-if-empty kill Explanation
We use LimitationsThe |