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.

  • You might want to look at the find command, which is better suited for this task. – Panki Nov 2 '18 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 '18 at 9:46
  • 2
    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 '18 at 9:47

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'
| improve this answer | |
  • This i exactly what I was looking for. I found out that it wouldn't show me the files on the desktop so I added a "&& find *.xls" after your expression and the problem is now solved. Thank you! – TOY Nov 2 '18 at 10:06
  • @TOY, if your current directory (indicated by .) isn't "above" your desktop directory, then you could add your desktop directory to the list of paths that you want find to look in: find . ~/Desktop ..., for example. – Jeff Schaller Nov 2 '18 at 10:58

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.