How to create symbolic link to a directory excluding certain files?

8,328

Solution 1

It would be better if you create symlinks for each file/directory instead.

mkdir /home/pt/Dropbox/pt
for d in /home/pt/*; do ln -s "$d" '/home/pt/Dropbox/pt/'; done

Then delete the link Videos and Dropbox

rm /home/pt/Dropbox/pt/Videos
rm /home/pt/Dropbox/pt/Dropbox

To delete every link, use

rm /home/pt/Dropbox/pt/*
rmdir /home/pt/Dropbox/pt

Solution 2

Your question seems to be the XY problem. Furthermore, the question sound like:

  • How to walk from New York to Washington without passing through London.

This because you probably have misunderstood the meaning of a symbolic link:

A symbolic link (also symlink or soft link) is a special type of file that contains a reference to another file or directory in the form of an absolute or relative path and that affects pathname resolution. [...]

A symbolic link contains a text string that is automatically interpreted and followed by the operating system as a path to another file or directory. This other file or directory is called the "target". The symbolic link is a second file that exists independently of its target. If a symbolic link is deleted, its target remains unaffected. If a symbolic link points to a target, and sometime later that target is moved, renamed or deleted, the symbolic link is not automatically updated or deleted, but continues to exist and still points to the old target, now a non-existing location or file. Symbolic links pointing to moved or non-existing targets are sometimes called broken, orphaned, dead, or dangling.

So, if you create /home/pt/Dropbox as a symlink for /home/pt/, this (/home/pt/Dropbox) will be interpreted and followed by the operating system as a path for /home/pt/ directory. Nothing more; it does not matter what files or directories /home/pt/ contain. When you enter in /home/pt/Dropbox actually you enter in /home/pt/.

Now, I assume that in fact you want to create symlinks in /home/pt/Dropbox directory for each files and folders from /home/pt/ excluding /home/pt/Videos. If so, make sure first that /home/pt/Dropbox directory exists:

mkdir -p ~/Dropbox

Then create the symlinks (excluding Videos an Dropbox) using the following commands:

cd ~
for file in *; do if [[ ! "$file" =~ Video|Dropbox ]]; then ln -s "$HOME/$file" "Dropbox/$file"; fi; done
Share:
8,328

Related videos on Youtube

Jill Clover
Author by

Jill Clover

Updated on September 18, 2022

Comments

  • Jill Clover
    Jill Clover over 1 year

    I want to create a symbolic link from my home directory to Dropbox, excluding Videos.

    This is what I know:

    ln -s /home/pt/ '/home/pt/Dropbox'
    

    What should I do? What will happen if I'd like to delete the symbolic link?

  • Radu Rădeanu
    Radu Rădeanu about 10 years
    For files containing spaces it will be created broken links! For example for ~/Ubuntu One directory it will be created two broken links: ~/Dropbox/pt/One and ~/Dropbox/pt/Ubuntu.
  • Karim Samir
    Karim Samir over 5 years
    it doesn't make a symbolic link for files that starts with a . you should do it twice , one with * and one with .