Computer Usage Notes
pdf and epub notes
The Adobe Acrobat tablet app writes my notes and underlinings to the PDF file itself. So, my notes persist when I copy the PDF file elsewhere.
Not so with the notes I make in the Google Play Reader. The changes aren’t saved to the file itself. It must be stored as metadata in the reader app (and synced with the cloud).
Manually install a deb package
To install foo.deb:
sudo dpkg -i foo.deb
If it fails due to dependency problems:
sudo apt-get install -f
(Thanks to AskUbuntu)
Install a font (like Inconsolata)
sudo apt install fonts-inconsolata
Number of Pages in PDFs
Useful when trying to calculate the number of slides in a bunch of lectures:
ls -1 *.pdf | xargs -n 1 pdfinfo | awknum '/Pages/{print $1}' | awksum
# Aliases for awknum and awksum
alias -g awknum="awk -v FPAT='[0-9]+(\\\\.[0-9]+)?'"
# Sum of the first number on each line.
alias -g awksum="awknum '{sum+=\$1}END{print sum}'"
PDF from Text
enscript file.txt --output=- | ps2pdf - > file.pdf
Useful when I need to create a simple one-page PDF with meta details for each folder so that when I collate all the PDFs, I can see the details for each one in order.
With Pandoc:
echo 'Foo test' | pandoc -o foo.pdf
Annotate a PDF on Ubuntu
To write text or doodle on a PDF and have those changes persist, use xournal.
Shell
Diff Output of Two Commands
Convoluted way to check if a file has duplicate lines (you could just do sort | uniq -d
):
diff <(sort foo.txt) <(sort -u foo.txt)
Zip
zip -r foo.zip foo-directory
To uncompress a .Z file (which in my case has usually been a non-gzip file):
tar zxvf foo.Z
Don’t use gzip -d foo.Z
. It gives some weird result.
Pass Input to Two Commands
I want lines with “Foo” to be displayed before the rest. Also, those two sections should be sorted within themselves.
Baz
Foo: Ball
Yoboyz
Foo: Apple
Bar
should show as
Foo: Apple
Foo: Baz
Bar
Baz
Yoboyz
How to do this?
One trick: zsh allows you to redirect output to multiple files (or processes). This uses the MULTIOS option, which is turned on by default.
cat input.txt > >(grep Foo | sort) > >(grep -v Foo | sort)
However, the output is non-deterministic. On some runs, I get the Foo section on top; on some other runs, I get it on the bottom.
Sort on an Intermediate Data Structure
Lesson: Whenever you want to sort on some special feature that you will compute from each line (or paragraph or section), create an intermediate stream with that value explicit, sort it, and then delete that value.
Here you go:
awk '$0=($0 ~ /Foo/)"\t"$0' sort-on-pass.txt | sort -k1,1nr -k2,2 | cut -f 2-
SSH: Port forwarding
First, do:
ssh -L 31337:cs527-vm0.cs.purdue.edu:8000 mc08.cs.purdue.edu -l <username>
Then open localhost:31337 in your browser. No need for FoxyProxy.
GnuPG or GPG
Tutorial: https://hashrocket.com/blog/posts/encryption-with-gpg-a-story-really-a-tutorial
# --armor makes the gpg output ASCII.
echo "hello world" | gpg --armor --encrypt -r gohanpra@gmail.com > ~/test/hello-world.gpg
gpg --output ~/test/hello-world-gpg-output.txt ~/test/hello-world.gpg
# Export public key.
gpg --export --armor foo@gmail.com
# Export private key.
gpg --export-secret-keys --armor foo@gmail.com
Emacs tutorial: https://www.masteringemacs.org/article/keeping-secrets-in-emacs-gnupg-auth-sources
To read a GPG-encrypted file in Emacs, just visit it and enter your passphrase.
To create a GPG file in Emacs, just save it as foo.gpg
.
To create a new encrypted version of a file, use : e
in Dired (or ; e
with Evil).
Since I have only one private key in my keyring, Emacs knows which key to use. I guess that if you have more than one key, you have to mention that at the top of the file.
Note: You will have to enter your passphrase every time you open a file (after closing it).
TODO: How to import your private key on a new computer? (Otherwise all your encrypted data will be lost once you lose or format your current hard drive.)
gpg-agent
Installed gpg-agent
.
# Add to .zshrc.
GPG_TTY=$(tty)
export GPG_TTY
Test: It doesn’t work right now even after I do gpg-agent --daemon
. I’m guessing that’s because I have gpg v1.4 and gpg-agent v2.1.
For example,
gpg -vvv --use-agent -d hello-world.txt.gpg
asked me for my passphrase twice.
Test: However, when I evaluated the code printed by gpg-agent
, then it works. It asks me for my passphrase the first time and then works without it the next time.
You need to do eval
because gpg-agent --daemon
prints code to export the value of GPG_AGENT_INFO
. Source: https://unix.stackexchange.com/a/407849/
Test: Check if it gets started automatically on bootup. – It doesn’t.
Test: Check if you need to add eval $(gpg-agent --daemon)
to .xsessionrc
. But won’t I be starting gpg-agent twice if gpg starts it too? – Seems to work fine for now.
Test: How to make Emacs know about the agent? Even when gpg-agent worked in the terminal, Emacs still kept asking me for a passphrase. I’m guessing it’s because it doesn’t know about the agent since I started it before the agent set its environment variables. – Update: It worked without trouble once I changed my .xsessionrc
.
Test: Emacs - if I type in the password from within Emacs, then it asks me again when I open a new file. But if I type in the password outside, it doesn’t ask me again. [Not true. It didn’t ask me again now when I closed and opened a gpg file. Maybe it’s because the agent timed out last time?]
Test: Continued – it’s been 6 hours since I typed in the password and now I have to type it again. Probably a time-out.
JSON to CSV
Use jq
(h/t http://bigdatums.net/2017/09/30/convert-json-to-csv-with-jq/):
echo '{"key1":"val1", "myarray":["abc", "def", "ghi"]}' | jq '[.key1, .myarray[0], .myarray[1]] | @csv'
To process an array of objects:
# The quotes are necessary, AFAICS.
echo '[{"yo": {"boyz": 1}, "foo": 3}, {"yo": {"boyz": 2}, "foo": 4}]' | jq -r '.[] | [.yo.boyz, .foo] | @csv'
Firefox
“To play audio, you may need to install the required PulseAudio software” error sometimes when I play videos (after upgrading to FF58).
Fix: Run start-pulseaudio-x11
when starting your X session.
Bash
Command without spaces:
{cat,foo.txt}|head
cat${IFS%?}foo.txt
Change Time Zone from the Command-line
$ sudo timedatectl set-timezone Asia/Kolkata
Another way:
$ sudo dpkg-reconfigure tzdata
Send in Stdin where a File is expected
$ echo yo boyz | foo /dev/stdin
Get Parts of Speech (POS) from a Sentence
Use the Stanford POS tagger.
Get verbs:
$ ./stanford-postagger.sh models/english-left3words-distsim.tagger foo.txt | awk '{for(i=1;i<=NF;i++) if($i ~ /_V/) print $i}'
ISO
To burn an ISO to a USB, use usb-creator-gtk
.
pyenv
[2022-11-06 Sun] Install using https://github.com/pyenv/pyenv-installer
curl https://pyenv.run | bash
comments powered by Disqus