# Tips & Tricks

##### Rsync 

```
rsync -avh -log-file=$HOME/.rsyncd.log --delete /mnt/nas/ /mnt/mybook/nas-backup/ --dry-run

-a = archive (preserve permissions, symbolic links, etc.)
-v = verbose
-h = human-readable
--delete = delete files in destination that no longer exist in source
--dry-run = simulate the process without making changes
```

##### Create ssh key

```
ssh-keygen -t ed25519 -C "comment" -f <keys-name>
```

##### Last commands exit code

```
echo $?
```

##### Delete only directories

```
rm -rf `ls -d */`
find . -maxdepth 1 -mindepth 1 -type d -exec rm -rf '{}' \;
```

##### Disable swap

```
cat /proc/sys/vm/swappiness
sysctl vm.swappiness=0

vim /etc/sysctl.conf
vm.swappiness=0
```

<div id="bkmrk-extract-multiple-par"><div><div><div>Extract multiple part rar file</div></div></div></div>```
unrar x <filename>.rar
```

<div id="bkmrk-sudo-%21%21---will-insta"><div><div><div>sudo !! - will instantly run the previously run command but with sudo prefix</div><div>  
</div></div></div><div>UHD - usb hsm device</div></div>##### Upgrade all outdates pip packages

```
pip list --outdated
pip install -U `pip list --outdated | awk 'NR>2 {print $1}'`
```

##### Clean /boot &amp; /usr disk, freeing inodes

First try

```
apt autoremove
apt autoremove --purge
purge-old-kernels
```

If unsuccessful, then

```
# Check your kernel
uname -r
cat /proc/version

# List installed kernels, headers, modules
dpkg --list 'linux-*' | grep ^ii
apt list --installed linux-*

# Remove and purge unnecessary ones
apt remove --purge linux-{headers,image,modules,modules-extra}-4.15.0-1*
apt remove --purge linux-{headers,image,modules,modules-extra}-4.15.0-20[0-6]

certain numbers/names - {1,2,3}
certain range - [1-3]

# Check iu packages
dpkg --list 'linux-*' | grep "^iU"

# Remove if necessary
apt remove --purge $(dpkg -l | grep "^iU" | awk '{print $2}')

```

##### JMXterm example

[https://www.googlecloudcommunity.com/gc/Cloud-Product-Articles/How-to-Read-JMX-Statistics-via-The-Shell/ta-p/77629](https://www.googlecloudcommunity.com/gc/Cloud-Product-Articles/How-to-Read-JMX-Statistics-via-The-Shell/ta-p/77629)

```
java -jar jmxterm-1.0.2-uber.jar
jvms
open <PID>
beans
get -b java.lang:type=Memory *
get used -b java.lang:type=Memory HeapMemoryUsage
```

##### Kick ssh session

```
who -u
echo "HAHA" | write user1 pts/2
kill -9 24995
```

##### putty pub2 to linux format

`ssh-keygen -i -f <pub2>`

##### restart network interface

`ip link set ens160 down && ip link set ens160 up`

##### journalctl vacuum

```
journalctl --disk-usage
journalctl --rotate
journalctl -D /var/log/journal/ --vacuum-size=100M
journalctl --verify
```

##### Measure command or script execution time

`time <command or script>`

##### Create user wo password

`adduser <user> --disabled-password --force-badname`

##### Add user to group

`usermod -a -G <group> <user>`

##### Remove user from group

`gpasswd -d <user> <group>`

##### Change shell to Bash

`sudo usermod -s /bin/bash $USER`

##### Switch iTerm2 to Bash

`chsh -s /bin/zsh`

##### Get pid info

`cat /proc/pid/cmdline`

##### Get key from port 80

`apt-key adv --keyserver hkp://keys.gnupg.net:80 --recv "E775D097"`

##### Sudo without password

create group, for ex named wheel  
`sudo visudo` and add the line below to it  
`wheel     ALL=(ALL) NOPASSWD:ALL`  
run `usermod -a -G wheel $USER`

##### Fix locale

```
open /etc/environment

LANGUAGE="en_US.UTF-8"
LANG="en_US.UTF-8"
LC_ALL="en_US.UTF-8"

locale-gen en_US.UTF-8
```

##### See lib-s waiting to be "updated"

`lsof | grep lib | grep DEL`

##### Attach NAS

**Mac**

Finder -&gt; Go -&gt; Connect to Server `afp://LS520DE2a5.local/<dir>`

**Linux**

mkdir /mnt/nas-plex

add to /etc/fstab

```
//{{ nas_host }}/Plex /mnt/nas-plex username={{ nas_username }},password={{ nas_password }},iocharset=utf8,uid=1000,gid=1000
```

mount -a

**Win**

Computer -&gt; add a network location -&gt; `\\hostname\dir`

##### My external IP

##### `curl ifconfig.me`

##### View CSR

`openssl req -text -noout -verify -in CSR.csr`

##### View certificate

`openssl x509 -in certificate.crt -text -noout`

##### Copy ssh key to machine

`ssh-copy-id <hostname or IP>`

##### Measure command execution time

`time <command>`

##### List All Directories

`ls -d */`

##### Move Directory One Lv Down

`mv Math/ ../`

##### Directory Size

`du -hs <dir_name>`

##### Check Storage

`df -h`

##### Show Linux distro info

`lsb_release -a`

##### Show certificate info

`openssl x509 -in certificate.crt -text -noout`

##### Delete all mail

`mail`  
`d *`  
`q`

##### Show cpu stats / list cores

`lscpu`  
`cat /proc/cpuinfo`  
`cat /proc/cpuinfo | grep processor | wc -l`

##### Select default editor for logged in user

`select-editor`

##### Count process open file handlers

`ls /proc/my_pid/fd | wc -l`

##### Traceroute

Linux `mtr qrl.ee`  
Windows `tracert qrl.ee`

##### Count open files (The value does not apply to a root user, so if you want to tally with the lsof output, exclude files opened by root)

`lsof|wc -l`

##### Open files limit by OS

`cat /proc/sys/fs/file-max`

##### Create trash 10MB base64 filled file

`base64 /dev/urandom | head -c 10000000 > file.txt`

##### Create trash 1MB text filled file

`yes "HelloWorld" | head --byte=1100000 > 1MB.txt`

##### Virtualbox shared folder

Go to Devices option of your VM and click on the Shared Folders option and add the required\_folder you want to share.  
Now in your file system (root) you can see media/sf\_required\_folder.   
But by default the Guest User won't have access to this folder.   
So to grant access to this folder you need to add Guest user to the group vboxsf.   
For this, `sudo adduser Guest_user vboxsf` to give access.   
Now have the access.