DEV Community

Kostas Kalafatis
Kostas Kalafatis

Posted on • Edited on

Over The Wire - Useful Commands Primer Part 2

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
Enter fullscreen mode Exit fullscreen mode

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

strings dogs.txt
Bloodhound
Komondor
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

strings -t d dogs.txt
0 Akbash
7 Bloodhound
18 Briard
...
Enter fullscreen mode Exit fullscreen mode

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.

cat data.txt
This is some sample data to be encoded
Enter fullscreen mode Exit fullscreen mode

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

base64 data.txt
VGhpcyBpcyBzb21lIHNhbXBsZSBkYXRhIHRvIGJlIGVuY29kZWQK
Enter fullscreen mode Exit fullscreen mode

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.

cat data-encoded.txt
VGhpcyBpcyBzb21lIHNhbXBsZSBkYXRhIHRvIGJlIGVuY29kZWQK
Enter fullscreen mode Exit fullscreen mode

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

base64 -d data-encoded.txt
This is some sample data to be encoded
Enter fullscreen mode Exit fullscreen mode

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.

file file.txt
file.txt: ASCII text
Enter fullscreen mode Exit fullscreen mode

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

file -b file.txt
ASCII text
Enter fullscreen mode Exit fullscreen mode

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.

file somefile.csv
somefile.csv: Zip archive data, at least v2.0 to extract
Enter fullscreen mode Exit fullscreen mode

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.

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
Enter fullscreen mode Exit fullscreen mode

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.

file -i file.txt
file.txt: text/plain; charset=us-ascii
Enter fullscreen mode Exit fullscreen mode

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

file -ib file.txt
text/plain; charset=us-ascii
Enter fullscreen mode Exit fullscreen mode

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.

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)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)