I am doing an assignment for school and using putty to complete the assignment. The format of the assignment does not allow for much variation and thus only one answer is sufficient enough to allow me to move on to the next question. They also want the shortest possible pathnames and commands used to get to the answer.

Your current directory is sample_dir. Find the records in cars2 that contain the string "ford" and display the number of characters in the result. sample_dir2

 -- sample_dir
    |-- admin
    |-- cambridge
    |   `-- security
    |       |-- annex
    |       `-- parking
    |-- history.exe
    |-- markham
    |   |-- annex
    |   |-- building1
    |   `-- parking
    `-- stenton
        |-- f1
        |-- f12
        |-- f2
        |-- gen_ed
        |   |-- Holidays
        |   `-- cars2
        |-- lib_arts
        |   `-- english.txt
        `-- phone_directory
        |   |-- Holidays
        |   `-- cars2
        |-- lib_arts
        |   `-- english.txt
        `-- phone_directory

I've tried grep -c ford stenton/gen_ed/cars2 and different options using grep. Is this along the right path or am I way off?

  • 2
    I don't think the "assignment" tag is for help with assignments... – Joseph R. Nov 15 '14 at 10:50
  • One of the difficulties in these sorts of questions is that there are a myriad of ways to achieve the end result. In your specific case the result depends a lot on what you have been previously taught—which we don't know. – forquare Aug 17 '15 at 7:18
  • This is ULI101 at Seneca College, which we get year after year. All assignments "MUST consist of the student’s OWN work". – JdeBP Oct 22 '16 at 1:53

To display the lines including ford in any cars2 file, you could use:

find . -name cars2 -exec grep ford {} \; 

To count the global number of characters of the records:

find . -name cars2 -exec grep -h ford {} \; | wc -c
  • Another option is find -name cars2 | xargs grep ford | wc -c. – ott-- Aug 16 '15 at 21:04
  • Rather than using find, since we know the structure of the directories, wouldn't use of globbing be more elegant? Something more like grep ford stenton/*/cars2 | wc -c? – forquare Aug 17 '15 at 7:16

The answer would be:

 grep "ford" stenton/gen_ed/cars2 | wc -c
  • 2
    This answer doesn't provide an answer that has not been given already. Recommend deletion. – MelBurslan Mar 16 '16 at 20:15
  • He attempted this question multiple times giving him/her the answer would be appropriate. – Programming Boy Mar 16 '16 at 20:30


grep ford stenton/gen_ed/cars2 -r

The way you would do this is simple

grep ford stenton/gen_ed/cars2 | wc -m

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.