Updated June 10, 2023
Introduction to Linux String Command
String command in Linux will return each string type of characters that are printable in the file. It is mainly used in determining the file’s contents and extracting the text from binary-type files. A string contains a sequence of finite characters and can also be considered one character.
For any file in Linux, the GNU string will print the printable context whose length is at least four characters, followed by a non-printable character. In short, we can say that a string command is used to determine the contents of binary-type or non-text files.
Syntax:
The basic syntax of the String command in Linux is given below. You may use the syntax with the required options.
strings [-afov] [-min-len]
[-n min-len] [--bytes=min-len]
[-t radix] [--radix=radix]
[-e encoding] [--encoding=encoding]
[-] [--all] [--print-file-name]
[-T bfdname] [--target=bfdname]
[--help] [--version] file...
How does Linux String Command work in Linux?
Options | Description |
-a, -all | Scan the whole context in the file. This scanning process includes loaded sections and initializes parts too in the file. |
-f, -print-file-name | It prints the file name ahead of the string. |
–help | Display the options available for string commands. |
-d, –data | It will scan only the data part in the file. |
-v -V –version | It will print the process’s version number. |
-T –target=<BFDNAME> | It specifically specifies object code other than your system’s default format. |
-o | It is an alias for –radix=o. |
-t –radix={o,d,x} | It will print the offset in the file ahead of each string. When any single char object is specified, its radix offset will include o for octal, d for decimal, and x for hexadecimal. |
-min-len, -n min-len | It will print the sequence of characters with a minimum length, rather than 4 as default. |
Examples
Below are a few examples with a screenshot of using string command with its options.
Example #1 – With no Options
When no options are mentioned in the string command, it will extract the strings from the file and display the output.
Syntax:
strings file_name
Example:
strings test_file
Example #2 – To Set Minimum Length of the String
In Linux, by default, the string command will scan for the strings that have four characters. We can also set a longer or shorter length by using the -n command option. Below is the syntax and example shown for better understanding.
Syntax:
strings -n -2 test_file
Example: Here in the screenshot below, we can see that the files with two-letter strings are also displayed. Moreover, spaces are calculated as printable characters.
strings test_file
Example #3 – To Use Strings with Object Files
Below is a sample C object file code as shown below.
Let us now create object C code for the code above, as shown below:
Let us think that we would like to scan for the string “welcome” in the above C code file. When we scan the file using ‘grep’ command, the code will result in us if the binary file matches the string given or not, which means the grep command’s output on the binary-type file will not give us the correct patterned words.
Other than the grep command, the string command in Linux will give us all the printable context from the binary-type file. The below code will display the difference between the strings command on a binary file and the grep command.
Here, when we use a string command in Linux, you will get to see all the printable context from the binary-type file. The above C object code will call the sys command to run the Linux ‘ls’ command.
Example # 4 – String Command for Many Binary Files
Strings command will accept scanning the printable characters from many files at a time and will display the printable characters for the list of files. When we do a grep command in Linux on the strings output of many files, the result will be clear where we would find a file_name and patterned string as given below.
Syntax:
strings -f file_location | grep pattern
Example:
Example #5 – To Find Out for User-Defined Characters
Considering that we would like to scan for “ls” character in the system, we would not get the character as given below.
But, by default, the string command in Linux would be searching for the pattern of a minimum of 4 characters, which will terminate as a ‘NULL’ character. If we would like to modify the total number of characters in the string that we would need to be scanned in the binary-type file, we would use the option ‘-n’ as given below, which will help the string command to result in the string that is minimum with the number of the integer in length.
The below example will scan for ‘ls’ string from the C object file as shown below:
Conclusion
String command in Linux will return each string type of characters that are printable in the file. It is mainly used in determining the file’s contents and extracting the text from binary-type files. A string contains a sequence of finite characters and can also be considered one character. For any file in Linux, the GNU string will print the printable context whose length is at least four characters, followed by a non-printable character. In short, we can say that a string command is used to determine the contents of binary-type or non-text files.
Recommended Articles
We hope that this EDUCBA information on “Linux String Command” was beneficial to you. You can view EDUCBA’s recommended articles for more information.