Linux
Set Rstudio theme
See Rstudio theme file.
Increase swap space
https://askubuntu.com/questions/178712/how-to-increase-swap-space
Encrypt/Decrypt files
gpg -c filename
gpg -d filename.gpg
Don’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-agent
CPU average usage (top)
top -b -n1 -u `whoami` | sed 1,7d | awk '{i += $9} END {print i/100}'
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.txt
Find start time of a long-running process
ps -eo pid,lstart,cmd | grep process_name
Find 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 xaxa
Find 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 -l
Rename 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_}"; done
Symbolic links
# Create symbolic link
ln -s /full_path_to/real_exe_file_target /full_path_to/link
# Delete a symbolic link
unlink /path_to/link
Mount external disk
sudo fdisk -l
# usually it's on `/dev/sdb1` partition
mkdir /media/disk
sudo mount /dev/sdb1 /media/disk
After you are done, run:
sudo umount /media/disk
Add command path permanently
cd /etc/profile.d
vim test.sh
# add lines
EXEC_HOME=/usr/bin/my-exetutable
export EXEC_HOME
# test
echo $EXEC_HOME
Counter with dots
To see an incremental counter:
i=0; while true; do sleep 1; echo $i; i=$((i+1)); done
To see dots (server connection awaiting!):
while true; do echo -n .; sleep 1; done
Ubuntu 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.1
Nice 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 filename
Do 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\")"
done
Make 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 cloc
Delete files fast
find . -maxdepth 1 -name "something*" -print0 | xargs -0 rm
List files in a dir efficiently
Copy the listdir.c file. Then:
gcc listdir.c -o listdir
./listdir /dirWithTooManyFiles
Count and change the reserved space in an ext4 partition ‘ONLINE’
Change the reserved space to 1%:
tune2fs -m 1 /dev/sdb1
Count 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 space
Kill many processes at once (that match a pattern)
ps aux | grep -v grep | grep -i patternToMatch | awk '{print $2}' | xargs kill -9
Deleting 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 0
Find number of CPUs and model
grep -c processor /proc/cpuinfo
lscpu
nproc
cat /proc/cpuinfo | grep name | tail -n1
Dynamic 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 now
Change hostname
# Stop MySQL if it is running
service mysql stop
old hostname = old-name
new hostname = new-name
In the next 2 files change old-name
to new-name
:
vim /etc/hosts
vim /etc/hostname
and execute:
hostname new-name
# check
hostname
Restart MySQL (if needed):
service mysql start
Service tag
dmidecode -t system
See cached files info in the pwd
linux-fincore --pages=false --summarize --only-cached *
Clean cached memory
sudo sysctl vm.drop_caches=3
sync && echo 3 | tee /proc/sys/vm/drop_caches
Build an ext4 filesystem on a disk partition
cat /etc/fstab
fdisk -l
cfdisk /dev/sdc
mkfs.ext4 /dev/sdc1
mount -a
df -h