Search code examples
linuxubuntumovesymlink

Move files from one location to another in Linux using Symbolic link


I would like to know is it possible through symbolic (soft) link to move files from one location (A) to another (B) when files are getting created in the A location.


Solution

  • So you have some program creating files in some directory (e.g. in /fixed/location/), and you want the data to be elsewhere (e.g /data/dir/somefile.txt...)

    If you know in advance the name of the created files, you could make them symbolic links before starting the program:

     ln -s /data/dir/somefile.txt /fixed/location/file.txt
    

    and if you create that symbolic link before running the program, it will write the data into /data/dir/somefile.txt even if that file does not exist (but the directory /data/dir should exist when you type that ln -s)

    Another (Linux specific) possibility is to make a bind mount. If e.g. you want the data inside /usr/src/ to reside in /home/Src/ you could first mkdir /home/Src then e.g. add the following line in your /etc/fstab file:

     /usr/src              /home/Src                 none    bind    0 0
    

    I'm actually doing that (on /usr/src and /usr/local) for every Linux system where /home/ is a different filesystem, because I want them to be in the same backed up file system as /home/