11

I have a college exercise which is "Find all files which name ends in ".xls" of a directory and sub-directories that have the word "SCHEDULE", without using pipes and using only some of the commands GREP, FIND, CUT, PASTE or LS

I have reached this command:

ls *.xls /users/home/DESKTOP/*SCHEDULE

This shows me only the .xls files on the Desktop and opens all directories with SCHEDULE on the name but when it does it it shows me all the files on the directories insted of only the .xls ones.

3
  • You might want to look at the find command, which is better suited for this task.
    – Panki
    Nov 2, 2018 at 9:45
  • I have tried find with the same command but the output is the same but shown with the find command syntax
    – TOY
    Nov 2, 2018 at 9:46
  • 3
    Well, you obviously need to specify some flags for find to know what it's looking for. I won't spoil the fun here, because homework is supposed to teach you something. Read 'man find'
    – Panki
    Nov 2, 2018 at 9:47

1 Answer 1

22

Assuming that by "file" they mean "regular file", as opposed to directory, symbolic link, socket, named pipe etc.

To find all regular files that have a filename suffix .xls and that reside in or below a directory in the current directory that contain the string SCHEDULE in its name:

find . -type f -path '*SCHEDULE*/*' -name '*.xls'

With -type f we test the file type of the thing that find is currently processing. If it's a regular file (the f type), the next test is considered (otherwise, if it's anything but a file, the next thing is examined).

The -path test is a test agains the complete pathname to the file that find is currently examining. If this pathname matches *SCHEDULE*/*, the next test will be considered. The pattern will only match SCHEDULE in directory names (not in the final filename) due to the / later in the pattern.

The last test is a test against the filename itself, and it will succeed if the filename ends with .xls.

Any pathname that passes all tests will by default be printed.

You could also shorten the command into

find . -type f -path '*SCHEDULE*/*.xls'
2
  • Hello Kusalananda Thank you for your detailed explanation, can you guide how we can redirect output for this since {} \; or -printf is not working for me
    – Samurai
    2 days ago
  • @Samurai Output is redirected as with any command, with > followed by a filename.
    – Kusalananda
    yesterday

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.