2

Goal: Output .txt file with the full directory path, including name, of all .html files except for those with "txt" or "text" in the .html file name.

I found the following line gives me the desired .txt file with the file's full directory path. The only problem is that it gives me ALL of the folder's contents:

ls -d "$PWD"/* > fileList.txt

Example Results:

/Users/username/Desktop/WebsiteFiles/notes.txt
/Users/username/Desktop/WebsiteFiles/index.html
/Users/username/Desktop/WebsiteFiles/index-TEXT.html
/Users/username/Desktop/WebsiteFiles/answers.html
/Users/username/Desktop/WebsiteFiles/answers_txt.html
/Users/username/Desktop/WebsiteFiles/image.jpg
/Users/username/Desktop/WebsiteFiles/image2.jpg
/Users/username/Desktop/WebsiteFiles/about.html
/Users/username/Desktop/WebsiteFiles/about_TXT.html
/Users/username/Desktop/WebsiteFiles/contact.html
/Users/username/Desktop/WebsiteFiles/contact_text.html
/Users/username/Desktop/WebsiteFiles/images

Desired Results:

/Users/username/Desktop/WebsiteFiles/index.html
/Users/username/Desktop/WebsiteFiles/answers.html
/Users/username/Desktop/WebsiteFiles/about.html
/Users/username/Desktop/WebsiteFiles/contact.html

Experimenting:

I'm fairly new to using the command line. I've been experimenting trying to figure this stuff out. I found that the following find helps find all .html files:

find . -iname '*.html' 

When used on the parent directory it will give me all .html files but not the full directory path, example result:

./index.html
./index-TEXT.html
./answers.html
./answers_txt.html
./about.html
./about_TXT.html
./contact.html
./contact_text.html

I'm not familiar enough with the parameters or assembling these commands and haven't been successful in getting a print of just the .html files without the ones with any of the variation of "text" in the name.

I have a ton of files to find with this and need that .txt file with the full paths. I want to understand this stuff so please provide detailed responses!

2

find will output the found names with the path that you give it, so you can start building the command with

find /Users/username/Desktop/WebsiteFiles

or, if that's where you're located currently,

find "$PWD"

Next, we restrict the found names to only names matching *.html:

find "$PWD" -type f -name '*.html'

If you have both *.html and *.HTML (or *.hTmL) files, and if you want to include these, then change -name to -iname (which does case-insensitive name matching).

I also added -type f on the off chance that you have any directories with names matching *.html (we don't want to see these in the result). -type f restricts the name to those of regular files only.

Then you wanted to remove particular filenames from the result. Names containing the strings txt or text (up or down case). This can be done through negating the -iname test with !:

find "$PWD" -type f -name '*.html' ! -iname "*txt*" ! -iname "*text*"

And there you have it.

Each "predicate" (-type f etc.) acts like a test against the names in the given directory, and there's an implicit logical AND between the tests. If all tests pass, the name is printed.

Running in a temporary directory on my machine, with the files that you have in your directory (just empty files for testing):

$ ls -l
total 24
-rw-r--r--  1 kk  wheel      0 Sep 26 17:47 about.html
-rw-r--r--  1 kk  wheel      0 Sep 26 17:47 about_TXT.html
-rw-r--r--  1 kk  wheel      0 Sep 26 17:47 answers.html
-rw-r--r--  1 kk  wheel      0 Sep 26 17:47 answers_txt.html
-rw-r--r--  1 kk  wheel      0 Sep 26 17:47 contact.html
-rw-r--r--  1 kk  wheel      0 Sep 26 17:47 contact_text.html
-rw-r--r--  1 kk  wheel    596 Sep 26 17:46 files
-rw-r--r--  1 kk  wheel      0 Sep 26 17:47 image.jpg
-rw-r--r--  1 kk  wheel      0 Sep 26 17:47 image2.jpg
-rw-r--r--  1 kk  wheel      0 Sep 26 17:47 images
-rw-r--r--  1 kk  wheel      0 Sep 26 17:47 index-TEXT.html
-rw-r--r--  1 kk  wheel      0 Sep 26 17:47 index.html
-rw-r--r--  1 kk  wheel      0 Sep 26 17:47 notes.txt
-rw-r--r--  1 kk  wheel  10240 Sep 26 19:11 test.tar

$ find "$PWD" -type f -name '*.html' ! -iname "*txt*" ! -iname "*text*"
/tmp/shell-ksh.p56GA7BA/index.html
/tmp/shell-ksh.p56GA7BA/answers.html
/tmp/shell-ksh.p56GA7BA/about.html
/tmp/shell-ksh.p56GA7BA/contact.html
  • 1
    Thank you! This worked for me. And thanks for explaining. I was able to add the output of > fileList.txt to the end and it did exactly what I wanted. – Melissa McCollum Sep 26 '17 at 17:39

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.