I have a file with name "folder/hello.js" in GIT repository. It has 10 other duplicate files in same GIT repo. Every time I checkout and modify "hello.js" I have to manually change all other 10 duplicate files.
Is there any way to create symlink for "folder/hello.js" file and delete other 10 files.
Symbolic links are just files containing symbolic pointer to other files. Git commits the contents of the symbolic link into its repo and clone and pull will recreate those symbolic links.
Symbolic links could contain absolute or relative paths to a file. A relative path starts at the directory where the link is located. So, imagine this directory/file struct
a -
|- b -
|- hello.js
|- hello.js
you want to replace one of the files with a relative symbolic link to the another one.
Supposedly you decide that your real file is in a/hello.js and your link will be in a/b/hello.js. So, you would need to do 2 things in a linux-type shell:
rm a/b/hello.js # you need to replace it with a symbolic link
ln -s ../hello.js a/b/hello.js
in the dos shell
mklink a\b\hello.js ..\hello.js
Now the file a/b/hello.js will contain text ../hello.js. The file system will use the text to find the real file in the parent directory a/b.
Git will save the link pointer and when you clone the repo, the file system will see the the symbolic link to ..\hello.js
If you decide to use another file as a symbolic link, you can do
ln -s b/hello.js hello.js
This way b/hello.js will be a relative symbolic link and will be substituted in the path instead of a/hello.js
Also, a symbolic link can have a name different from the original file. A symbolic link can point to another symbolic link as well in a chain-like sequence.
Hope it helps you with your problem.
Related
I want to ask when I delete file in folder symlink target, is it possible make the file is still available in original file?
I use ls -n /folder /target but when I delete file in /target , in original folder deleted too.
Remove Symbolic Links with rm
rm symlink_name
Remove Symbolic Links with unlink
unlink symlink_name
Unlink command and rm command without -R option doesn't delete regular directory. The two commands delete symbolic link from directory because it's considered as a file so, when using the rm or unlink command to remove a symbolic link from a directory, make sure you don’t end the target with a / character because that will create an error
Please read more in documentation, man pages etc.
As I understood question - you should remove symlink to directory, but make symlink for each file in directory to target directory. So you will have two normal directories and lot of symlink for files.
Only this way you can delete one particular file in target directory and still have it in original directory
If you want to make it work both ways - to delete file in original directory and still have it in target - make non symlink but hard link with ln for each file.
trying to create a symbolic link to an xml file in a different directory. I am using the command in the current file directory.
ln -s file_name.xml /home/path_to_desired_symbolic_link_location/file_name.xml
The symbolic link is created but it is empty.
It's safe to use the absolute address if you want to link to the file in another directory. Like this:
ln -s /path a/file_name.xml /path b/file_name.xml
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.
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/
I want a create a symbolic link to a folder. The follow command will create a file with the link name but I'm trying to link to the source folder. What am I doing wrong?
ln -s /Users/me/somefolder somefolder
This creates a file "somefolder" in my current directory. How do I create a symbolic link to the folder and it's contents?
Thanks!
You need to use absolute path names to create the links.
For example, I'm now at
$ pwd
/home/alex/my_folder
And I'm creating a symbolic link to the folder "directoryA" in a sub-directory under my pwd (present working directory):
$ ln -s $PWD/directoryA $PWD/temp/link_to_directoryA
In this case variable $PWD holds absolute path to my working directory.
You can surely use your absolute path without any variables like this:
$ ln -s /home/alex/my_folder/directoryA /home/alex/my_folder/temp/link_to_directoryA
You need to be inside the same directory where you create the symbolic link
For instance:
cd /Users/me
ln -s somefolder somefolderNewName
Not creating a directory is an expected behavior.
When you do
ls -ali
It should show something beginning with;
lrwxrwxrwx
In which "l" represents symlink and allows you to traverse using cd.
NOTICE: ln command will not complain when you provide an invalid source path. And this will result with an error message when you try cd in to that.
Late for the party..
This is what worked for me..
if you want to create a symbolic link from sourceFolder to destinationFolder
you should be inside the parent of the destinationFolder "parentOfDestinationFolder" while doing so.
I think you have what you want, you just don't know it. A link has an entry in the directory, just like data files or directories do. You can see this most clearly if you run ls -l in the directory where you're creating the link.
You can use your link as if it were a directory, e.g.:
$ cd somefolder
You might also like to know that if you change directory this way, the parent of somefolder will be the directory that contains the link. If you don't want that, use:
$ cd -P somefolder
I have a script run.sh located somewhere on read only directory /install/app/release_1.0.0/ and a symbolic link to that script in the fully accessed directory /packages/app/. This script operates with files using relative paths. When I'm running this script using symbolic link it's not able to locate files because it's looking in the current directory of symbolic link. How can I force it to look into the current directory of link's target? Changing the script is not prefered.
Don't use a symlink, use a wrapper instead. Remove /packages/app/run.sh and create a new file at that location, with these contents:
#!/bin/sh
cd /install/app/release_1.0.0/
./run.sh
Mark it executable (chmod +x run.sh) and that should do it.