How can I move/copy files from one directory to another, leaving the symlinks in the original directory. I know the ln -screates 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?


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

or, if copying, replace the mv command line with

    cp ./$i $new_directory/  
    rm ./$i   
  • 1
    The cp/rm version - what are the possible benefits of it? Looks like it does the same only much slower :) – Sergey Nov 25 '11 at 7:06
  • @Sergey One could check the exit status of cp and proceed to remove only if it was successful. – lgarzo Nov 25 '11 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 in mv :) – Sergey Nov 26 '11 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. – deox Nov 27 '11 at 23:46
  • 1
    Replace $original_directory and $new_directory with the directories you want to move from/to, or set them like this original_directory=/home/walt/from. For use in scripts, you have to parse the arguments yourself. See the getopts builtin in man bash. – waltinator Nov 28 '11 at 2:13

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.