# Bash

Command man pages.

# VI/VIM

#### Line Numbers

vim can display line numbers using the below command:

```
:set number
```

##### Skip to specific line number

To skip to line 255:

```
:255
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------ </span></span><span class="EOP SCXO41186676 BCX0"> </span></span></span>

##### Searching strings

To search for a string in a text file, the / option can be used. In the below example, we're searching for the word test

```
:/test
```

To skip to the next found instance of the word, press the n key.

##### Search string on a range of lines

In this example, I'm searching for the string 'test' on lines 1-100:

```
:1,100/test
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------ </span></span><span class="EOP SCXO41186676 BCX0"> </span></span></span>

#### Deleting Lines

Vim can delete lines using a number of methods

##### Remove X amount of lines.

 The below command would remove 5 lines from (and including) the current line:

```
:5d
```

##### Remove a range of lines:

```
1,5d
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------ </span></span><span class="EOP SCXO41186676 BCX0"> </span></span></span>

#### Search and replace

##### Search and replace a word through the whole file:

```
:%s/wordtosearch/wordtoreplacewith
```

##### Search and replace on specific lines

We can couple the search and replace function of vim with the line number range tool. The below would find any instances of the word 'test' within the line range of 1-100, and then replace all instances of the word with 'testing':

```
:1,100s/test/testing
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------ </span></span><span class="EOP SCXO41186676 BCX0"> </span></span></span>

#### Copy &amp; pastse

##### Copy the currently selected line  


```
:yy
```

##### Copy a range of lines

The below would copy lines 1-1000

```
:1,1000y
```

##### Paste the line:

```
p
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------ </span></span><span class="EOP SCXO41186676 BCX0"> </span></span></span>

#### <span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB">Saving &amp; exiting </span>

##### <span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB">save and quit:</span>

```
:wq
```

##### <span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB">don't save and quit</span>

```
:q!
```

##### Save as new file

```
:w newfilename
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------ </span></span><span class="EOP SCXO41186676 BCX0"> </span></span></span>

# Find, Locate, and Grep

====================================================================================

### FIND

Find is a linux search tool that can be used to find a variety of files based on the given criteria.

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------------</span></span></span></span><span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0"> </span></span> </span></span>

##### Basic find

find targetdirectory -name stringtofind

```
find / -name hello.txt
```

##### wildcards

```
find / -name "*.txt"
```

##### Discard errors (ie permission denied)

```
find / -name "*.txt" 2>/dev/null
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------------</span></span></span></span><span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0"> </span></span></span></span>

##### <span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="EOP SCXO41186676 BCX0"> </span></span></span>Find files with specific permissions

Find files with read,write, and execute permissions

```
find / -perm +rwx
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------------</span></span></span></span><span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0"> </span></span> </span></span>

##### Find files by size

Find files that are greater than 2MB:

```
find / -size +2M
```

Find files that are less than 2MB:

```
find / -size -2M
```

Find files that are exactly 2MB:

```
find / -size 2M
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------------</span></span></span></span><span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0"> </span></span> </span></span>

##### Find files by owner/group

Find files based on group

```
find / -group groupname
```

Find files based on owner

```
find / -user username
```

====================================================================================

### LOCATE

Locate is similar to find in its functionality, however, there are some important distinctions:

- Locate keeps it's own database of files on a system
- Locate is less disk IO intensive since it doesn't have to scan the whole hard drive for files, instead it references it's own database.
- Locate isn't installed by default on most systems, the package name is mlocate. Once installed, you'll need to run the updated command to update locate's database - this should really be updated every time you use the command

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------------</span></span></span></span><span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0"> </span></span></span></span>

##### Locate file based on name

```
locate test.txt
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">====================================================================================</span></span></span></span>

### <span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="EOP SCXO41186676 BCX0"> GREP</span></span></span>  


<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="EOP SCXO41186676 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------------</span></span><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0"> </span></span></span></span></span>

##### <span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="EOP SCXO41186676 BCX0">-i - case insensitive</span></span></span>

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="EOP SCXO41186676 BCX0">As with all of Linux, grep is case sensitive. The -i flag can be passed to ignore case type.</span></span></span>

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="EOP SCXO41186676 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------------</span></span><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0"> </span></span></span></span></span>

