Thursday, February 20, 2014

Unix: file names containing non printing characters

In Unix a filename can contain non printing characters. Most likely the name will be the result of a mistake but in any case you might want to rename it, remove it, look at its content etc. So let us illustrate this issue. First let us build any filename including the "del" non printable character. So type "test", then press and release "CTRL-v", and finally press and release the "del" key. You will notice something like the below:
$ echo hello > test^?
$ ls test*
test
$ ls -b test*
test\177
$ ls -q test*
test?
To see the content you can take advantage of wild card support where the "?" means any single character matching just use:
$ cat test?
hello
To move it, remove it etc just use wildcards again. Note that "^?" means I have inserted a printable character as explained above:
$ mv test? test2^?
$ rm test2?
hello
You can also use the regular pipes and commands to come up with other ways to go around the issue like the below:
$ cat $(ls | grep test)
hello
$ rm $(ls | grep test)

No comments:

Followers