I ran this command that a user on reddit wrote for me:
dest=final_dir
files=$(find dir1 dir2 dir3 -type f)
for file_path in $files; do
IFS='/' read -r -a tokens <<< "$file_path"
dest_fname="${tokens[1]}.${tokens[0]}"
ln -s "$PWD/$file_path" "$PWD/$dest/$dest_fname"
done
It is supposed to take all files in all subfolders and create a symlink to another directory with the name of the link being filename+filefolder. It does however only give me the error: failed to create symbolic link - no such file or directory here is a screenshot of the console: https://i.sstatic.net/xdL1k.jpg
any ideas what could be the problem?
Edit: Ok, the problem seems to be that it when the path includes a space, it thinks the name stops at this space and thus doesnt find the file. Any idea how to fix this?
Here a description of what the script is supposed to do. my current filesystem looks like this:
dir1: file1, file2
dir2: file1, file2
if i now run the above command i get:
dir3: file1, file2
and the other 2 files are skipped as they have the same name.
also given this scenario:
dir1: file1, file3
dir2: file2, file4
in this case the files would be mixed to:
dir3: file1, file2, file3, file4
what i want though is:
dir3: file1, file3, file2, file4
In my opinion the easiest way to achieve this would be to make the source folder part of the symlinks name, like this:
dir3: dir1-file1, dir1-file3, dir2-file3, dir2-file4
I hope this explains it well enough.
The problem right now seems to be that when file- or foldername contain any special characters like "-" or "_" it stops reading the name at that point.
This results in: dir1-2: file3-4 creating a symlink to dir1.file3 which doesnt exist
ln -s
does not care about whether the source path exists, so the problem must be with the target path. The command creates a symbolic link, so if it is complaining that a file or directory does not exist then the issue must be with the path to the target link, not the link's name.
In your case, the target path is "$PWD/$dest/$dest_fname"
.
From its construction, it does not appear that $dest_fname
can contain /
characters, so it is a simple file name without any path components.
$PWD
represents (the shell's idea of) the present working directory. Unless you are playing games with that variable or the working directory has been deleted out from under you, $PWD
will expand to the name of an existing directory.
That leaves $dest
as the probable locus of the issue. The script does nothing to ensure that the directory it designates exists, so the most plausible explanation is that it does not. Consider adding
mkdir -p "$PWD/$dest"
to your script, immediately prior to the for
loop, if you want it to create the destination directory at need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With