[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: The ls command



Greg Wooledge (HE12025-10-31):
> hobbit:~$ printf '%s\n' -*
> --K'
> .pdf

I had forgotten that printf will repeat its format string entirely if it
has extra arguments. It is both abominable and genius.

> Now, arguably ls's output may be better for some use cases, and worse
> for others.

ls can sort the list differently; printf cannot. Zsh can at globbing, sh
cannot, bash I do not know.

At this point it is important to remind people that the output of ls is
meant for users, not programs. A script that uses ls for anything else
than showing a list of files to the operator, a script that reads the
output of ls and parses it, is almost certainly broken when it
encounters some special characters, and if not it is at least needlessly
complicated.

You'll have to take that into consideration for each

>				   printf, awkward as it may be, is the
> only 100% sure way to get exactly the output you want in bash.

cat with a heredoc might do the trick too.

> Finally, tab completion is your friend.  If you believe there's only
> one filename beginning with a hyphen, and you want to rename it,
> you can just start typing
> 
>     mv ./-

If we know there is only one, we can just do:

mv -- -* this_file_had_a_stupid_name.whatever

And good tab completions will show a list or iterate through the options
when called multiple times.

With zsh, you can also call tab on a glob pattern, it will expand into
the list of files.

Regards,

-- 
  Nicolas George


Reply to: