Make Diff to Ignore Symbolic Link

make diff to ignore symbolic link

Use find in the following way:

find . ! -type l

This option should skip following symbolic links. Use that command to locate your file before running diff.

Ignore symbolic link in TortoiseHG

If you have untracked content in your working folder, and want to keep them as untracked, there's generally three ways to do that:

  1. Never issue any of the "please figure out what needs to be added and removed for me" commands (hg addremove and hg commit ... --addremove are examples of this)
  2. Specifically list the files that you want to avoid being added, every time you issue such a command
  3. List them in your .hgignore file

If you want to go with option 3, then you should consult the documentation for .hgignore. In general, you can just list the names of the files in that file, it's just a normal text file, but there's a few options regarding syntax for that, so best to just check the documentation.

How to make rm not delete symbolic links?

Joe W's answer is correct in saying rm typically does not delete the targets of symlinks, but to say that it "does not follow symlinks" is not quite accurate, at least on my system (GNU coreutils 8.25). And deleting files is a place where accuracy is pretty important! Let's take a look at how it behaves in a few situations.

If your symlink is to a file, rather than to a directory, there is no plausible way to accidentally delete the target using rm. You would have to do something very explicit like rm "$(readlink file)".

Symlinks to directories, however, get a bit dicey, as you saw when you accidentally deleted one. Here's a test case we can use:

$ mkdir test1
$ touch test1/foo.txt
$ ln -s test1 test2
$ ls -lR
.:
total 4
drwxrwxr-x 2 soren soren 4096 Jun 29 17:02 test1
lrwxrwxrwx 1 soren soren 5 Jun 29 17:02 test2 -> test1

./test1:
total 0
-rw-rw-r-- 1 soren soren 0 Jun 29 17:02 foo.txt

These are all safe:

  • rm test2 (deletes the symlink only)
  • rm -r test2 (deletes the symlink only)
  • rm -rf test2 (deletes the symlink only)
  • rm test2/ (rm: cannot remove 'test2/': Is a directory -- no action taken)
  • rm -rf *2 (or any other glob matching the symlink -- deletes the symlink only)

These are not safe:

  • rm -r test2/ (rm: cannot remove 'test2/': Not a directory -- but deletes the contents of the test1 directory)
  • rm -rf test2/ (deletes the contents of the directory, leaves the symlink, no error)
  • rm -rf test2/* (deletes the contents of the directory, leaves the symlink, no error)

The last unsafe case is probably obvious behavior, at least to someone well-acquainted with the shell, but the two before it are quite a bit more subtle and dangerous, especially since tab-completing the name of test2 will drop the trailing slash in for you!

It's interesting to note that test has similar behavior, considering a symlink to a directory with a trailing slash to be not a symlink but a directory, while a symlink without a trailing slash is both:

$ [ -L "test2" ] && echo "is link"
is link
$ [ -d "test2" ] && echo "is directory"
is directory
$ [ -L "test2/" ] && echo "is link"
$ [ -d "test2/" ] && echo "is directory"
is directory

Here's a previous treatment of "deleting a symlink to a directory without deleting the target," with a less thorough analysis of exactly what works and what doesn't but with a bunch of other useful information.


Unfortunately, I am not aware of any way to alias rm to prevent this mistake. I suppose you could write a function to parse the arguments to rm and warn you if any of them are symlinks that end with a trailing slash, something like this:

function rm {
for i in "$@"; do
if [[ $i =~ /$ ]] && [ -L "${i:0:-1}" ]; then
read -rp "Really delete symlink '$i' with trailing slash (y/n)? " result
[ "$result" != "y" ] && return
fi
done
command rm "$@"
}

Use at your own risk, though! It passes shellcheck and it worked when I tested it, but implementing a wrapper on top of something as fundamental and dangerous as rm gives me the heebie-jeebies.

Two other potentially useful switches you might include in your alias/function would be --one-file-system (at least skips files past a symlink or mount onto a different drive) and, if you don't already use it, -i or -I to prompt when doing something potentially dangerous.

How do I make Git ignore symlink?

Git considers symlinks to be files, not directories. You need to remove the / at the end of each .gitignore line:

NameOfSymlink
AnotherSymlink


Related Topics



Leave a reply



Submit