skip non printable characters

skip non-printable characters with “strings”

Have you ever seen something like this, when you needed to skip non printable characters in order to see file contents?

[root@linux ~]# less test
"test" may be a binary file.  See it anyway?
[root@linux ~]#

If answer is “yes” and if you were about to use such data file somewhere in your scripts, this might be annoying. Here comes the “strings” command – it prints only the printable characters in the files. You can then safely use data which might potentially contain some non-printable characters.

Quick example with a small compiled C program:

[root@linux ~]# cat test.c
#include <stdio.h>

int main () {
  printf("foo bar\n");
  return 0;
}
[root@linux ~]# gcc test.c -o test
[root@linux ~]# ./test
foo bar
[root@linux ~]#

Check what’s printable:

[root@linux ~]# strings test
/lib64/ld-linux-x86-64.so.2
libc.so.6
puts
__libc_start_main
__gmon_start__
GLIBC_2.2.5
UH-8
UH-8
[]A\A]A^A_
foo bar
;*3$"
GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-39)
crtstuff.c
__JCR_LIST__
deregister_tm_clones
__do_global_dtors_aux
completed.6355
__do_global_dtors_aux_fini_array_entry
frame_dummy
__frame_dummy_init_array_entry
test.c
__FRAME_END__
__JCR_END__
__init_array_end
_DYNAMIC
__init_array_start
__GNU_EH_FRAME_HDR
_GLOBAL_OFFSET_TABLE_
__libc_csu_fini
puts@@GLIBC_2.2.5
_edata
__libc_start_main@@GLIBC_2.2.5
__data_start
__gmon_start__
__dso_handle
_IO_stdin_used
__libc_csu_init
__bss_start
main
__TMC_END__
.symtab
.strtab
.shstrtab
.interp
.note.ABI-tag
.note.gnu.build-id
.gnu.hash
.dynsym
.dynstr
.gnu.version
.gnu.version_r
.rela.dyn
.rela.plt
.init
.text
.fini
.rodata
.eh_frame_hdr
.eh_frame
.init_array
.fini_array
.jcr
.dynamic
.got
.got.plt
.data
.bss
.comment
[root@linux ~]#