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...
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
cfrom the man page:
> The format is identical to that produced by ``ls -dgils''.
$ find ./tmp -maxdepth 1 -type f -printf '%f\n' $ find ./tmp -maxdepth 1 -type f -printf '%f\n'
find: -printf: unknown primary or operatorI also use MacOS and I have setup the gnu userland on my local so it matches the linux environment on our servers and containers.
It's not about convenience. It is about having a development environment that matches the target runtime environment.
You can run into nasty surprises when the default behavior of a tool in your dev environment is different than in your production environment.
I've been writing shell scripts professionally for over 20 years and I have always taken this approach and it has served me well.
Works for me on OSX.
+ $ 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.
fls () {
find . -maxdepth 1 -type f
}
vs fls () {
find . ! -path . -prune \
-type f \
-exec sh -c 'for p; do printf '\''%s\n'\'' "${p##*/}"; done' - {} +
}
The user would just type `fls` in the directory they were exploring. $ (cd tmp && find . -maxdepth 1 -type f) $ 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 .
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