Javatpoint Logo
Javatpoint Logo

Linux strings command

Linux strings command is used to return the string characters into files. It primarily focuses on determining the contents of and extracting text from the binary files (non-text file).

It is a complex task for a human to find out text from an executable file. The binary files, such as program files, contain human-readable text. These files are large-sized if we use a cat or less command; it may cause the terminal to hang up.

There can be two types of characters in a file; printable and non-printable. The alphanumeric characters, punctuation, or whitespaces are known as printable characters; except the printable character, all the characters are known as non-printable characters.

In simple words, we can say that it extracts printable characters from files so that other commands can use the strings without non-printable characters.

Here, the question arises why do we put text in executable files? When an application or software is deployed, most developer packages the binary files. But it is good to pack some ASCII text in the binary file. It will be helpful for the users and for the developers to understand more about the executable file.

So, the strings command is useful to determine the contents of non-text files.

How to use it?

Use of strings command is straight forward, just pass the file name as an argument and execute it. Let's understand it with an example.

We have a system file called gyp.el. To extract the string from this file, execute the command as follows:

Consider the below output:

Linux strings command

Customize the character limit

By default, the strings command prints only the character sequence that is at least four characters long. However, we can change this limit forcefully by using the -n option. It allows us to pass the number to signify a limit.

It is recommended to use a higher limit because of the shorter number you use, the more junk you'll see.

To set the limit for two characters, execute the below command:

Consider the below output:

Linux strings command

Although the above output contains too many strings, so it is complex to identify the two character strings, but we can understand how things are working for the strings command.

Piping the limit

In case we have a file that contains a lengthy output, we can pipe it through less. It will show less output, and we can scroll and append output as our need.

To pipe a limit through less, execute the command as follows:

Consider the below output:

Linux strings command

we can append the above output by scrolling it down.

How to look through RAM using Strings command?

The strings command is not just limited to files. It allows us to look through the RAM of our computer system. We need to use Sudo privilege because we are accessing /dev/mem, which holds the blueprint of the main memory of our system . Execute the command as follows:

In the above command, we are using less to limit the output. Consider the below output:

Linux strings command

Include Whitespaces

The default behavior of the strings command contains whitespace as a printable character. Therefore if we have a string, " Javatpoint is one of the best platforms To learn technology," then the strings command would return the whole text.

It does not consider the new line characters and carriage returns as printable characters.

To print a string that recognizes new line characters and carriage returns as a printable character, execute the command as follows:

Change the Encoding

The strings command provides four types of encoding options, which are as following:

s: 7-bit byte (used for ASCII, ISO 8859)

S: 8-bit byte

b: 16-bit bigendian

l: 16-bit littleendian

The default option is 's'; a 7-bit byte.

We can change the encoding; to change it, execute the following command:

or

In the above commands, we specified the encoding as 'S,' which means 8 bit-byte encoding. You can specify any of the above options, simply by replacing 'S' with the encoding letter of your choice.

Reading options from file

If you are using the same options repeatedly, you don't need to specify each time. Instead, you can create a file and specify the options within that file.

To create a file for specifying the options, execute the below command:

The above command will open the vi editor. Here we are creating a file using vi editor; you can choose any editor of your choice.

Press ESC and 'i' keys for insert mode. Type the options that you want to specify as follows:

>

After entering the option, save the file by pressing ESC and :wq! Keys. Consider the below image:

Linux strings command

To run the strings command with the specified options, execute the command as follows:

The above command will execute the command with the specified options. The gyp.el is the filename that we are using; you can type your file name. Consider the below output:

Linux strings command

Getting Help

If you stuck anywhere while using the strings command. You can take help from your terminal by using the help option. To get help, execute the below command:

Consider the below output:

Linux strings command

you can also read the manual at any time by executing the command as follows:

Check the Strings Version

To check the version of strings that you are using, execute one of the following commands:

The above commands will display the installed version of the strings. Consider the below output:

Linux strings command




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA