Linux
Set Rstudio theme
See Rstudio theme file.
Increase swap space
https://askubuntu.com/questions/178712/how-to-increase-swap-spaceEncrypt/Decrypt files
gpg -c filename
gpg -d filename.gpgDon’t ever cache the password:
vim ~/.gnupg/gpg-agent.conf
# copy-paste following two lines
default-cache-ttl 1
max-cache-ttl 1
echo RELOADAGENT | gpg-connect-agentCPU and MEM usage (top)
Single commands:
# CPU: how many CPU/cores
top -b -n1 -u $(whoami) | awk 'NR>7 {sum+=$9} END {print sum/100, "cores"}'
# MEM: how many GB? (RES)
ps -u $(whoami) --no-headers -o rss | awk '{sum+=$1} END {print sum/(1024*1024) " GB"}'Using watch:
#!/bin/bash
watch -n 1 '
# CPU usage
cpu=$(top -b -n1 -u $(whoami) | awk '"'"'NR>7 {sum+=$9} END {print sum/100, "cores"}'"'"')
# MEM (RES) usage
mem=$(ps -u $(whoami) --no-headers -o rss | awk '"'"'{sum+=$1} END {print sum/(1024*1024) " GB"}'"'"')
# Output the results
echo -e "CPU Usage: $cpu\nMemory Usage: $mem"
'Break paragraph to multiple sentences, each in a new line
Paragraph is like: AAaa. Bbbbb. Cccc.. What you get is: AAaa.\nBbbbb.\nCccc.
sed -i 's/\. /\.\n/g' test.txtFind start time of a long-running process
ps -eo pid,lstart,cmd | grep process_nameFind pattern in files and retrieve file name as well
Find in all .html files inside a directory, the mentions of string xaxa:
find . -name \*.html -print0 | xargs -0 grep -n -H xaxaFind pattern recursively in directories and files
E.g. my current directory has multiple directories that each one has multiple files which I want to cat and find a pattern:
find . -type f -exec cat {} + | grep stable | wc -lRename files
Let’s say I have many files in a directory which have the sub-string _rand_ and I want to change that sub-string to _whatever_. Run inside the directory:
for file in `ls | grep "_rand_"`; do mv "$file" "${file/_rand_/_whatever_}"; doneSymbolic links
# Create symbolic link
ln -s /full_path_to/real_exe_file_target /full_path_to/link
# Delete a symbolic link
unlink /path_to/linkMount external disk
sudo fdisk -l
# usually it's on `/dev/sdb1` partition
mkdir /media/disk
sudo mount /dev/sdb1 /media/diskAfter you are done, run:
sudo umount /media/diskAdd command path permanently
cd /etc/profile.d
vim test.sh
# add lines
EXEC_HOME=/usr/bin/my-exetutable
export EXEC_HOME
# test
echo $EXEC_HOMECounter with dots
To see an incremental counter:
i=0; while true; do sleep 1; echo $i; i=$((i+1)); doneTo see dots (server connection awaiting!):
while true; do echo -n .; sleep 1; doneUbuntu text scaling
Make all text bigger/smaller:
# big
/usr/bin/gsettings set org.gnome.desktop.interface text-scaling-factor 1.5
# small
/usr/bin/gsettings set org.gnome.desktop.interface text-scaling-factor 1.1Nice bash aliases
# choose file to put the aliases
vim ~/.bashrc
vim ~/.bash_aliases # better
alias 'com'='git commit'
alias 'cam'='git commit --amend'
alias 'gt'='git status'
alias 'updocs'='git add docs; git commit -m "update docs"'
alias 'disk'='df -h | grep -v loop'
alias 'l'='ls -ltrh'
alias 'off'='sudo poweroff'
alias 'useful'='cd ~/repos/useful'
alias 'p8'='ping 8.8.8.8'
alias 'renet'='sudo service network-manager restart; i=0; while [[ $i < 6 ]]; do echo -n .; sleep 1; let i=i+1; done; echo "";'
alias 'ssh-server'='ssh name@server'
alias 'ssh-server'='ssh -J name@proxy-server name@main-server'Change terminal prompt look
Add to ~/.bashrc this line:
export PS1='\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]~\[\033[00m\]\$ 'or this one that includes the name of the current working dir:
export PS1='\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;36m\][\W]\[\033[00m\]\$ 'Notes:
32m=> green,00m=> white,34m=> blue,36m=> Cyan\u=> user,\W=> working dir
Extract specific characters from each line in a file
cut -c 1-30 filenameDo something on many files in a dir
Something can be: rendering notebooks to HTML or checking if the files are the same as other files for example
#!/bin/bash
files=`ls`
dir="/somewhere/over/the/rainbow"
rmd_files=`ls | grep Rmd`
for file in ${rmd_files}; do
filename_no_extension="$(basename "${file}" .Rmd)"
diff -s $dir$file $file
Rscript -e "library(rmarkdown); rmarkdown::render(\"./$file\",\"html_document\")"
doneMake desktop icons appear and disappear
/usr/bingsettings set org.gnome.desktop.background show-desktop-icons false
Find total MB of files in a dir
ls | xargs stat --format=%s | awk '{s+=$1} END {print s/(1024*1024)}'Count lines of source code
find . -name '*.php' | xargs wc -l# or use cloc:
apt-get install clocDelete files fast
find . -maxdepth 1 -name "something*" -print0 | xargs -0 rmList files in a dir efficiently
Copy the listdir.c file. Then:
gcc listdir.c -o listdir
./listdir /dirWithTooManyFilesCount and change the reserved space in an ext4 partition ‘ONLINE’
Change the reserved space to 1%:
tune2fs -m 1 /dev/sdb1Count the percentage:
a=$(tune2fs -l /dev/sdb1 | grep -i 'Reserved block count' | awk '{ print $4 }')
b=$(tune2fs -l /dev/sdb1 | grep 'Block count' | awk '{ print $3 }')
echo "scale=5; ($a/$b)*100" | bc # this is the percentage of the reserved spaceKill many processes at once (that match a pattern)
ps aux | grep -v grep | grep -i patternToMatch | awk '{print $2}' | xargs kill -9Deleting file descriptors
After deleting many files, the space is not freed and you need to delete the file descriptors:
find /proc/*/fd -ls 2> /dev/null | awk '/deleted/ {print $11}' | xargs -p -n 1 truncate -s 0Find number of CPUs and model
grep -c processor /proc/cpuinfo
lscpu
nproc
cat /proc/cpuinfo | grep name | tail -n1Dynamic linking
ldd <executable_name>if you see not found for some library, put the <something>.so.x.x.x file in: /usr/local/lib
ln -s <something>.so.x.x.x <something>.so.x` # or whatever is needed
ldconfig -v
ldd <executable_name> # should be OK nowChange hostname
# Stop MySQL if it is running
service mysql stop
old hostname = old-name
new hostname = new-nameIn the next 2 files change old-name to new-name:
vim /etc/hosts
vim /etc/hostnameand execute:
hostname new-name
# check
hostnameRestart MySQL (if needed):
service mysql startService tag
dmidecode -t systemSee cached files info in the pwd
linux-fincore --pages=false --summarize --only-cached *Clean cached memory
sudo sysctl vm.drop_caches=3sync && echo 3 | tee /proc/sys/vm/drop_cachesBuild an ext4 filesystem on a disk partition
cat /etc/fstab
fdisk -l
cfdisk /dev/sdc
mkfs.ext4 /dev/sdc1
mount -a
df -h