##### <span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="EOP SCXO41186676 BCX0">-v -exclude string</span></span></span>

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="EOP SCXO41186676 BCX0">The -v flag is used to exclude a string from an output.</span></span></span>

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="EOP SCXO41186676 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------------</span></span><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0"> </span></span></span></span></span>

##### <span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="EOP SCXO41186676 BCX0">| - or statement</span></span></span>

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="EOP SCXO41186676 BCX0">The below will search for string1 or string2 within a file, if both are found then both will be outputted.</span></span></span>

```
grep 'string1|string2' filename
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="EOP SCXO41186676 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------------</span></span><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0"> </span></span></span></span></span>

##### <span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="EOP SCXO41186676 BCX0">-r - search files recursively </span></span></span>

```
grep -r hello /
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------------</span></span></span></span><span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0"> </span></span></span></span>

##### -A &amp; -B (before and after)

Sometimes you might want to search for a string, and see the lines before/after that string.

The below will show the 2 lines following the string

```
grep -A2 string filename
```

The below will show the 2 lines before the string

```
grep -B2 string filename
```

You can also combine these to see the lines before and after

```
grep -A2 -B2 string filename
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">====================================================================================</span></span></span></span>

#### <span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="EOP SCXO41186676 BCX0">-E or egrep - extended regular expression</span></span></span>

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="EOP SCXO41186676 BCX0">Regular expression is essentially the methodology that we can use to manipulate grep to find advanced string patterns.</span></span></span>

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="EOP SCXO41186676 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------------</span></span><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0"> </span></span></span></span></span>

##### <span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="EOP SCXO41186676 BCX0">Special characters</span></span></span>

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="EOP SCXO41186676 BCX0">When trying to grep for special characters, you need to make sure to 'escape' those characters, this is done by proceeding special characters with a \\:</span></span></span>

```
grep -E year\'s
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------------</span></span></span></span><span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0"> </span></span></span></span>

##### Beginning and end of line

line begins with

The below example would show any lines beginning with the character '1'

```
grep -E "^1" filename
```

line ends with

The below example would show any lines ending with the character '1'

```
grep -e "1$" filename
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------------</span></span></span></span><span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0"> </span></span></span></span>

##### Ranges

grep interprets ranges that are defined through square brackets \[\].

line begins with 1 and is followed by numbers in the 0-2 range:

```
grep -E "^1[0-2]" filename
```

We can also search for ranges of letters

The below command would search for the letter b, proceeded by any letter in the specified range, followed by the letter g:

```
grep -E "b[aeiou]g" filename
```

We can also search a range of letters like this:

```
grep -E "b[a-z]g" filename
```

You can also combine ranges

```
grep -E "b[a-z,A-Z]g" filename
```

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------------</span></span></span></span><span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0"> </span></span></span></span>

##### Wildcards

There are a number of wildcard options available to use in egrep.

. - any single character

```
grep -e "c.t" filename
```

- Matches: "cat", "cot", "cut", etc.
- Does not match: "ct", "caat"

\* - matches zero or more occurrences of the preceding character

```
grep -e "g*d" filename
```

- Matches: "gd", "god", "good", "goood"
- Does not match: "go", "goooo", "gdo", "goddy"

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">.\* - match zero or more of any character</span></span></span></span>

```
grep -e "a.*b" filename
```

- Matches: "ab", "acb", "axyzb", "a123b"
- Does not match: "a b", "ab ", "acbd", "a"

<span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0">------------------------------------------------------------------------------------------------------------------------------------------------</span></span></span></span><span class="TextRun SCXO38321458 BCX0" data-contrast="none" lang="EN-GB" xml:lang="EN-GB"><span class="NormalTextRun SCXO38321458 BCX0"><span class="TextRun SCXO41186676 BCX0" data-contrast="none" lang="EN-US" xml:lang="EN-US"><span class="NormalTextRun SCXO41186676 BCX0"> </span></span></span></span>