Kedar Mhaswade Kedar Mhaswade - 1 year ago 39
Bash Question

How to remove file type classifier from the 'ls' command output on oh-my-zsh and Mac?

I suspect this has to do with my

setup on my Mac, but I am not able to figure out. When I do
ls *.sh
on command line, I get something like:**

I understand that by default,
is applied on

-F Display a slash ('/') immediately after each pathname that is a directory, an asterisk ('*') after each that is executable, an at
sign ('@') after each symbolic link, an equals sign ('=') after each
socket, a percent sign ('%') after each white-out, and a vertical bar
('|') after each that is a FIFO.

I really want to list the file names without their classifiers. This is desirable because without that, my

for f in `ls *.sh`
wc -l $f

breaks on executable shell scripts (because of the trailing

wc:*: open: No such file or directory
wc:*: open: No such file or directory

Of course, I can easily work around this. I just wanted to know if I can take care of it on the output of the
command. Also, this works fine when my shell is

Answer Source

There is no need to use ls in order to generate a list of matching files. You can just use the pattern directly:

for f in *.sh
    wc -l $f

This also has the advantage to work with filenames that contain spaces.

Generally speaking: the output of ls is for human consumption, it should never be parsed. Usually file lists can be generated directly with globbing and any information ls might provide can be retrieved by other means (e.g. stat).

That being said:

-F is not enabled by default, hence why there is an option for that. I suspect that ls is an alias. You can check with

whence -v ls

While oh-my-zsh by default creates an alias to ls, -F is not part of the settings.

If you just want to use an unmodified ls (maybe with different options) you can prepend the command precommand modifier:

command ls

This will run the external command ls (usually /bin/ls) instead of an alias or a function.

If you want to get rid of -F permanently, you can either overwrite it somewhere at the very end of your configuration. Or you track down where it is set and change it there. This command might help in finding where the alias is set:

grep -r $(whence ls) ~/.zshrc ~/ ~/.oh-my-zsh

Depending on your setup there might be additional configuration files.