Linux – How to find a specific file and move it to a specific directory

findlinuxmv

How to find a specific file, and move it to the specific directory /var/tmp?

For example I want to find the file 0914_Jul-2014.gz.

Remark, the file 0914_Jul-2014.gz, is under ~300 subdirectories:

      /usr/../../../../../../../../../../../../../../../../../../0914_Jul-2014.gz

An example: when I do a

find /usr -name '0914_Jul-2014.gz' -exec mv {} /var/tmp

The result is

mv: cannot stat: File name too long

error.

Best Answer

You can use find:

find /usr -name '0914_Jul-2014.gz' -exec mv {} /var/tmp \;

Or for extremely nested directory hierarchies

find /usr -name '0914_Jul-2014.gz' -execdir mv {} /var/tmp \;

Although as the documentation states you must ensure that your $PATH environment variable does not reference the current directory (namely .) if you use -execdir

Related Question