Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux find file and create hardlink

Tags:

linux

bash

I'm trying to find a file, then create a hard link with the same name in a different directory. But this is not working, if I take the -exec and after off, it displays one result. I already have it working with cp, but I've decided to make a hard link instead.

find . -iname "*sample*" -exec link {} ~/{} \;
like image 687
David Custer Avatar asked Dec 31 '25 09:12

David Custer


1 Answers

The problem comes from the fact that {} contains the path of the file found; hence ~/{} is not a path in ~ but in some nonexistent sub-directory.

I didn't find a way to get the {} basename using find neither xargs.

Here is one solution, which works but is unsafe:

find . -iname "*sample*" | while read f ; do link "$f" "$HOME/${f##*/}" ; done

You may add a filter between find and read to get rid of "dangerous" file names.

like image 131
Edouard Thiel Avatar answered Jan 02 '26 22:01

Edouard Thiel