Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to include current directory #364

Closed
ghost opened this issue Nov 19, 2018 · 19 comments
Closed

Option to include current directory #364

ghost opened this issue Nov 19, 2018 · 19 comments
Labels

Comments

@ghost
Copy link

ghost commented Nov 19, 2018

The find tool includes the current directory:

$ find /var -maxdepth 1
/var
/var/cache
/var/lib
/var/log
/var/run
/var/tmp

$ cd /var
$ find -maxdepth 1
.
./cache
./lib
./log
./run
./tmp

and the ls tool has an option for it:

$ ls -a /var
.  ..  cache  lib  log  run  tmp

$ cd /var
$ ls -a
.  ..  cache  lib  log  run  tmp
@tmccombs
Copy link
Collaborator

tmccombs commented Nov 19, 2018

Pedantic: the "-a" option for ls also shows the parent directory ".." and includes hidden (dot) files.

What is the use case for this?

@sharkdp
Copy link
Owner

sharkdp commented Dec 15, 2018

Closing this, as there is no further feedback.

@sharkdp sharkdp closed this as completed Dec 15, 2018
@ghost
Copy link
Author

ghost commented Dec 15, 2018

@sharkdp apologies - i didnt realize feedback was needed

we already have -H to cover hidden items, so we only need to deal with . and
..

we could introduce an option -n, my preference would be that -n will list
. but not .., so that using -H -n would give you a find like output

please reopen - thank you

@sharkdp sharkdp reopened this Dec 16, 2018
@sharkdp
Copy link
Owner

sharkdp commented Dec 16, 2018

Ok. Could you please tell us what your use case is?

@tmccombs
Copy link
Collaborator

Just because another program does something a certain way, doesn't necessarily mean it is valuable. Adding functionality just because that's the way find does it seems like wasted effort to me. Personally I have never needed find or ls to include the current directory, and have sometimes had to exclude it when piping it to another command.

Have you found that functionality useful? If so, for what kind of things?

@sharkdp
Copy link
Owner

sharkdp commented Jan 1, 2019

Ok, I'm going to close this for now. I still don't see the need for this, and there are simple ways to work around this in scripting (echo "."; fd ...)

@sharkdp sharkdp closed this as completed Jan 1, 2019
@ghost
Copy link
Author

ghost commented Jan 8, 2019

@sharkdp I remembered my use case - ok with find say you want to operate on a subtree, you can do this:

$ find gamma
gamma
gamma/delta
gamma/epsilon.txt

and then

find gamma -exec SOMETHING

and it will operate on everything including gamma - but FD cant do this, see:

$ fd --search-path gamma
gamma\delta
gamma\epsilon.txt

so can we reopen this issue?

@sharkdp sharkdp reopened this Jan 8, 2019
@sharkdp
Copy link
Owner

sharkdp commented Jan 8, 2019

Sorry to be insistent here: what is SOMETHING in this case? What kind of command do you apply to every file and directory within a given directory? Thinks like chmod and chown have -R arguments.

@ghost
Copy link
Author

ghost commented Jan 8, 2019

@sharkdp i would really rather not divulge, as i dont want this issue to be pigeonholed or judged on just the basis of whatever specific example i give, as i feel strongly that this is a general and valid issue however one use case might be:

find gamma -exec touch {} +

again this isnt a great example but i dont think original issue should be judged on a specific example

@sharkdp
Copy link
Owner

sharkdp commented Jan 8, 2019

Ok, let's keep this open for a while and try to get some other opinions.

@miguno
Copy link

miguno commented Apr 25, 2022

I was running into this, too.

Building https://github.com/crawl/crawl, I was looking for possible locations of any generated "crawl" files/binaries:

$ cd crawl/crawl-ref/source
$ ls -l | wc -l
1338 # lots of files/dirs in current directory!

# Let's try to find "crawl" files/dirs. Seems like there are only two of them...
$ fd -g crawl
android-project/src/org/develz/crawl
mac/crawl

# But the one I was looking for, the third one, was actually in the current directory
$ find . -name crawl
./android-project/src/org/develz/crawl
./mac/crawl
./crawl  # <<< this one

It really caught me by surprise that fd does NOT include the current directory by default. At least it should have an option to include the cwd, as per this GH issue.

