0

I want to find all html files and only show the file names, not the full file path, what I tried:

find /home/irakli/Desktop/irakli_linux -print | grep -i '.*[.]html'

Results:

/home/irakli/Desktop/irakli_linux/htmll/10.html
/home/irakli/Desktop/irakli_linux/htmll/11.html
/home/irakli/Desktop/irakli_linux/htmll/12.html
/home/irakli/Desktop/irakli_linux/htmll/13.html
/home/irakli/Desktop/irakli_linux/htmll/14.html
/home/irakli/Desktop/irakli_linux/htmll/15.html

I only want 1.html 2.html ...

  • @Sergiy I doubt this is a duplicate of How to Search for Files Recursively into Subdirectories. This one particularly asks how to print just the filename (without the path) while the other one asks for find in general. – PerlDuck Dec 30 '18 at 11:56
  • @PerlDuck Admittedly, the many top answers don't mention printing only filename without path, however IMHO it's a matter of reading the manual and adding proper flag to the command. I have, however, edited my own answer there to mention some of the flags find has, so hopefully that covers it. Alternatively, askubuntu.com/a/651321/295286 would probably be a more appropriate, and it pretty much same question in the title. Feel free to vote on reopening, though. – Sergiy Kolodyazhnyy Dec 30 '18 at 12:01
  • @SergiyKolodyazhnyy Congrats on command-line gold :) This seems more of a duplicate for printing basename only? – WinEunuuchs2Unix Dec 30 '18 at 14:46
  • @WinEunuuchs2Unix Hi, thanks. I've had command-line gold for about 2-3 years now. Check the list of duplicates to this post - there's two, How do you output the filename in find command while using -exec? addresses that issue. – Sergiy Kolodyazhnyy Dec 30 '18 at 22:53
4

There is no need to use grep, find can do exactly what you seek.

Use:

find  -iname "*.html" -printf "%f\n"

It will look for all html files and only prints out their name.

If you want all names at the same line:

find  -iname "*.html" -printf "%f "
  • It's works, can you please tell me what's is %f\n? – Irakli Dec 30 '18 at 11:14
  • %f file name, \n, a new line. – Ravexina Dec 30 '18 at 11:16
  • 1
    @Irakli See manpages.ubuntu.com/manpages/bionic/man1/find.1.html and scroll down to -printf. There are various other %something flags you can use, e.g. %s for the file's size. – PerlDuck Dec 30 '18 at 11:19
  • "%f\n" maybe better in case some filenames have spaces in them. – Justice for Monica Dec 30 '18 at 11:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.