bash - animeshtrivedi/notes GitHub Wiki

Execute sudo where you do not have to become root

sudo sh -c '/usr/bin/echo 3 > /proc/sys/vm/drop_caches'

Enumerate devices for fio

from=1
to=46
dev_str=""
for (( i = $from; i < $to; i++)) do 
        echo $i
        dev_str+="/dev/nvme${i}n1:"
done
final=${dev_str::-1}
echo $final 

Print tab in bash echo

echo $'\t'"I am $USER"

Echo tab characters in bash script

make docx from PDF

https://stackoverflow.com/questions/52277264/convert-doc-to-docx-using-soffice-not-working

soffice --headless --convert-to docx test.pdf 
Error: no export filter for /home/atr/Downloads/rocksdb-benchmark-tuning-guide-on-xeon.docx found, aborting.
Error: no export filter

Just open in the office.

Make a tar archive

tar -czvf filename.tar.gz /path/to/dir1 dir2 dir3 

extract in the /tmp folder

tar -xzvf projects.tar.gz -C /tmp/

RegEx

# match lines containing numbers 
grep -E -i [0-9]+
# that also contain blk keyword 

https://www.cyberciti.biz/faq/grep-regular-expressions/

Multi-line comments

: ' 
Here 
it 
is 
'

SSH tunnel

.ssh/config example: Both keys needs (first, and second) to be on the local machine

Host host 
	Hostname a.b.c.d 
	ProxyJump JumpSrv 
	User atr2
	IdentityFile ~/.ssh/second 

Host JumpSrv 
	HostName w.x.y.z
	User atr1
	IdentityFile ~/.ssh/first 
	IdentitiesOnly yes

Note: make sure to use the right key extension id_rsa not id_rsa.pub otherwise cryptic messages.

debug1: Trying private key: id_rsa
Load key "id_rsa": error in libcrypto
debug2: we did not send a packet, disable method
debug3: authmethod_lookup password

Typos in the commands

$ sudo /home/animesh.trivedi/src/ubdsrv/ublk add -t loop –f /dev/nullb0 -q 16 d 512
start dev 0 daemon failed, ret -1
$ sudo /home/animesh.trivedi/src/ubdsrv/ublk add -t loop -f /dev/nullb0 -q 16 d 512
dev id 0: nr_hw_queues 16 queue_depth 128 block size 512 dev_capacity 67108864

https://www.babelstone.co.uk/Unicode/whatisit.html

Writing/Piping a file and terminal output

# all output and show it 
SomeCommand 2>&1 | tee log.txt

#all to a file 
SomeCommand &>./log.txt 

#discard stderr 
SomeCommand 2>/dev/null

Preserving alias in sudo

https://askubuntu.com/questions/22037/aliases-not-available-when-using-sudo

alias sudo='sudo '

From the bash manual:

Aliases allow a string to be substituted for a word when it is used as the first word of a simple command. The shell maintains a list of aliases that may be set and unset with the alias and unalias builtin commands.

