by rmetzler · 6y ago · 0 comments
It’s discussed on stack-overflow. [0]
Someone even wrote a nodejs tool for this functionality [1], but I would rather have something written in a compiled language.
[0]: https://stackoverflow.com/questions/10574794/how-to-list-onl...
> find . -maxdepth 1 -type f
A little clunky, but there's certainly no need to mess about in JavaScript land. If you use it often, create a shell macro.
$ mkdir -p tmp && touch tmp/a tmp/b tmp/c
$ find ./tmp -maxdepth 1 -type f
./tmp/a
./tmp/c
./tmp/b
$ ls -1 ./tmp
a
b
c $ find ./tmp -maxdepth 1 -type f -exec basename {} \;
b
a
c
I don't know the downsides of using basename though.Thanks to all the helpers, who took their time to propose solutions.
But what I was trying to say is: I would be ok with adding a new command to the tools I use on my machine, VMs and containers. fzf, fd, rg are tools which make my life easier. I would prefer to have the features of fls in ls itself or a tool similar to fls in coreutils, moreutils or another package .
+ $ find /tmp/x ! -path /tmp/x -prune -type f -exec sh -c 'for p; do printf '\''%s\n'\'' "${p##*/}"; done' - {} +
c
b
a
Seems to do the trick and is POSIX only (so works on busybox and mac). You can probably just tack `| sort` at the end if you want it sorted.People value software that's easy to use. That command you typed is a Frankenstein's monster.
$ (cd tmp && find . -maxdepth 1 -type f) ls -1p | grep -v '/$'
You may wrap it in an alias or .bashrc function. I use the reverse (list only directories): function lsd {
ls -1p $* | grep '/$'
}But I don't think it's too hard to solve this problem. A command which filters should also be able to invert the filter. One example for this is grep -v.
Coming from a more low level background but doing front end development from time to time, I am always amazed by the sheer amount of seemingly useless javascript reimplementations of what would be bash one liners.
For your question, how about "find <dir> -type f - maxdepth 1" ?
You can put that in an alias or script in your path