Locate search is not limitable to current directory:

I am trying to learn so I decided to write this script

cd /usr/share/doc && ls -R | grep "\.html" | sudo tee htmldoclist.txt

Then I want to pipe the doc list into locate command similar to

locate $(head -n 1 htmldoclist.txt)

but I want to use cat.

I want to save the output of this to hlinks.txt and use sed to append file:// to line starts with pipe to sed as follows:

| sed -e 's/^/file:///g'

I expect to create a bunch of terminal hyperlinks for my documentation files stored in /usr/share/docs

  • And what is the exact problem, what are you not able to resolve? – Eduardo Trápani Jan 29 '20 at 22:14
  • locate command is not searching within current directory thus it is very slow I tried the -P option too. and dirname $(ls -R /usr/share/doc | grep "\.html") – noob_sabidiot Jan 29 '20 at 22:31
  • If I could grep and get previous line that would help too. – noob_sabidiot Jan 29 '20 at 22:34

It looks like find would be more appropriate for the task, it will give your the list of files and filter them and then you can use sed:

find /usr/share/doc -iname '*html' | sed  's|^|file://|'

But find does a lot more, it can also format the output for you:

find /usr/share/doc -iname '*html' -printf 'file://%p\n'
  • Thank you very much! Worked much better and creates clickable links Ctrl click. – noob_sabidiot Jan 29 '20 at 23:17
  • @noob_sabidiot please accept the answer if it worked for you. That's how the system works. – Eduardo Trápani Jan 29 '20 at 23:19
  • find /usr/share/doc -iname '*html' -printf 'file://%p\n' | less – noob_sabidiot Jan 29 '20 at 23:26
  • Now to make an alias docs="`find /usr/share/doc -iname '*html' -printf 'file://%p\n'`" – noob_sabidiot Jan 29 '20 at 23:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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