Move regular files below workspace whose names end in .tmp to archive.
Include workspace/sub in the search. The two .tmp filenames are different.
workspace/
├── a.tmp
├── keep.txt
└── sub/
└── b.tmp
archive/Which command is appropriate?
find searches recursively below its starting point. Combine -type f and -name, then pass the matching paths to mv with -exec ... {} +.
Detailed explanation
mv workspace/*.tmp archive/Incorrect. A normal * does not cross a directory separator, so sub/b.tmp is not selected.
Incorrect. A normal * does not cross a directory separator, so sub/b.tmp is not selected.
find workspace -type f -name '*.tmp' -exec mv -t archive -- {} +Correct. It recursively finds regular files and passes the matching paths to mv in batches.
Correct. It recursively finds regular files and passes the matching paths to mv in batches.
find workspace -type d -name '*.tmp' -exec mv -t archive -- {} +Incorrect. -type d selects directories, while this task requires regular files with -type f.
Incorrect. -type d selects directories, while this task requires regular files with -type f.
find workspace -type f -name '*.tmp' -deleteIncorrect. -delete removes matching files instead of moving them.
Incorrect. -delete removes matching files instead of moving them.
Try it yourself
An example you can run in a temporary verification environment.
mkdir -p workspace/sub archive
touch workspace/a.tmp workspace/keep.txt workspace/sub/b.tmp
find workspace -type f -name '*.tmp' \
-exec mv -t archive -- {} +
find . -maxdepth 3 -type f -printf '%P\n' | sortExpected result
archive/a.tmp
archive/b.tmp
workspace/keep.txtKey points
- The search scope of a glob versus find
- Filtering with -type f and -name
- Handling same-name collisions separately in production
Notes
- Environment:
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.