The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias. The characters ‘/’, ‘$’, ‘`’, ‘=’ and any of the shell metacharacters or quoting characters listed above may not appear in an alias name. The replacement text may contain any valid shell input, including shell metacharacters. The first word of the replacement text is tested for aliases, but a word that is identical to an alias being expanded is not expanded a second time. This means that one may alias ls to "ls -F", for instance, and Bash does not try to recursively expand the replacement text. If the last character of the alias value is a space or tab character, then the next command word following the alias is also checked for alias expansion.

(Emphasis mine). Bash only checks the first word of a command for an alias, any words after that are not checked. That means in a command like sudo ll, only the first word (sudo) is checked by bash for an alias, ll is ignored. We can tell bash to check the next word after the alias (i.e sudo) by adding a space to the end of the alias value.

Mount a remote machine's fs

sshfs atr@localhost:/home/atr/ ./vm-qemu7777/ -p 7777

preserving sudo environment

sudo -E env "PATH=$PATH" <command> [arguments]
sudo env "PATH=$PATH" <command> [arguments]

Convert SVGs to PDF in a batch with file looping

$ sudo apt-get install librsvg2-bin
$ rsvg-convert -f pdf -o t.pdf t.svg

in a loop

for f in *; do rsvg-convert -f pdf -o ${f%%.*}.pdf ${f%%.*}.svg; done

https://superuser.com/questions/381125/how-do-i-convert-an-svg-to-a-pdf-on-linux

bash file looping without extension

for f in *; do echo ${f%%.*}; done

https://stackoverflow.com/questions/7119223/file-name-without-extension-in-bash-for-loop

Convert PNGs printscreens to a PDF

convert *.png file.pdf 

https://askubuntu.com/questions/158093/how-do-i-convert-a-png-to-pdf-with-only-the-default-packages-on-11-10

Eliminate all free space lines

awk 'NF' filename 

https://stackoverflow.com/questions/16414410/delete-empty-lines-using-sed

print every Nth line

lets say 3rd

awk 'NR % 3 == 0' file.txt

https://unix.stackexchange.com/questions/369181/printing-every-nth-line-out-of-a-large-file-into-a-new-file/369220

file patterns search from one file to another

grep -f inputfile.txt inputfile2.txt 

https://stackoverflow.com/questions/21287389/how-to-find-words-from-one-file-in-another-file

Look up file in which package

$ dpkg -S /bin/ls
coreutils: /bin/ls

Key management

https://www.redhat.com/sysadmin/manage-multiple-ssh-key-pairs

https://czarpino.medium.com/how-to-tell-git-which-ssh-key-to-use-c8574fb243fd

Rotate images

convert input.jpg -rotate 180 out.jpg

https://unix.stackexchange.com/questions/365592/how-to-rotate-a-set-of-pictures-from-the-command-line

JPEG to PDF

convert -compress jpeg *.jpg my-jpegs.pdf

https://stackoverflow.com/questions/4234164/bash-convert-to-pdf/16325857

Security error about authorization: https://askubuntu.com/questions/1081895/trouble-with-batch-conversion-of-png-to-pdf-using-convert

Comment out this line: <policy domain="coder" rights="none" pattern="PDF" /> in /etc/ImageMagick-6/policy.xml

Striping the first pages from a pdf

for i in *pdf ; do pdftk "$i" cat 2-end output "trimmed/$i" ; done

https://askubuntu.com/questions/146375/remove-only-1st-page-from-a-lot-of-pdf-files

Check if a port is in use

https://www.cyberciti.biz/faq/unix-linux-check-if-port-is-in-use-command/

sudo lsof -i -P -n | grep LISTEN 

PDF rotate and compress

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf
  • -dPDFSETTINGS=/screen lower quality, smaller size. (72 dpi)
  • -dPDFSETTINGS=/ebook for better quality, but slightly larger pdfs. (150 dpi)
  • -dPDFSETTINGS=/prepress output similar to Acrobat Distiller "Prepress Optimized" setting (300 dpi)
  • -dPDFSETTINGS=/printer selects output similar to the Acrobat Distiller "Print Optimized" setting (300 dpi)
  • -dPDFSETTINGS=/default selects output intended to be useful across a wide variety of uses, possibly at thee expense of a larger output file https://askubuntu.com/questions/113544/how-can-i-reduce-the-file-size-of-a-scanned-pdf-file

Rotating:

First 9 pages

pdftk xxx.pdf cat 1-9north output xxx2.pdf

https://makandracards.com/makandra/1487-rotate-a-pdf-under-ubuntu-linux

Global search and replace

find vector/ -type f -exec sed -i 's/BitVectorHelper.setValidityBit(validityBuffer, thisIndex, from.isSet(fromIndex));/setValidityBit(thisIndex, from.isSet(fromIndex));/g' {} \;
find /path/to/files -type f -exec sed -i 's/oldstring/new string/g' {} \;

https://stackoverflow.com/questions/15402770/how-to-grep-and-replace

Modifies the time for every file. Hence, some IDE or build systems might get confused.

Pass arguments to xargs

cat items | xargs -I % git clone --mirror %

https://unix.stackexchange.com/questions/3593/using-xargs-with-input-from-a-file

Reset a hung ssh session

~.

https://www.cyberciti.biz/faq/openssh-linux-unix-osx-kill-hung-ssh-session/

Redirect stderr (2) to stdout (1) or vice-versa

2>&1 

See if a variable is empty

(notice the negation !)

if [[ ! $id ]]; 
    echo "$id is empty"
fi

Reading a file line by line

while IFS='' read -r line || [[ -n "$line" ]]; do
    echo "Text read from file: $line"
done < "$1"

https://stackoverflow.com/questions/10929453/read-a-file-line-by-line-assigning-the-value-to-a-variable

Looping in bash

#!/bin/bash
# Tested using bash version 4.1.5
for ((i=1;i<=100;i++));
do
# your-unix-command-here
echo $i
done

Directory enum in bash

you can replace * with some pattern matching

for entry in "$search_dir"/*
do
  echo "$entry"
done

Who invoked sudo

https://stackoverflow.com/questions/3522341/identify-user-in-a-bash-script-called-by-sudo

Checking if have sudo access

#!/bin/bash
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 
   exit 1
fi

https://askubuntu.com/questions/15853/how-can-a-script-check-if-its-being-run-as-root

⚠️ **GitHub.com Fallback** ⚠️