How to run “find” command on current directory only and not on sub-directories

Question: How to perform find commands on the current directory, without going into the subdirectories.

Example Scenario:
– Remove all the files in the current directory ONLY that are a week old.

The Solution

1. Add the following operations to the desired find commands:

! \( -type d ! -name {DIRECTORY NAME} -prune \)

2. Substituting the directory to be searched for {DIRECTORY NAME}

Example Solution:

# find . ! \( -type d ! -name . -prune \) -mtime 7 -exec rm {} \;

The above command will only search in the current directory (.) and not the sub-directories under it and will remove file that are older than 1 week.

Related Post