Over The Wire - Useful Commands Primer Part 2
Tags: beginners, tutorials, shell
Published: 2020-06-29 15:20:59

---
title: Over The Wire - Useful Commands Primer Part 2
published: true
description: A set of Unix command-line utilities that are used for solving the OverTheWire wargames.
tags: beginners, tutorials, shell
//cover_image: https://direct_url_to_image.jpg
series: Over The Wire
---

<!-- TOC depthTo:2 -->

- [The strings Command](#the-strings-command)
- [The base64 Command](#the-base64-command)
- [The file Command](#the-file-command)

<!-- /TOC -->

## The strings Command

The `strings` command is a command-line utility for returning each string of *printable characters* in a file. It can be used to determine the contents of a file, and also to extract text from binary files.

### How to get strings of a specific length?

To return all strings in a file that is at least of a specific length, use the `-n` option. When followed by an integer, it will return strings which are at least the number of that integer in length.

In the following example, there is a file, `dogs.txt`.

The `dogs.txt` file contains the following:

```
cat dogs.txt
Akbash
Bloodhound
Briard
Komondor
Vizsla
```

The `strings` tool can be used to get all strings of size 7 or greater:

```shell
strings dogs.txt
Bloodhound
Komondor
```

### How to get the offset of each string in the file?

To get the offset position for each line on which one or more strings are found, use the `-t` option. This option must be followed by a letter indicating the numbering system to be used. You can use *o* for octal, *d* for decimal and *x* for hexadecimal.

Each printing character, each space, and the start of each new line add one to the count.

In the following example, there is a file, `dogs.txt`.

The `dogs.txt` files contain the following

```
cat dogs.txt
Akbash
Bloodhound
Briard
Komondor
Vizsla
```

The `strings` tool can be used to get the offset of the strings in the file.

```shell
strings -t d dogs.txt
0 Akbash
7 Bloodhound
18 Briard
...
```

## The base64 Command

The `base64` is a command-line utility for encoding and decoding data in base64 format. 

### How to encode a file?

To encode a file, use the `base64` command along with the file you want to encode.

In the following example, there is a file, `data.txt`.

```shell
cat data.txt
This is some sample data to be encoded
```

The `base64` tool can be used to encode the file in base64 format.

```shell
base64 data.txt
VGhpcyBpcyBzb21lIHNhbXBsZSBkYXRhIHRvIGJlIGVuY29kZWQK
```

### How to decode a file?

To decode a file, use the `base64` command with the `-d` option. 

In the following example, there is a file, `data-encoded.txt`. 

```shell
cat data-encoded.txt
VGhpcyBpcyBzb21lIHNhbXBsZSBkYXRhIHRvIGJlIGVuY29kZWQK
```

The `base64` tool can be used to decode the file from the base64 format.

```shell
base64 -d data-encoded.txt
This is some sample data to be encoded
```

## The file Command

The `file` command determines the file type of a file. It reports the file type in a human-readable format or MIME type. As filenames in UNIX can be entirely independent of file type, `file` can be a useful command to determine how to view or work with a file.

### How to determine the file type of a file

To determine the file type of a file, pass the name of a file to the `file` command. The filename along with the file type will be printed to standard output.

```shell
file file.txt
file.txt: ASCII text
```

To show just the file type pass the `-b` option.

```shell
file -b file.txt
ASCII text
```

The `file` command can be useful as filenames in UNIX bear no relation to their file type. So a file called `somefile.csv` could actually be a zip file. This can be verified by the `file` command.

```shell
file somefile.csv
somefile.csv: Zip archive data, at least v2.0 to extract
```

### How to determine the file of multiple files?

The `file` command can also operate on multiple files and will output a separate line to standard output for each file.

```shell
file unix-*.md
unix-cat.md:         ASCII text, with very long lines
unix-comm.md:        ASCII text, with very long lines
unix-cut.md:         UTF-8 Unicode text
unix-exit-status.md: ASCII text
unix-file.md:        ASCII text, with very long lines
```

### How to view the mime type of a file

To view the mime type of a file rather than the human-readable format pass the `-i` option.

```shell
file -i file.txt
file.txt: text/plain; charset=us-ascii
```

This can be combined with the `-b` option to just show the mime type

```shell
file -ib file.txt
text/plain; charset=us-ascii
```

### How to view compressed files without decompressing?

To view compressed files without decompressing them pass the `-z` option. In the following example, a file `foo.txt.gz` is a gzip-compressed ASCII text file.

```shell
file -z bar.txt.gz
bar.txt.gz: ASCII text (gzip compressed data, was "bar.txt", last modified: Wed Jun  7 19:31:23 2020, from Unix)
```