Find files in specific directories

27,886

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'
Share:
27,886

Related videos on Youtube

TOY
Author by

TOY

Updated on September 18, 2022

Comments

  • TOY
    TOY almost 2 years

    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.

  • TOY
    TOY over 5 years
    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!
  • Jeff Schaller
    Jeff Schaller over 5 years
    @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.