# Linux - Bag of Tricks

## Introduction

This document has many useful command.

### Change Files and Folder Permissions

To change the permissions of files to 655 and subfolders to 755 (which is the common practice for directories to allow execution for navigating into them) within a specified directory and its subdirectories, you can use the `find` command with `chmod`.

Explanation of permissions:

- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad"><span data-huuid="5743926955397761133">**655 for files:**</span></div><div class="vM0jzc">
    - <span data-huuid="5743926955397761748">Owner: Read (4) + Write (2) = 6</span>
    - <span data-huuid="5743926955397762158">Group: Read (4) + Execute (1) = 5</span>
    - <span data-huuid="5743926955397762568">Others: Read (4) + Execute (1) = 5</span>
    
    </div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad"><span data-huuid="5743926955397762978">**755 for directories:**</span></div><div class="vM0jzc">
    - <span data-huuid="5743926955397759497">Owner: Read (4) + Write (2) + Execute (1) = 7</span>
    - <span data-huuid="5743926955397759907">Group: Read (4) + Execute (1) = 5</span>
    - <span data-huuid="5743926955397760317">Others: Read (4) + Execute (1) = 5</span>
    
    </div></div></div></div>

Command

```bash
find /path/to/directory -type f -exec chmod 655 {} +
find /path/to/directory -type d -exec chmod 755 {} +

find /path/to/directory \( -type d -exec chmod 755 {} + \) -o \( -type f -exec chmod 644 {} + \)

chmod -R u+rwX,go+rX,go-w /path/to/directory

find /path/to/directory -type d -print0 | xargs -0 chmod 755
find /path/to/directory -type f -print0   | xargs -0 chmod 644

find /path/to/directory -print0 \
  \( -type d -exec chmod 755 {} + \) \
  -o \( -type f -exec chmod 644 {} + \)
```

### Linux Set Time Examples

You can also simplify format using following syntax:

```shell
date +%Y%m%d -s "20081128"
```

To set time use the following syntax:

```shell
date +%T -s "10:13:13"
```

Use the following syntax to set new data and time:

```shell
date --set="STRING"
```

For example, set new data to 2 Oct 2006 18:00:00, type the following command as root user:

```shell
date -s "2 OCT 2006 18:00:00"
```

OR

```shell
date --set="2 OCT 2006 18:00:00"
```

### Rclone Copy Examples

Click here for the page [HERE](https://docs.thehomestack.io/books/linux/page/linux-how-to-use-rclone "rClone")

### Rsync Copy Examples

This is to move files from one server to another

Ending the folder WITHOUT a “/” slash means copy that folder everything in that folder

Ending the folder WITH a “/” slash means copy everything within that folder

Example for “remote to local” location

```shell
rsync -chavzP --stats --progress -e ssh user@remote_host:/remote_folder/dir1/ /local_folder/dir1/
```

Example for “local to remote” location

```shell
rsync -chavzP --stats --progress -e ssh /local_folder/dir1/ user@remote_host:/remote_folder/dir1/
```

### Rsync Auto Login while sending

Example to add a Rsync key on the remote server

On the local server simply login as a given user ex: ROOT or USER

```shell
ssh-keygen -t rsa
```

If it already exists simply hit "n"

<table border="1" id="bkmrk-generating-public%2Fpr" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>Generating public/private rsa key pair.  
Enter file in which to save the key (/root/.ssh/id\_rsa):   
/root/.ssh/id\_rsa already exists.  
Overwrite (y/n)?</td></tr></tbody></table>

If not then simply hit enter through all options

Example: of using ROOT

<table border="1" id="bkmrk-generating-public%2Fpr-1" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>Generating public/private rsa key pair.  
Enter file in which to save the key (/root/.ssh/id\_rsa):   
Enter passphrase (empty for no passphrase):   
Enter same passphrase again:   
Your identification has been saved in /root/.ssh/id\_rsa  
Your public key has been saved in /root/.ssh/id\_rsa.pub  
The key fingerprint is:  
SHA256:JoMN/cxvsqZWBHws4eyrU5Q0F0qRe//44qdrrjiQmbU root@client01  
The key's randomart image is:  
+---\[RSA 3072\]----+  
| .+=.. |  
| =B.+ |  
| ..=O |  
| ==+o |  
| = E=... |  
| o... oo |  
| ..o..++ o |  
| .oo+o==\*. |  
+----\[SHA256\]-----+  
You have mail in /var/spool/mail/root</td></tr></tbody></table>

Run the following to add the key to the remote server, you can also use IP instead of host name

```shell
ssh-copy-id -i ~/.ssh/id_rsa.pub remuser@server01
```

Example of using a USER you will have to enter yes and the USER password

<table border="1" id="bkmrk-%2Fusr%2Fbin%2Fssh-copy-id" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 99.881%;"></col></colgroup><tbody><tr><td>/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id\_rsa.pub"  
The authenticity of host 'server02 (192.168.x.x)' can't be established.  
ED25519 key fingerprint is SHA256:oZnvrgY+2Xpd2/huaffvzLMBAgI52AMPUmq/LPLIXbE.  
This key is not known by any other names  
Are you sure you want to continue connecting (yes/no/\[fingerprint\])?<span style="color: rgb(241, 196, 15);"> yes</span>  
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed  
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys  
r<span style="color: rgb(241, 196, 15);">emuser@server02's password: </span>  
tput: No value for $TERM and no -T specified  
tput: No value for $TERM and no -T specified  
tput: No value for $TERM and no -T specified  
tput: No value for $TERM and no -T specified  
tput: No value for $TERM and no -T specified  
tput: No value for $TERM and no -T specified  
tput: No value for $TERM and no -T specified

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'remuser@server02'"  
and check to make sure that only the key(s) you wanted were added.

</td></tr></tbody></table>

<span style="color: rgb(241, 196, 15);">Optional:</span> If the command cannot be run above you can copy the key to the remote server manually into the “authorized\_keys” file

```shell
cd
cd .ssh
vi authrized_keys
```

<span style="color: rgb(241, 196, 15);">Optional:</span> Change the permissions on the local server

```shell
chmod 600 ~/.ssh/*
chmod 711 ~/.ssh
chmod 711 ~
```

### Synology Rsync

```shell
rsync -aXHmS --syno-acl /volum1/[xxx] /volume2/[xxx]
```

-a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)  
-p, --perms preserve permissions  
-X, --xattrs preserve extended attributes  
-o, --owner preserve owner (super-user only)  
-g, --group preserve group  
\--syno-acl copy Synology ACL data

