How to Turn a Regular File into a Symlink on Linux

How to convert symlink to regular file?

There is no single command to convert a symlink to a regular file. The most direct way is to use readlink to find the file a symlink points to, and then copy that file over the symlink:

cp --remove-destination `readlink bar.pdf` bar.pdf

Of course, if bar.pdf is, in fact, a regular file to begin with, then this will clobber the file. Some sanity checking would therefore be advisable.

How to turn a regular file into a symlink on Linux

I don't believe there is a way in Linux to do this as you describe. IIRC, the filesystem stores symlink information in the inode table and not in a regular file so there's no direct way of turning a file into a link.

If the symlink's path is stored inside the file, why not read out the path, delete the file, and create a symlink in its place?

How can I symlink a file in Linux?

To create a new symlink (will fail if symlink exists already):

ln -s /path/to/file /path/to/symlink

To create or update a symlink:

ln -sf /path/to/file /path/to/symlink

Convert file or directory to symbolic link and preserve permissions

Possibly by doing the following:

  1. Copy everything from ~/git/project/src into ~/public_html
  2. mv ~/git/project/src ~/git/project/src2 to get it out of the way
  3. mv ~/public_html ~/git/project/src
  4. finally link it back: ln -s ~/git/project/src ~/public_html

The idea is to keep the original public_html directory because it has the correct owner/permissions, but reuse it as the link target.

How to get the Linux file type (regular, durector, symlink, char device, etc) in .NET?

It's interesting that even a Linux-friendly language like Java doesn't offer a comprehensive solution to do so.

Fortunately, File.GetAttributes(string) method provides helpful (but still not as complete as question looks for) information.

var path = "/path/to/file";
var attributes = File.GetAttributes(path);

if (attributes.HasFlag(FileAttributes.Directory))
Console.WriteLine("directory");
else if (attributes.HasFlag(FileAttributes.Normal))
Console.WriteLine("file");
else if (attributes.HasFlag(FileAttributes.ReparsePoint))
Console.WriteLine("link");
else if (attributes.HasFlag(FileAttributes.System))
Console.WriteLine("system");

The code above is tested on multiple sample files over WSL2 and works fine. However, I didn't managed to test all sort of files but it seems some attributes like Device or System represents more than one type out of seven Linux file types.

How to differentiate between a regular file and a symbolic link?

You are probably using stat() to get the file modes. stat() actually returns the info about the target, not the link itself.
So, in order to get the information about the link itself you need to use lstat().

How to copy a folder containing absolute symlinks and preserve their relative targets

Use the symlinks program to convert your absolute symlinks into relative ones:

symlinks -c  wxcrafter

then copy the entire directory:

cp -a wxcrafter codeblocks

If you must use absolute symlinks (but why would you?), make a copy of the original directory before running symlinks and restore it after you're done.

The copy (codeblocks) will have relative links, if you need them absolute you'll have to write a little script (using ln -fs $(pwd)/$(readlink $l) $l to convert any symlink $l from relative to absolute), as symlinks only converts in one direction. But, again, why bother?

git commit symlink as a regular file

Nope, Git knows it's a symlink. It'd be kind of dangerous for Git to pretend otherwise, since it would then end up writing to files outside the repo. Tracking it as a symlink is exactly the intended behavior.



Related Topics



Leave a reply



Submit