4 ms·
find doesn't return the same output as ls when run on another directory. Maybe there is a switch to do this? I'm not sure. $ mkdir -p tmp && touch tmp/a tm
by rmetzler 6y ago
find doesn't return the same output as ls when run on another directory. Maybe there is a switch to do this? I'm not sure.
$ 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
- RX14 6y agoThe -ls flag to find prints the same as ls
- rmetzler 6y agonot quite. from the man page: > The format is identical to that produced by ``ls -dgils''.
- fooblat 6y agoI think this is what you are looking for $ find ./tmp -maxdepth 1 -type f -printf '%f\n'
- rmetzler 6y agoI'm on MacOS... :-/ $ find ./tmp -maxdepth 1 -type f -printf '%f\n' find: -printf: unknown primary or operator
- fooblat 6y agoIf you use brew or macports you can install the coreutils package and then you're good to go with gnu find. I also use MacOS and I have setup the gnu userland on my local so it matches the linux environment on our servers and containers.
- gpanders 6y agoYou’re right that defaulting to the GNU coreutils is probably more convenient. However, NOT doing this is a good way to ensure that any scripts you write remain portable and don’t use GNU extensions. That’s the reason I stick with the default BSD coreutils on macOS.
- fooblat 6y ago> You’re right that defaulting to the GNU coreutils is probably more convenient. 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.
- saagarjha 6y agoYou could also try restricting yourself to POSIX shell.
- Normille 6y agofind <directory path> -type f -d 1 Works for me on OSX.
- pbhjpbhj 6y agoNot got a shell handy, but "-exec ls"?
- gray_-_wolf 6y ago+ $ 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.
- nindalf 6y agoThis answer reminds me of the classic HN comment - why use Dropbox when "you can already build such a system yourself quite trivially by getting an FTP account, mounting it locally with curlftpfs, and then using SVN or CVS on the mounted filesystem. From Windows or Mac, this FTP account could be accessed through built-in software." People value software that's easy to use. That command you typed is a Frankenstein's monster. https://news.ycombinator.com/item?id=9224 https://news.ycombinator.com/item?id=9224
- tingletech 6y agoThis had become a discussion of the best way to write the "shell macro" version that could replace the node.js program 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.
- nrclark 6y agoYou can run it in a subshell pretty easily, like this: $ (cd tmp && find . -maxdepth 1 -type f)
- iib 6y agoYou can also use basename with the find command $ find ./tmp -maxdepth 1 -type f -exec basename {} \; b a c I don't know the downsides of using basename though.
- rmetzler 6y agofrom all the solutions, this seems to be the most simple with readily available tools. Unfortunately it's still not as short as I would like it to be and I hope I remember the syntax with -exec $CMD {} \; the next time I need it. 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 .