Personally I have never needed find or ls to include the current directory, and have sometimes had to exclude it when piping it to another command.

Opposite experience here, fwiw. Frankly, I would have expected the default to include the cwd, and if anything have a separate option to exclude the cwd. From a user experience POV, excluding the cwd doesn't make sense to me.

Yes, fd doesn't need to do X just because other tools do so, but the CLI tools I commonly use always include the current directory when they operate in "recurse" mode. A few examples:

  • ls and ls -R
  • exa and exa -R
  • find (recurse by default)
  • fzf (recurse by default)
  • git, including git grep (recurse by default)
  • rg aka ripgrep (recurse by default)

Programming language examples where standard behavior when recursing a directory tree does include the cwd:

Personally, I don't know any tool where cwd would not be included. Well, now I know fd does not. ;-)

PS: I ended up here because I was googling for how to include the cwd when running fd, the help/man page didn't answer that question for me.

@tmccombs
Copy link
Collaborator

@miguno I think you are seeing something else. Probably, ./crawl is ignored by your .gitignore file? (See https://github.com/sharkdp/fd#troubleshooting). fd does include files in the current working directory. What this issue is about is including the current working directory in the output. So in your example, including a "." line in the output (or /prefix-path/crawl/crawl-ref/source if using the --absolute-path option)

@miguno
Copy link

miguno commented Apr 26, 2022

@tmccombs: Doh, you are totally right. It was indeed a .gitignore file! Sorry for necroing this thread for the wrong reasons.

@salvaft
Copy link

salvaft commented May 31, 2023

My use case is the following:
In a make file I can use find to tell my target that it depends on the contents of a given directory:
filename.jar: $(shell find PATH)

Using find also returns the parent directory. If a file is removed, the parent directory modification date changes and make accounts for removed files.

If I use fd:
filename.jar: $(shell fd -t d -t f . PATH)

I get the nested folders individually but not the the parent (for the case a file is deleted in the "root" of PATH. Do you know a workaround in order to show the parent folder?

@tavianator
Copy link
Collaborator

I think the easiest workaround is something like

filename.jar: PATH $(shell fd . PATH)

i.e. just listing PATH explicitly as a prerequisite. (Personally I would just use find in a Makefile as it's more portable.)

@2ax3ax5
Copy link

2ax3ax5 commented Sep 20, 2023

As a user, I find weird that fd doesn't have an option for including the current directory.

@mahdi739
Copy link

mahdi739 commented Mar 2, 2024

Hi. My use case is that I want to perform a global search using fzf. I would like to set its default command as follows:

For searching across drives:

$env:FZF_DEFAULT_COMMAND = 'fd . C:\ D:\ E:\ -u'

Additionally, I want to include the drive letters themselves in the search results.
Alternatively, I want to specify specific folders as the source:

$env:FZF_DEFAULT_COMMAND = 'fd . $env:UserProfile\Downloads "D:\IDM Downloads" -u'

In this case, I also want to include the source folders in the result list so that I can choose them as the paste destination, for example.


Edit: Here’s an alternative approach that worked for me:

$env:FZF_DEFAULT_COMMAND = 'echo "C:\" && echo "D:\" && echo "E:\" && fd . C:\ D:\ E:\ -u'

@rieje
Copy link

rieje commented May 8, 2024

My use case (for including target in results) is this example to remove all empty directories recursively. Correct me if I'm wrong but there's no equivalent to this find command:

find . -name testdir -type d -empty -delete

With fd:

fd -t e -t d  testdir -X rmdir '{}'

testdir itself is not removed.

target=testdir; fd -t e -t d . "$target" -X rmdir {} "$target" only removes testdir if there are results.

I'm curious of a workaround that is relatively simple and doesn't involved calling rmdir more than once.

P.S. Unrelated, but n my attempt above, I was also thinking that exit codes to reflect whether or not there are results would be be useful. fd -q is not compatible with -x, -X. Again, workarounds like fd . . -X printf '%s\n' {} | wc -l involves unnecessarily calling external commands.

@amesaine
Copy link

My use case is opening directories immediately with nvim through fzf. Sometimes, I just want the parent dir instead of choosing any of the subdirs.

Here is my workaround for it.

nvim $({ echo $XDG_CONFIG_HOME & fd -L -d 1 -t d . $XDG_CONFIG_HOME } | fzf)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants