3

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

/home/user/symlink/$(*symlink_file_name1*).mkv
/home/user/symlink/$(*symlink_file_name2*).mkv

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

/home/user/original/$(*original_file_name1*).mkv
/home/user/original/$(*original_file_name2*).mkv

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?

slm
  • 363,520
  • 117
  • 767
  • 871

1 Answers1

2

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’
Kevin Kruse
  • 378
  • 2
  • 13
  • 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 08: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