I’m using a Mac ssh’ing into a headless RedHat box, and I need to create a list of files for a downstream program to operate on.
When I redirect the output of ls, the file that’s created has a bunch of escape characters, and my downstream scripts can’t read in the filenames properly. When I create and cat the file, everything looks fine:
sdt5z@franklin:~/cufflinks$ ls *.pbs
01_SL8426.pbs 03_SL8428.pbs 09_SL8891.pbs
02_SL8427.pbs 04_SL8429.pbs
sdt5z@franklin:~/cufflinks$ ls *.pbs > myfiles.txt
sdt5z@franklin:~/cufflinks$ cat myfiles.txt
01_SL8426.pbs
02_SL8427.pbs
03_SL8428.pbs
04_SL8429.pbs
09_SL8891.pbs
But if I open the file in vim, this is what it looks like (all the little ^[[ and ^[ characters are colored blue in my terminal emulator):
1 ^[[0m^[[0m01_SL8426.pbs^[[0m
2 ^[[0m02_SL8427.pbs^[[0m
3 ^[[0m03_SL8428.pbs^[[0m
4 ^[[0m04_SL8429.pbs^[[0m
5 ^[[0m09_SL8891.pbs^[[0m
6 ^[[m
~
~
~
~
Running the file command shows that this file has escape sequences.
sdt5z@franklin:~/cufflinks$ file myfiles.txt
myfiles.txt: ASCII text, with escape sequences
My question is, how can I very simply ls the files, redirect those to a file, without any escape characters? What causes this in the first place?
Thanks.
myfiles.txt: ASCII text, with escape sequences
Your
lslooks like it’s aliased to something like ‘ls --color‘. If you unalias it, you will not get any colouring, which is where the escape characters are coming from.alias | grep lswill tell you. I leavelsunaliased and instead alias something likelto ‘ls --color -F‘.