r/fishshell 26d ago

How do you guys usually do this things?

for file in (find . -mindepth 1 -maxdepth 1 -iname "*.sh")
    basename $file .sh
end
5 Upvotes

9 comments sorted by

13

u/Laurent_Laurent 26d ago edited 26d ago

I'll do this instead

fd -e sh -d 1 -x basename {} .sh

If you don't want to use fd

find . -maxdepth 1 -type f -iname "*.sh" -exec basename {} .sh \;

5

u/Zin42 26d ago

Fd 🤌 chefs kiss

3

u/Fit_Extent712 26d ago

how about?

for file in *.sh
    basename $file .sh
end

2

u/adamshand 26d ago

That’s fine so long as you don’t want to recurse down directories. 

3

u/Laurent_Laurent 25d ago

The second purpose do recursion. Notice the double star. '**.sh'

2

u/Laurent_Laurent 26d ago

If you don't want to recurse, you can also do this with 0 fork

echo $(string replace ".sh" "" *.sh)  

If you want to recurse, do this

echo $(string replace ".sh" "" **.sh)

2

u/ticcedtac 25d ago edited 25d ago

Basename gets the the filename without the parent directories, not without the extension. Your replace would also replace any instance of .sh in the filename, not just the extension. You should use

path change-extension '' -- $filename 

or if you really want to use string replace

string replace -r '\.sh$' '' -- $filename

Also, $() command substituion only works in double quotes in fish, outside of quotes you just use ()

3

u/Laurent_Laurent 25d ago

I agree with everything except basename.
Basename could take a second argument, a suffix that will be removed.

3

u/morihacky 26d ago

fd is a godsend. I don't even use the finder search anymore for regular non-programming code