---
title: Useful Command Line Snippets
date: 2018-09-11
tags: unix, snippets
source: https://brianchildress.co/blog/useful-command-line-snippets
---

# Useful Command Line Snippets

### Here are some useful command line snippets

Get the number of files in a directory, by using the `ls` command this will count the result.
```
ls -l | wc -l
```

List all files in a directory/ directories recursively
```
ls -LR
```

Find all files in a directory based on a pattern
```
ls | grep -P "^A.*[0-9]{2}$"
```

And remove all files that match that pattern
```
ls | grep -P "^A.*[0-9]{2}$" | xargs -d "\n" rm
```

Move multiple files to a directory
```sh
mv -t path/to/DESTINATION file1 file2
```

OR Using a wildcard(\*) to match file names

```sh
mv *.jpg path/to/DESTINATION
```

Decode a Base64 string in the command line
```sh
echo < base64 string > | base64 --decode
```

Search your bash history
```sh
history
```
Search the last `N` number of results from you history
```sh
history 5
```

Grep your history
```sh
history | grep <thing to search for>
history | grep ssh
```

Execute command found in history
```sh
!<history id>
!5
```