I use the following options myself:  
rsync -avhxWog --stats --backup --suffix $OLDSUFFIX --exclude-from=$RSYEXCL --syno-pseudo-root

No idea why I list options "og" since they're implied by -a, but it works...

Regards, Arild  
PS: "rsync --help" lists all available options for rsync

### Find and Replace String with <span class="code" spellcheck="false">sed</span>

There are several versions of <span class="code" spellcheck="false">sed</span>, with some functional differences between them. macOS uses the BSD version, while most Linux distributions come with GNU <span class="code" spellcheck="false">sed</span> pre-installed by default. We’ll use the GNU version.

The general form of searching and replacing text using <span class="code" spellcheck="false">sed</span> takes the following form:

```shell
sed -i 's/SEARCH_REGEX/REPLACEMENT/g' INPUTFILE
```

<span class="fabric-text-color-mark" data-text-custom-color="#ffffff">Cop</span>

- <span class="code" spellcheck="false">-i</span> - By default, <span class="code" spellcheck="false">sed</span> writes its output to the standard output. This option tells <span class="code" spellcheck="false">sed</span> to edit files in place. If an extension is supplied (ex -i.bak), a backup of the original file is created.
- <span class="code" spellcheck="false">s</span> - The substitute command, probably the most used command in sed.
- <span class="code" spellcheck="false">/ / /</span> - Delimiter character. It can be any character but usually the slash (<span class="code" spellcheck="false">/</span>) character is used.
- <span class="code" spellcheck="false">SEARCH\_REGEX</span> - Normal string or a regular expression to search for.
- <span class="code" spellcheck="false">REPLACEMENT</span> - The replacement string.
- <span class="code" spellcheck="false">g</span> - Global replacement flag. By default, <span class="code" spellcheck="false">sed</span> reads the file line by line and changes only the first occurrence of the <span class="code" spellcheck="false">SEARCH\_REGEX</span> on a line. When the replacement flag is provided, all occurrences are replaced.
- <span class="code" spellcheck="false">INPUTFILE</span> - The name of the file on which you want to run the command.

### Find and Replace String with <span class="code" spellcheck="false">sed</span> within <span class="code" spellcheck="false">vi</span>

This is to search and replace a file globally withing vi

```shell
:%s/search_string/replacement_string/g
```

### Kill Users in Linux

This is to be used when trying to kill users using the connection, replace the ? with the number of the session.

```shell
pkill -KILL -t pts/?
```

<p class="callout info">Continued in [Linux - Bag of Tricks (Part 2): Certificates, Databases, Disks &amp; Boot](https://docs.thehomestack.io/books/linux/page/linux-bag-of-tricks-part-2-certificates-databases-disks-boot).</p>