I have a directory with some symlink file, for example:


And I have another directory with the original file (of the symlink file):


So the problem is to "transform" all the .mkv file from /home/user/symlink/ into the original file BUT with the same file name.

There in the symlink folder, I want to have the all the original files with their name

symlink name $(*symlink_file_name*)

So can I possibly move /home/user/original/$original_file_name1.mkv to /home/user/symlink/$(symlink_file_name1).mkv or somethings like that?


Using GNU readlink to resolve the symlink into the original filename:

First I mimic your directory setup:

$ mkdir original "symlink dir"
$ touch original/file1 original/file2
$ cd "symlink dir"/
$ ln -s ../original/file1 link1
$ ln -s ../original/file2 link2
$ cd ..

Then copy the files:

$ for link in "./symlink dir"/*; do
> orig="$(readlink -f "$link")"
> rm "$link"
> cp -v "$orig" "$link"
> done
‘/home/user/original/file1’ -> ‘./symlink dir/link1’
‘/home/user/original/file2’ -> ‘./symlink dir/link2’
  • I got one last question my original contain space and special character meaning I must put "\ ", how can I make $link valid. Like my symlink directory is: "symlink\ directory/symlink\'s\ link.mkv – Julien Blanchon Aug 22 '18 at 8:01
  • @JulienBlanchon see my edit. If you properly quote your variables and pathnames you do not need to escape the spaces with backslashes. – Kevin Kruse Aug 22 '18 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.