AIX : Displaying non-printable charatcers in files & directories

What happens when you find yourself in a situation where you're totally unable to make changes in a file no matter how much you diff the content of the file. It's because when you had created the contents in the first place, you may have hit a non-printable key on your keyboard, like Shift + G or ^I or something like that

So what do you do to fix this problem? Why you go back to basics. You FIND the non-printable characters first!

Use the cat command with these three options to give you a good idea on how the file was created:

-v = displays non-printing characters as visible characters
-t = displays tab characters as ^I
-e = displays a $ at the end of each line

# cat myfile
This  file has tabs and spaces and ends with a return

# cat -vte myfile
This ^file^G has tabs and spaces and ends^I with a return$

Then there are also times when you list the contents of a directory and you see the file you want to work with but you cannot access it. It might be because you accidentally pressed a control character while creating the name of the file. 

View the contents of the directory by piping the output of ls to cat using its varied options. Identify what the probem is with the file name you are trying to access.

# ls
greatfile myfile

# rm greatfile
No such file

# ls | cat -vt

To fix this file, use one of these three methods!

If you do not need the file any longer, remove it but ensure you key in the control character as part of the file name

1. rm ^Ggreatfile

If you need to keep the file, rename it, also ensuring that you include the control characters as part of the source file name

2. mv ^Ggreatfile greatfile

If you cannot remove the file using method 1, find the i-node number of the file and use the find command with the -inum expression

3. ls -i
130 ^Ggreatfile 127 myfile
find . -inum 130 -exec rm {}\;     *** use rm command with care~

And that folks is how you fix it!


Post a Comment