Shell – Find files in specific directories

directoryfindgreplsshell

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.

Best Answer

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'
Related Question