How can I move/copy files from one directory to another, leaving the symlinks in the original directory. I know the ln -s
creates symlinks in the destination, but I want to leave the symlinks in the source directory and move the actual files to a new one. How can I do this?
-
1The best answer to this question is here: unix.stackexchange.com/a/228166/7000– Sridhar SarnobatDec 30, 2016 at 16:06
Add a comment
|
1 Answer
Process the files individually, e.g.,
cd $original_directory
for i in <files to process> ; do
mv ./$i $new_directory/
ln -s $new_directory/$i ./$i
done
or, if copying, replace the mv
command line with
cp ./$i $new_directory/
rm ./$i
-
1The cp/rm version - what are the possible benefits of it? Looks like it does the same only much slower :)– SergeyNov 25, 2011 at 7:06
-
@Sergey One could check the exit status of
cp
and proceed to remove only if it was successful.– lgarzoNov 25, 2011 at 12:26 -
@lgarzo: That would be valuable if
mv
wasn't checking if copy succeeded before deleting the file - but that would be a serious bug inmv
:)– SergeyNov 26, 2011 at 10:18 -
Are "$new_directory" and "<files to process>" "special variables", where the last argument I give the script will always be considered "$new_directory" and whatever initial arguments are considered "<files to process>"?. Does this apply to any other script I write? Or are you implying that I should fill in the blanks, like using "$1" and "$2"? Sorry for the question but I'm very new to coding in general.– deoxNov 27, 2011 at 23:46
-
1Replace
$original_directory
and$new_directory
with the directories you want to move from/to, or set them like thisoriginal_directory=/home/walt/from
. For use in scripts, you have to parse the arguments yourself. See the getopts builtin inman bash
. Nov 28, 2011 at 2:13