etopiei's blog

A blog about minimalism, programming, productivity and happiness.
All Posts - RSS Feed

Misc. Useful Bash Commands

At various times I have noted down some handy bash scripts that I constantly have to DuckDuckGo for, but no more, I'm putting them here so I never lose them, and may update this periodically. Hopefully these can help other people too, and I will try and keep them with good descriptions so they are findable.

How to: Remove Whitespace From Filenames

 $ for x in *.md; do mv "$x" "${x// /}"; done; 
This is a nice one liner that comes in handy quite a bit. I originally used this to fix up my ournal entries names to be consistent for future scripting. (replace the *.md) with a glob that matches the files you want to operate on.

How to: Count Words in a Group of Text Files (plus get an average)

$ for x in *.md; do cat "$x" | wc -w; done | awk '{s+=$1} END {print s}'
$ for x in *.md; do cat "$x" | wc -w; done | awk '{s+=$1; t+=1} END {print s/t}'
Both these commands are quite succinct, I used this also to get some information about my journalling habits. Initally the wc part was very apparent to me, and some vague recollection from an OS class in Uni told me to look to awk for the answer, but it took a couple of minutes to construct, so I have preserved it here to save myself some future work. (Again replace *.md with the necessary glob)

How to: Convert Markdown to PDF

$ pandoc -o document.pdf 

How to: Extract specific pages of a PDF into a shiny new PDF

$ pdftk full.pdf cat 12-15 output outfile_p12-15.pdf 

How to: Merge PDF's (Done Two Ways)

$ qpdf --empty --pages 1.pdf 2.pdf -- combined.pdf 
$ pdfunite 1.pdf 2.pdf combined.pdf 
I personally prefer option 2 for brevity.
(Wow, just noticing there are a lot of document based stuff in this post.)

Create an Admin MYSQL User

> CREATE USER 'username'@'host' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'username'@'host';
I have had to google this so many times, I saved myself a bit more time by writing it done somewhere safe.

How to: Format a C file nicely

$ clang-format -i 
The need for this arose when I copied some code from the internet and the formatting was all over the place due to tab/spaces problems. I then found this handy command that rectified the situation beautifully. I'm sure this is well known, but it was a life saver for me and I wish I knew about it earlier.

How to: Start SSH Agent and add Key

$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/KEY
I can't count how many times I have visited the GitHub page with the instructions for setting up SSH keys and these are copied verbatim, but it's good to have these in the same place.

How to: Create a new sudo user

$ adduser 'username' 
$ usermod -aG sudo 'username'
Nice simple one, but gets the job done.

That might be all for now, but I may add to this a little bit as I go.

< Previous Post Next Post >

Post History