LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to get file path for specific type of file on my server? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-get-file-path-for-specific-type-of-file-on-my-server-4175472531/)

Ferodactyl 08-07-2013 03:30 PM

How to get file path for specific type of file on my server?
 
I need to find the file path to all the .html files stored in my directory so that I can easily create links to them for a site map.

How can I do this?

I have tried ls -R | grep \.html$

It does pull out all the html files in my directory but doesn't show the path to them so I can't create the link.

The files are in many different sub-folders.

Thanks for anyone's input. Please let me know if you need more details or if I should rephrase my question - I am totally new to Linux so am not sure if I am explaining my needs correctly.

Firerat 08-07-2013 03:35 PM

purpose built tool for this is find

Code:

find -name "*.html"
^^ very basic, will find files which end .html, relative to current directory

Code:

find $PWD -name "*.html"
the same, but since we give full path to current directory ( with $PWD env var. )
it prints the full path


many more options to find

Code:

man find
for details

Ferodactyl 08-07-2013 03:54 PM

This is really amazing. Thank you! :)

Just one more question. Is there anything I can add to that command in order to stop it from displaying certain files? I have some files called Preview.html that I don't need to display, so could I do something to show all .html files except Preview.html?

dt64 08-07-2013 03:58 PM

Code:

locate .html > all_html_files.txt
would give you a file including all your html files.

If you recently added some more files you could run
Code:

updatedb
before running the above. updatedb usually runs once a day automatically.

This way of doing it might be quicker on file systems with loads of files (especially if you don't have to run the updatedb bit) but would find any html files on your system. You might need to grep out your relevant path/subdirs.

Firerat 08-07-2013 04:02 PM

more than a few

probably the simplest is to pipe the output of find through grep

Code:

find $PWD -name "*.html" | grep -v Preview.html
grep is normally used to show matching lines ( or patterns )
But here we use the -v flag, which inverts it

man grep


you can also get find to do this
Code:

find $PWD -name "*.html" ! -name "Preview.html"
here the ! is NOT , so we are looking for files ending .html AND NOT named Preview.html

with find whitespace implies AND,
Code:

find $PWD -name "*.html" -a ! -name "Preview.html"
is the same, but you won't see it all that often
you can use -o for OR
It can get quite complicated, but once you get into the swing of things you are off..

Ferodactyl 08-08-2013 08:21 AM

Quote:

Originally Posted by Firerat (Post 5005114)
more than a few

probably the simplest is to pipe the output of find through grep

Code:

find $PWD -name "*.html" | grep -v Preview.html
grep is normally used to show matching lines ( or patterns )
But here we use the -v flag, which inverts it

man grep


you can also get find to do this
Code:

find $PWD -name "*.html" ! -name "Preview.html"
here the ! is NOT , so we are looking for files ending .html AND NOT named Preview.html

with find whitespace implies AND,
Code:

find $PWD -name "*.html" -a ! -name "Preview.html"
is the same, but you won't see it all that often
you can use -o for OR
It can get quite complicated, but once you get into the swing of things you are off..

Thank you!


All times are GMT -5. The time now is 01:22 AM.