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:
- Copy everything from
~/git/project/src
into~/public_html
mv ~/git/project/src ~/git/project/src2
to get it out of the waymv ~/public_html ~/git/project/src
- 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
Difference Between $() and () in Bash
How to Use Vi to Edit a Command in Terminal on Linux
How to Select a Static Port Number for a Custom App
Capture Output of a Bash Command, Parse It and Store into Different Bash Variables
How to Change Core Pattern Only for a Particular Application
Can "Text File Busy" Happen When Two Processes Trying to Execute a Perl File in The Same Time
Dlopen with Two Shared Libraries, Exporting Symbols
Bash "&" Without Printing "[1]+ Done "
Authenticating Gtk App to Run with Root Permissions
Sed Command Works on Linux, But Not on Os X
Git Clone Gnutls Recv Error (-9): a Tls Packet with Unexpected Length Was Received
Building Gcc with Glibc in a Non-Standard Location Without Root
What Is a Shell Command to Find The Longest Common Substring of Two Strings in Unix
Gnome Shell Extension Key Binding
How to Compile Redis So That I Can Upload and Run It on Shared Hosting
Bypassing 4Kb Block Size Limitation on Block Layer/Device