{"id":21971283,"url":"https://github.com/eldada/command-examples","last_synced_at":"2025-04-28T11:42:16.793Z","repository":{"id":44385173,"uuid":"156664145","full_name":"eldada/command-examples","owner":"eldada","description":"A collection of useful commands with various tools","archived":false,"fork":false,"pushed_at":"2025-04-22T06:15:05.000Z","size":115,"stargazers_count":9,"open_issues_count":0,"forks_count":11,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-22T07:41:33.378Z","etag":null,"topics":["bash","command-line","hacktoberfest","linux","shell"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eldada.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-11-08T07:01:43.000Z","updated_at":"2025-04-22T06:15:09.000Z","dependencies_parsed_at":"2024-04-14T12:31:44.964Z","dependency_job_id":"86a4cff6-fecf-4610-b12a-32305eb39425","html_url":"https://github.com/eldada/command-examples","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eldada%2Fcommand-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eldada%2Fcommand-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eldada%2Fcommand-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eldada%2Fcommand-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eldada","download_url":"https://codeload.github.com/eldada/command-examples/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251306955,"owners_count":21568358,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","command-line","hacktoberfest","linux","shell"],"created_at":"2024-11-29T14:49:52.086Z","updated_at":"2025-04-28T11:42:16.784Z","avatar_url":"https://github.com/eldada.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Commands example\n\nThis is a repository with a collection of useful commands, scripts and examples for easy copy -\u003e paste\n\n# Table of contents\n\n* [Linux](#linux)\n  * [Examples](#examples)\n  * [The proc directory](#the-proc-directory)\n  * [Screen](#screen)\n  * [Sysbench](#sysbench)\n  * [Apache Bench](#apache-bench)\n  * [Load generator](#load-generator)\n* [Git](#git)\n* [Java](#java)\n* [Docker](#docker)\n  * [Tools](#tools)\n  * [My Dockerfiles](#my-dockerfiles)\n* [Artifactory](#artifactory)\n* [Matrix](#matrix)\n* [Contribute](#contribute)\n\n## Linux\n\n### Examples\n\n* Clear memory cache\n```shell script\nsync \u0026\u0026 echo 3 | sudo tee /proc/sys/vm/drop_caches\n```\n\n* Create a self-signed SSL key and certificate\n```shell script\nmkdir -p certs/my_com\nopenssl req -nodes -x509 -newkey rsa:4096 -keyout certs/my_com/my_com.key -out certs/my_com/my_com.crt -days 356 -subj \"/C=US/ST=California/L=SantaClara/O=IT/CN=localhost\"\n```\n\n* Create binary files with random content\n```shell script\n# Just one file (1mb)\ndd if=/dev/urandom of=file bs=1024 count=1024\n\n# Create 10 files of size ~10MB\nfor a in {0..9}; do \\\n  echo ${a}; \\\n  dd if=/dev/urandom of=file.${a} bs=10240 count=1024; \\\ndone\n```\n\n* Test connection to remote `host:port` (check port being opened without using `netcat` or other tools)\n```shell script\n# Check if port 8080 is open on remote\nbash -c \"\u003c/dev/tcp/remote/8080\" 2\u003e/dev/null\n[ $? -eq 0 ] \u0026\u0026 echo \"Port 8080 on host 'remote' is open\"\n```\n\n* Suppress `Terminated` message from the `kill` on a background process by waiting for it with `wait` and directing the stderr output to `/dev/null`. This is from in this [stackoverflow answer](https://stackoverflow.com/a/5722874/1300730).\n```shell script\n# Call the kill command\nkill ${PID}\nwait $! 2\u003e/dev/null\n```\n\n* **curl** variables\u003cbr\u003e\nThe `curl` command has the ability to provide a lot of information about the transfer. See [curl man page](https://curl.haxx.se/docs/manpage.html).\u003cbr\u003e\nSearch for `--write-out`.\u003cbr\u003e\nSee all supported variables in [curl.format.txt](files/curl.format.txt)\n```shell script\n# Example for getting http response code (variable http_code)\ncurl -o /dev/null -s --write-out '%{http_code}' https://curl.haxx.se\n\n# Example for one-liner printout of several connection time parameters\ncurl -w \"\\ndnslookup: %{time_namelookup} \\nconnect: %{time_connect} \\nappconnect: %{time_appconnect} \\npretransfer: %{time_pretransfer} \\nredirect: %{time_redirect} \\nstarttransfer: %{time_starttransfer} \\n---------\\ntotal: %{time_total} \\nsize: %{size_download}\\n\" \\\n        -so /dev/null https://curl.haxx.se\n        \n# Example for printing all variables and their values by using an external file with the format\ncurl -o /dev/null -s --write-out '@files/curl.format.txt' https://curl.haxx.se\n```\n\n* Single binary `curl`\n```shell script\n# Get the archive, extract (notice the xjf parameter to tar) and copy.\nwget -O curl.tar.bz2 http://www.magicermine.com/demos/curl/curl/curl-7.30.0.ermine.tar.bz2 \u0026\u0026 \\\n    tar xjf curl.tar.bz2 \u0026\u0026 \\\n    cp curl-7.30.0.ermine/curl.ermine curl \u0026\u0026 \\\n    ./curl --help\n```\n\n* Single static binaries\nTaken from this cool [static-binaries](https://github.com/yunchih/static-binaries/) repository\n```shell script\n# tcpdump\ncurl -O https://raw.githubusercontent.com/yunchih/static-binaries/master/tcpdump\n```\n* Single static binary `vi`\n```shell script\n# vi (vim)\ncurl -OL https://eldada.jfrog.io/artifactory/tools/x86_64/vi.tar.gz\n```\n\n* Single static binary `jq` (Linux). Look in https://stedolan.github.io/jq/download/ for additional flavors\n```shell script\n# jq\ncurl -OL https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64\n```\n\n* Get **http code** using **wget** (without **curl**)\u003cbr\u003e\nIn cases where **curl** is not available, use **wget** to get the http code returned from an HTTP endpoint\n```shell script\nwget --spider -S -T 2 www.jfrog.org 2\u003e\u00261 | grep \"^  HTTP/\" | awk '{print $2}' | tail -1\n```\n\n* Poor man's `top` shell scripts (in Linux only!). Good for when `top` is not installed\u003cbr\u003e\nGet CPU and memory usage by processes on the current host. Also useful in Linux based Docker containers\u003cbr\u003e\n  * Using data from `/proc` [top.sh script](scripts/top.sh)\u003cbr\u003e \n  * Using data from `ps -eo` [top-ps.sh script](scripts/top-ps.sh)\n\n* Process info (in Linux only!)\u003cbr\u003e\nTo get process info using its PID or search string: Command line, environment variables. Use [procInfo.sh](scripts/procInfo.sh).\n\n* Add file to WAR file [addFileToWar.sh](scripts/addFileToWar.sh)\n\n### The proc directory\n\nThe `/proc` file system has all the information about the running processes. See full description in the [proc man page](https://man7.org/linux/man-pages/man5/proc.5.html).\n\n* Get current processes running (a simple alternative to `ps` in case it's missing)\n```shell script\nfor a in $(ls -d /proc/*/); do if [[ -f $a/exe ]]; then ls -l ${a}exe; fi; done\n```\n\n* Get a process command line (see usage in [procInfo.sh](scripts/procInfo.sh))\n```shell script\n# Assume PID is the process ID you are looking at\ncat /proc/${PID}/cmdline | tr '\\0' ' '\n# or\ncat /proc/${PID}/cmdline | sed -z 's/$/ /g'\n```\n\n* Get a process environment variables (see usage in [procInfo.sh](scripts/procInfo.sh))\n```shell script\n# Assume PID is the process ID you are looking at\ncat /proc/${PID}/environ | tr '\\0' '\\n'\n# or\ncat /proc/${PID}/environ | sed -z 's/$/\\n/g'\n```\n\n* Get load average from disk instead of command\n```shell script\ncat /proc/loadavg | awk '{print $1 \", \" $2 \", \" $3}'\n```\n\n* Get top 10 processes IDs and names sorted with highest time waiting for disk IO (Aggregated block I/O delays, measured in clock ticks)\n```shell script\ncut -d\" \" -f 1,2,42 /proc/[0-9]*/stat | sort -n -k 3 | tail -10\n```\n\n### Screen\n\n* Full source in this [gist](https://gist.github.com/jctosta/af918e1618682638aa82)\n* `screen` [MacOS man page](https://ss64.com/osx/screen.html) and [bash man page](https://ss64.com/bash/screen.html)\n* The `screen` command quick reference\n```shell script\n# Start a new session with session name\nscreen -S \u003csession_name\u003e\n\n# List running screens\nscreen -ls\n\n# Attach to a running session\nscreen -x\n\n# Attach to a running session with name\nscreen -r \u003csession_name\u003e\n\n# Detach a running session\nscreen -d \u003csession_name\u003e\n```\n\n* Screen commands are prefixed by an escape key, by default Ctrl-a (that's Control-a, sometimes written ^a). To send a literal Ctrl-a to the programs in screen, use Ctrl-a a. This is useful when when working with screen within screen. For example Ctrl-a a n will move screen to a new window on the screen within screen. \n\n| Description             | Command                                 |\n|-------------------------|-----------------------------------------|\n| Exit and close session  | `Ctrl-d` or `exit`                      |\n| Detach current session  | `Ctrl-a d`                              |\n| Detach and logout (quick exit) | `Ctrl-a D D`                     |\n| Kill current window     | `Ctrl-a k`                              |\n| Exit screen             | `Ctrl-a :` quit or exit all of the programs in screen|\n| Force-exit screen       | `Ctrl-a C-\\` (not recommended)          |\n\n* Help\n\n| Description | Command                       |\n|-------------|-------------------------------|\n| See help    | `Ctrl-a ?` (Lists keybindings)|\n\n### Sysbench\n\n**Sysbench** is a mutli-purpose benchmark that features tests for CPU, memory, I/O, and even database performance testing.\u003cbr\u003e\nSee full content for this section in [linuxconfig.org's how to benchmark your linux system](https://linuxconfig.org/how-to-benchmark-your-linux-system#h7-sysbench).\n\n* Installation (Debian/Ubuntu)\n```shell script\nsudo apt install sysbench\n```\n* CPU benchmark\n```shell script\nsysbench --test=cpu run\n```\n* Memory benchmark\n```shell script\nsysbench --test=memory run\n```\n* I/O benchmark\n```shell script\nsysbench --test=fileio --file-test-mode=seqwr run\n```\n\n### Apache Bench\n\nFrom the [Apache HTTP server benchmarking tool](http://httpd.apache.org/docs/2.4/programs/ab.html) page: \"`ab` is a tool for benchmarking your Apache Hypertext Transfer Protocol (HTTP) server.\"\n\n```shell script\n# A simple benchmarking of a web server. Running 100 requests with up to 10 concurrent requests\nab -n 100 -c 10 http://www.jfrog.com/\n```\n\n### Load generator\n\nA simple [createLoad.sh](scripts/createLoad.sh) script to create disk IO and CPU load in the current environment. This script just creates and deletes files in a temp directory which strains the CPU and disk IO.\u003cbr\u003e\n**WARNING:** Running this script with many threads can bring a system to a halt or even crash it. USE WITH CARE!\n```shell script\n./createLoad.sh --threads 10\n```\n\n## Git\n\n* Rebasing a branch on master\n```shell script\n# Update local copy of master\ngit checkout master\ngit pull\n\n# Rebase the branch on the updated master\ngit checkout my-branch\ngit rebase master\n\n# Rebase and squash\ngit rebase master -i\n\n# If problems are found, follow on screen instructions to resolve and complete the rebase.\n```\n\n* Resetting a fork with upstream. **WARNING:** This will override **any** local changes in your fork!\n```shell script\ngit remote add upstream /url/to/original/repo\ngit fetch upstream\ngit checkout master\ngit reset --hard upstream/master  \ngit push origin master --force \n```\n\n* Add `Signed-off-by` line by the committer at the end of the commit log message.\n```shell script\ngit commit -s -m \"Your commit message\"\n```\n\n## Java\n\nSome useful commands for debugging a `java` process\n```shell script\n# Go to the java/bin directory\ncd ${JAVA_HOME}/bin\n\n# Get your java process id\nPID=$(ps -ef | grep java | grep -v grep | awk '{print $2}')\n\n# Get JVM native memory usage\n# For this, you need your java process to run with the the -XX:NativeMemoryTracking=summary parameter\n./jcmd ${PID} VM.native_memory summary\n\n# Get all JVM info\n./jinfo ${PID}\n\n# Get JVM flags for a java process\n./jinfo -flags ${PID}\n\n# Get JVM heap info \n./jcmd ${PID} GC.heap_info\n\n# Get JVM Metaspace info\n./jcmd ${PID} VM.metaspace\n\n# Trigger a full GC\n./jcmd ${PID} GC.run\n\n# Java heap memory histogram\n./jmap -histo ${PID}\n \n```\n\n## Docker\n\n* Allow a user to run docker commands without sudo\n```shell script\nsudo usermod -aG docker user\n# IMPORTANT: Log out and back in after this change!\n```\n\n* See what Docker is using\n```shell script\ndocker system df\n```\n\n* Prune Docker unused resources\n```shell script\n# Prune system\ndocker system prune\n\n# Remove all unused Docker images\ndocker system prune -a\n\n# Prune only parts\ndocker image/container/volume/network prune\n```\n\n* Remove dangling volumes\n```shell script\ndocker volume rm $(docker volume ls -f dangling=true -q)\n```\n\n* Quit an interactive session without closing it:\n```\n# Ctrl + p + q (order is important)\n```\n\n* Attach back to it\n```shell script\ndocker attach \u003ccontainer-id\u003e\n```\n\n* Save a Docker image to be loaded in another computer\n```shell script\n# Save\ndocker save -o ~/the.img the-image:tag\n\n# Load into another Docker engine\ndocker load -i ~/the.img\n```\n\n* Connect to Docker VM on Mac\n```shell script\nscreen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty\n# Ctrl +A +D to exit\n```\n\n* Connect to Rancher Desktop VM on Mac\n```shell script\nLIMA_HOME=\"${HOME}/Library/Application Support/rancher-desktop/lima\" \"/Applications/Rancher Desktop.app/Contents/Resources/resources/darwin/lima/bin/limactl\" shell 0\n```\n\n* Remove `none` images (usually leftover failed docker builds)\n```shell script\ndocker images | grep none | awk '{print $3}' | xargs docker rmi\n```\n\n* Using [dive](https://github.com/wagoodman/dive) to analyse a Docker image\n```shell script\n# Must pull the image before analysis\ndocker pull redis:latest\n\n# Run using dive Docker image\ndocker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock wagoodman/dive:latest redis:latest\n```\n\n* Adding health checks for containers that check tcp port being opened without using netcat or other tools in your image\n```shell script\n# Check if port 8081 is open\nbash -c \"\u003c/dev/tcp/localhost/8081\" 2\u003e/dev/null\n[ $? -eq 0 ] \u0026\u0026 echo \"Port 8081 on localhost is open\"\n```\n\n### Tools\n\nA collection of useful Docker tools\n* A simple terminal UI for Docker and docker-compose: [lazydocker](https://github.com/jesseduffield/lazydocker)\n* A web based UI for local and remote Docker: [Portainer](https://www.portainer.io/)\n* Analyse a Docker image with [dive](https://github.com/wagoodman/dive)\n\n### My Dockerfiles\n\nA few `Dockerfile`s I use in my work\n* An Ubuntu with added tools and no root: [Dockerfile-ubuntu-with-tools](Dockerfiles/Dockerfile-ubuntu-with-tools)\n```shell\n# For a local build\ndocker build -f Dockerfile-ubuntu-with-tools -t eldada.jfrog.io/docker/ubuntu-with-tools:24.04 .\n\n# Multi arch build and push\n# If needed, create a buildx builder and use it\ndocker buildx create --platform linux/arm64,linux/amd64 --name build-amd64-arm64\ndocker buildx use build-amd64-arm64\n\n# Multi arch build and push\ndocker buildx build --platform linux/arm64,linux/amd64 -f Dockerfile-ubuntu-with-tools -t eldada.jfrog.io/docker/ubuntu-with-tools:24.04 --push .\n```\n\n* An Alpine with added tools: [Dockerfile-alpine-with-tools](Dockerfiles/Dockerfile-alpine-with-tools)\n```shell\n# For a local build\ndocker build -f Dockerfile-alpine-with-tools -t eldada.jfrog.io/docker/alpine-with-tools:3.21.0 .\n\n# Multi arch build and push\n# If needed, create a buildx builder and use it\ndocker buildx create --platform linux/arm64,linux/amd64 --name build-amd64-arm64\ndocker buildx use build-amd64-arm64\n\n# Multi arch build and push\ndocker buildx build --platform linux/arm64,linux/amd64 -f Dockerfile-alpine-with-tools -t eldada.jfrog.io/docker/alpine-with-tools:3.21.0 --push .\n```\n\n## Artifactory\n\nSee Artifactory related scripts and examples in [artifactory](artifactory)\n\n## Matrix\nA command line effect of the Matrix (the movie) text\n```shell\nwhile true; do\n  echo $LINES $COLUMNS $((RANDOM % $COLUMNS)) $(printf \"\\U$((RANDOM % 500))\"); sleep 0.04; \ndone | awk '{a[$3]=0; for (x in a){o=a[x];a[x]=a[x]+1; printf \"\\033[%s;%sH\\033[2;32m%s\",o,x,$4; printf \"\\033[%s;%sH\\033[1;37m%s\\033[0;0H\", a[x],x,$4; if (a[x]\u003e=$1){a[x]=0;}}}'\n```\n\n## Contribute\n\nContributing is more than welcome with a pull request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feldada%2Fcommand-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feldada%2Fcommand-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feldada%2Fcommand-examples/lists"}