Skip to content

Bunch of handy scripts and one-liners to help with various tasks

License

Notifications You must be signed in to change notification settings

a-karah/bash-scripts-collection

Repository files navigation

One-liners:

Miscellaneous

same as prepare-c-files-for-makefile.sh 1 2 3 4

foo@bar:~$ ls -1 <path to src folder> | sed -z 's/\n/ \\\n/g'
ft_atoi.c \
ft_bzero.c \
ft_calloc.c \
...

download release builds from github using curl and wget 5

curl -s https://api.github.com/repos/<ogranization-name>/<repo-name>/releases/latest \
| grep "browser_download_url.*zip" \
| cut -d : -f 2,3 \
| tr -d \" \
| wget -qi -

show sorted used space 6

foo@bar:~$ du -h <folder path> | sort -h
3.2M	./libs/glfw/docs/html
5.5M	./libs/glfw
 15M	./libs/glm/doc/api
 18M	./libs/glm/doc
 23M	./libs/glm
 35M	./libs
 39M	.

foo@bar:~$ du -sh -- * | sort -h
 36K	LICENSE
288K	include
536K	template
764K	assets
 35M	libs

create folder in each subfolder 7

for dir in */; do mkdir -- "$dir/<folder-name>"; done

Regex

Match

Grep8

foo@bar:~$ grep -HEn '^SRCS' Makefile
Makefile:26:SRCS := ft_strlen.cpp

Grep + awk

Useful when need to find match and quickly open in code editor

foo@bar:~$ grep -HEn 'main' src/main.cpp | awk -F: '{print $1":"$2" "$3}'
src/main.cpp:3 int      main(int ac, char **av)

Perl9

foo@bar:~$ perl -ne 'print if /^SRCS/' Makefile
SRCS := ft_strlen.cpp

Search and replace

Vim

:%s/<match>/<replace>/g
Backreference: \0 or &

References

Pure sh bible

Pure bash bible

Bash handbook

Bash Pitfalls

TDD Bash

The Bash Hackers Wiki

Quick tutorial

Shell Scripting Tutorial

Idempotent Bash Scripts

Footnotes

  1. https://superuser.com/questions/951905/is-there-a-way-to-echo-a-blackslash-followed-by-newline-in-bash

  2. https://stackoverflow.com/questions/27658675/how-to-remove-last-n-characters-from-a-string-in-bash

  3. https://stackoverflow.com/questions/30993056/get-full-path-to-file-from-argument

  4. https://askubuntu.com/questions/1400937/how-can-i-get-the-path-of-directory-in-which-the-command-is-running

  5. https://gist.github.com/steinwaywhw/a4cd19cda655b8249d908261a62687f8

  6. https://unix.stackexchange.com/questions/4681/how-do-you-sort-du-output-by-size

  7. https://unix.stackexchange.com/questions/61907/how-do-i-create-a-directory-in-all-subdirectories

  8. https://stackoverflow.com/questions/8105685/show-filename-and-line-number-in-grep-output

  9. https://www.rexegg.com/regex-perl-one-liners.html

About

Bunch of handy scripts and one-liners to help with various tasks

Topics

Resources

License

Stars

Watchers

Forks

Languages