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*
$ ls -b test*
$ ls -q test*
To see the content you can take advantage of wild card support where the "?" means any single character matching just use:
$ cat test?
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?
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)
$ rm $(ls | grep test)

No comments: