bashoneliners's profile picture. A collection of practical or just pure awesome bash one-liner shell script tips with explanations

Bash One-Liners

@bashoneliners

A collection of practical or just pure awesome bash one-liner shell script tips with explanations

Download a URL with cookies set on the command line with curl: curl -b "session=$session" 'adventofcode.com/2023/day/18/in…'; tinyurl.com/yop33hdc #bash #linux


Print the lines of file2 that exist in file1: grep -xFf file1 file2; tinyurl.com/yttvg9ql #bash #linux


Extract regex capture groups using [[ and BASH_REMATCH: text='3 blue; 5 green'; [[ $text =~ ([0-9]+)" "(blue|green) ]] && { echo "count = ${BASH_REMATCH[1]}"; echo "color = ${BASH_REMATCH[2]}"; }; tinyurl.com/yn5ydgdq #bash #linux


Count the lines of each file extension in a list of files: git ls-files | xargs wc -l | awk -F ' +|\\.|/' '{ sumlines[$NF] += $2 } END { for (ext in sumlines) print ext, sumlines[ext] }'; tinyurl.com/yscjkqtr #bash #linux


Truncate a file or create an empty file: > /path/to/file; tinyurl.com/ymfyyrjc #bash #linux


Print the paths in $PATH sorted by line length: echo "${PATH//:/\\n}" | awk '{print length, $0}' | sort -n | cut -f2- -d' '; tinyurl.com/yu6otuwa #bash #linux


Print lines of a file that don't exist in another file, ignoring the first two fields: awk -F'|' 'NR == FNR { $1 = ""; $2 = ""; seen[$0]++ } NR != FNR { orig = $0; $1 = ""; $2 = ""; if (!seen[$0]) print orig }' first.txt second.txt; tinyurl.com/yplkg7pf #bash #linux


Use inverted conditions with the find command: find . -type f -not -name '*.mp3'; tinyurl.com/yl2p694u #bash #linux


Replace a pattern in a file in a portable way: f=/path/to/file; sed -e "s/pattern/replacement/" "$f" > "$f".bak && mv "$f".bak "$f"; tinyurl.com/yqgln2av #bash #linux


Generate a sequence of numbers using Brace Expansion: for i in {1..10}; do echo "$i"; done; tinyurl.com/ym63ea9y #bash #linux


Print the first 100 characters of a file: head -c 100 /path/to/file; tinyurl.com/yrd5u2f5 #bash #linux


Pretty-print JSON with Python: curl -s 'api.github.com/orgs/github/re…' | python -m json.tool; tinyurl.com/yn8flloh #bash #linux


Generate a sequence of numbers: seq 1 10; tinyurl.com/yrnkllyg #bash #linux


Send an HTTP POST request as if submitting an HTML form using curl: curl --data "title=recipe&text=steps123" example.com; tinyurl.com/yro87asp #bash #linux


Printing with jq multiple values in CSV or TSV formats: curl -s 'api.github.com/orgs/github/re…' | jq -r '.[] | [.id, .name, .stargazers_count] | @csv'; tinyurl.com/yujc6a95 #bash #linux


List docker log sizes: docker ps -qa | xargs docker inspect --format='{{.LogPath}}' | xargs sudo du -hl; tinyurl.com/ylo7cvdf #bash #linux


Display the number of connections per IP to port 80: while true; do clear; date; echo; echo "[Count] | [IP ADDR]"; echo "-------------------"; netstat -n | grep ':80\>' | awk '! /LISTEN/ {print $5}' | cut -d: -f1 | uniq -c; sleep 5; done; tinyurl.com/yo5dtctb #bash #linux


Move file and cd with one command: mv file dir/ && cd "$_" && pwd; tinyurl.com/yp8oz2rj #bash #linux


Check if a version string is in valid SemVer format: re_semver=...; perl -wln -e "/$re_semver/ or exit(1)" <<< "$version"; tinyurl.com/yt3eaowb #bash #linux


Print raw values with jq -r: echo '{"foo": "bar \" baz"}' | jq -r .foo; tinyurl.com/ytm64vdz #bash #linux


이 계정은 현재 아무도 팔로우하지 않습니다
Loading...

Something went wrong.


Something went wrong.