Explain find's -path and -prune options

-path works exactly like -name, but applies the pattern to the entire pathname of the file being examined, instead of to the last component.

-prune forbids descending below the found file, in case it was a directory.

Putting it all together, the command

find $HOME -path $HOME/$dir_name -prune -o -name "*$file_suffix" -exec cp {} $HOME/$dir_name/ \;
  1. Starts looking for files in $HOME.
  2. If it finds a file matching $HOME/$dir_name it won't go below it ("prunes" the subdirectory).
  3. Otherwise (-o) if it finds a file matching *$file_suffix copies it into $HOME/$dir_name/.

The idea seems to be make a backup of some of the contents of $HOME in a subdirectory of $HOME. The parts with -prune is obviously necessary in order to avoid making backups of backups...


It is part of the find command, the -exec statement.

It allows you to interact with the file/directory found by the find command.

find $HOME -path $HOME/$dir_name -prune -o -name "*$file_suffix" -exec cp {} $HOME/$dir_name/ \;

find $HOME means find files/directories in $HOME

To understand -path <some_path>, see `find -path` explained

To understand -prune, see https://stackoverflow.com/questions/1489277/how-to-use-prune-option-of-find-in-sh

-o means OR, so -path <some_path> OR -name *$file_suffix

-exec means execute the command.

cp {} $HOME/$dir_name/ copy any files matching to $HOME/$dir_name/

\; means terminate the -exec command

Tags:

Find