bash - animeshtrivedi/notes GitHub Wiki
sudo sh -c '/usr/bin/echo 3 > /proc/sys/vm/drop_caches'
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
echo $'\t'"I am $USER"
Echo tab characters in bash script
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.
tar -czvf filename.tar.gz /path/to/dir1 dir2 dir3
extract in the /tmp folder
tar -xzvf projects.tar.gz -C /tmp/
# match lines containing numbers
grep -E -i [0-9]+
# that also contain blk keyword
https://www.cyberciti.biz/faq/grep-regular-expressions/
: '
Here
it
is
'
.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
$ 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
# all output and show it
SomeCommand 2>&1 | tee log.txt
#all to a file
SomeCommand &>./log.txt
#discard stderr
SomeCommand 2>/dev/null
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.
sshfs atr@localhost:/home/atr/ ./vm-qemu7777/ -p 7777
sudo -E env "PATH=$PATH" <command> [arguments]
sudo env "PATH=$PATH" <command> [arguments]
- https://stackoverflow.com/questions/12996397/command-not-found-when-using-sudo
- https://unix.stackexchange.com/questions/83191/how-to-make-sudo-preserve-path
$ 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
for f in *; do echo ${f%%.*}; done
https://stackoverflow.com/questions/7119223/file-name-without-extension-in-bash-for-loop
convert *.png file.pdf
awk 'NF' filename
https://stackoverflow.com/questions/16414410/delete-empty-lines-using-sed
lets say 3rd
awk 'NR % 3 == 0' file.txt
grep -f inputfile.txt inputfile2.txt
https://stackoverflow.com/questions/21287389/how-to-find-words-from-one-file-in-another-file
$ dpkg -S /bin/ls
coreutils: /bin/ls
https://www.redhat.com/sysadmin/manage-multiple-ssh-key-pairs
https://czarpino.medium.com/how-to-tell-git-which-ssh-key-to-use-c8574fb243fd
convert input.jpg -rotate 180 out.jpg
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
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
https://www.cyberciti.biz/faq/unix-linux-check-if-port-is-in-use-command/
sudo lsof -i -P -n | grep LISTEN
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
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.
cat items | xargs -I % git clone --mirror %
https://unix.stackexchange.com/questions/3593/using-xargs-with-input-from-a-file
~.
https://www.cyberciti.biz/faq/openssh-linux-unix-osx-kill-hung-ssh-session/
2>&1
(notice the negation !
)
if [[ ! $id ]];
echo "$id is empty"
fi
while IFS='' read -r line || [[ -n "$line" ]]; do
echo "Text read from file: $line"
done < "$1"
#!/bin/bash
# Tested using bash version 4.1.5
for ((i=1;i<=100;i++));
do
# your-unix-command-here
echo $i
done
you can replace * with some pattern matching
for entry in "$search_dir"/*
do
echo "$entry"
done
https://stackoverflow.com/questions/3522341/identify-user-in-a-bash-script-called-by-sudo
#!/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