Hi,
First, thanks for this project !
I have an issue using the --exec (or --exec-batch) argument with aliases/functions.
It returns most of the cases
[fd error]: Command not found:
According to this issue, I understood that --exec needs a proper program invocation and it does not work with shell built-in so I tried using the bash -c but I still have the same error.
Here is an example:
> ~/Downloads ยป command fd --no-ignore --type f --extension jpg
documents/photo/photo.jpg
> ~/Downloads ยป command fd --no-ignore --type f --extension jpg --exec ls {}
documents/photo/photo.jpg
> ~/Downloads ยป command fd --no-ignore --type f --extension jpg --exec ll {}
[fd error]: Command not found: "ll" "documents/photo/photo.jpg"
> ~/Downloads ยป command fd --no-ignore --type f --extension jpg --exec bash -c "ll {}"
bash: ll: command not found
> ~/Downloads ยป alias ll
alias ll='ls -Al --group-directories-first'
In the next case, mkdir is also an alias however command is execute fine and folder is created (I suppose it's not the alias that is executed because I do not see the verbose message for mkdir)
> ~/Downloads ยป command fd --no-ignore --type f --extension jpg --exec mkdir {//}/test/
> ~/Downloads ยป alias mkdir
alias mkdir='mkdir -pv'
Function example:
> ~/Downloads ยป function lll() {
> command ls
> }
> ~/Downloads ยป command fd --no-ignore --type f --extension jpg --exec lll {}
[fd error]: Command not found: "lll" "documents/photo/photo.jpg"
> ~/Downloads ยป command fd --no-ignore --type f --extension jpg --exec bash -c "lll {}"
bash: lll: command not found
> ~/Downloads ยป type lll
lll is a function
lll ()
{
command ls
}
Or another example without command:
> ~/Downloads ยป function llll() {
> \ls
> }
> ~/Downloads ยป type llll
llll is a function
llll ()
{
\ls
}
> ~/Downloads ยป command fd --no-ignore --type f --extension jpg --exec \ls {}
documents/photo/photo.jpg
> ~/Downloads ยป command fd --no-ignore --type f --extension jpg --exec llll {}
[fd error]: Command not found: "llll" "documents/photo/photo.jpg"
> ~/Downloads ยป command fd --no-ignore --type f --extension jpg --exec bash -c "llll {}"
bash: llll: command not found
fd 7.4.0
My usecase is simple, I have a some functions/aliases defined in .bashrc and I would like to be able to call these functions/aliases on fd results for files/folder. Is there a way to achieve this ?
Thanks for your help.
According to this issue, I understood that
--execneeds a proper program invocation and it does not work with shell built-in so I tried using thebash -cbut I still have the same error.
If you run bash -c "โฆ", you still don't have access to aliases (try bash -c "ll").
You can use bashs -i option (for interactive). This should work:
fd โฆ -X bash -i -c ll
Use -X instead of -x to run the command just once for all arguments. Also, do not use "ll {}", but rather ll {} or just ll. Otherwise, fd will expand this to "ll result1" "ll result2" โฆ.
In the next case, mkdir is also an alias however command is execute fine and folder is created (I suppose it's not the alias that is executed because I do not see the verbose message for
mkdir)
Yes, it will probably call /usr/bin/mkdir directly.
Thanks for your answer, unfortunately I tried these but none worked, here is the output:
> ~/wallpapers ยป ll
total 4248
-rwxr-xr-x 1 steir steir 396 May 22 17:39 rename.sh
-rw-r--r-- 1 steir steir 639532 May 7 17:44 wallpaper_001.jpg
-rw-r--r-- 1 steir steir 804017 May 7 17:44 wallpaper_002.jpg
-rw-r--r-- 1 steir steir 598474 May 7 17:44 wallpaper_003.jpg
-rw-r--r-- 1 steir steir 861147 May 7 17:44 wallpaper_004.jpg
-rw-r--r-- 1 steir steir 483533 May 7 17:44 wallpaper_005.jpg
-rw-r--r-- 1 steir steir 940807 May 7 17:44 wallpaper_006.jpg
> ~/wallpapers ยป alias ll
alias ll='ls -Al --group-directories-first'
> ~/wallpapers ยป fd --no-ignore --type f --extension jpg
wallpaper_001.jpg
wallpaper_002.jpg
wallpaper_003.jpg
wallpaper_004.jpg
wallpaper_005.jpg
wallpaper_006.jpg
> ~/wallpapers ยป fd --no-ignore --type f --extension jpg -X bash -c "ll"
wallpaper_003.jpg: ll: command not found
> ~/wallpapers ยป fd --no-ignore --type f --extension jpg -x bash -c "ll"
wallpaper_003.jpg: ll: command not found
wallpaper_005.jpg: ll: command not found
wallpaper_002.jpg: ll: command not found
wallpaper_006.jpg: ll: command not found
wallpaper_001.jpg: ll: command not found
wallpaper_004.jpg: ll: command not found
> ~/wallpapers ยป fd --no-ignore --type f --extension jpg -X bash -i -c ll
total 4248
-rwxr-xr-x 1 steir steir 396 May 22 17:39 rename.sh
-rw-r--r-- 1 steir steir 639532 May 7 17:44 wallpaper_001.jpg
-rw-r--r-- 1 steir steir 804017 May 7 17:44 wallpaper_002.jpg
-rw-r--r-- 1 steir steir 598474 May 7 17:44 wallpaper_003.jpg
-rw-r--r-- 1 steir steir 861147 May 7 17:44 wallpaper_004.jpg
-rw-r--r-- 1 steir steir 483533 May 7 17:44 wallpaper_005.jpg
-rw-r--r-- 1 steir steir 940807 May 7 17:44 wallpaper_006.jpg
Only the last command is what I suggested. Isn't the output exactly as expected?
The command fd --no-ignore --type f --extension jpg only find jpg images, I was expecting the ll alias to print only the results, but it included the script rename.sh
The ll command printed the whole content of the folder, it was not executed on the results returned by fd.
> ~/Pictures ยป ll
total 4
drwxr-xr-x 2 steir steir 4096 Oct 24 14:05 wallpapers
> ~/Pictures ยป fd --no-ignore --type f --extension jpg
wallpapers/wallpaper_001.jpg
wallpapers/wallpaper_002.jpg
wallpapers/wallpaper_003.jpg
wallpapers/wallpaper_004.jpg
wallpapers/wallpaper_005.jpg
wallpapers/wallpaper_006.jpg
> ~/Pictures ยป fd --no-ignore --type f --extension jpg -X bash -i -c ll
total 4
drwxr-xr-x 2 steir steir 4096 Oct 24 14:05 wallpapers
> ~/Pictures ยป fd --no-ignore --type f --extension jpg -X bash -i -c ll {}
total 4
drwxr-xr-x 2 steir steir 4096 Oct 24 14:05 wallpapers
You are right, I missed that. That doesn't actually work. I'm afraid it's not that simple, in this case. Let me think about this...
The following should work:
fd โฆ -X bash -ic 'll "$@"' --
Obviously, this is far from optimal :smile:. A few other options are documented here: https://stackoverflow.com/questions/7653446/using-an-alias-in-find-exec
The one which uses ${BASH_ALIASES[โฆ]} is also quite neat.
If you really want to use this in practice, I would suggest that you place a few bash scripts in your PATH that are named like your aliases.
That works well ๐ Thank !
I will certainly have to add few scripts in PATH to make it easier.
Thanks for your help.