Remove symlink on Msys - symlink

I used msys to create a symlink. Now I wish to remove it, but here's what I get:
$ ln -s /mypath mylink
$ rm mylink
rm: cannot remove directory `mylink': Is a directory
What's the proper way to remove a symlink on msys?

Symlinks on MSys2 aren't real symlinks but copies. So you can actually use rm:
rm
-r, -R, --recursive remove directories and their contents recursively
Example:
rm -r mylink

Related

Cannot remove .git/: Directory not empty

I have a .git/ folder that I'd like to delete. However, it isn't possible because there is a puzzling file into it which is not reachable.
Below is my try:
$ rm -rf .git/
rm: cannot remove '.git/': Directory not empty
$ rm -r .git/
rm: descend into directory '.git/'? y
rm: cannot remove '.git/t8QVta1': No such file or directory
rm: remove directory '.git/'? y
rm: cannot remove '.git/': Directory not empty
$ ls -l .git/
ls: cannot access '.git/t8QVta1': No such file or directory
total 0
?????????? ? ? ? ? ? t8QVta1
$
I have no idea what is the 't8QVta1' file and all of the question marks.
Many thanks for your help.
The ?????????? ? ? ? ?... output of ls may indicate that you are missing the correct permissions to access this file. If this is the problem, you could try giving yourself permissions over the parent directory and its children with the following command:
sudo chmod -R g+x .git/
To remove a non-empty folder use -rf flags
$ rm -rf .git

Cannot remove symbolic link to directory

I have a directory reference in my Downloads directory that contains a symbolic link (created with ln -s) to another directory. I get conflicting error message when trying to remove the symlink:
rm returns "Is a directory"
rmdir returns "Not a directory"
This only occurs with cellranger/ (followed by a forward slash) and not with cellranger.
[tom#rlgsw68 cellranger]$ pwd
/home/tom/Downloads/reference
[tom#rlgsw68 cellranger]$ ls -lth
lrwxrwxrwx 1 tom genome 33 Apr 4 14:52 cellranger -> /analysisdata/genomes/cellranger/
[tom#rlgsw68 cellranger]$ rm cellranger/
rm: cannot remove directory `cellranger/': Is a directory
[tom#rlgsw68 cellranger]$ rmdir cellranger/
rmdir: cellranger/: Not a directory
[tom#rlgsw68 cellranger]$ rm cellranger
Why does neither of these commands to remove the symlink work and why do these conflicting errors occur? What is the recommended way to remove symbolic links without removing the content in the source directory. rm -rf cellranger/ also does not remove the symlink (but does not return an error).
Information: I'm running a linux server (Debian 9.0). These errors occur with both bash and zsh. Ambiguous names have been removed from the example. I encountered this when a directory included a link to the parent directory in addition to the contents:
/home/tom/Downloads/reference/cellranger/cellranger/ -> /analysisdata/genomes/cellranger/
By putting a trailing slash you are referring to the directory the symlink points to, no longer the symlink itself. Printing the inode number (the number that a path refers to in the file system) shows the difference between dereferencing the symlink and the directory:
$ cd "$(mktemp --directory)"
$ mkdir a
$ stat --format %i a/
9
$ ln --symbolic a b
$ stat --format %i b
10
$ stat --format %i b/
9
This may be related to the fact that a symlink is never a directory, it is always just a file containing a path.

Using rm -rf with a directory

I tried to search on SO, but not able to find the difference between the following commands. if I have a directory named dir, how the below commands differ?
rm -rf dir/*
rm -rf dir/
rm -rf dir
Also how do the user permissions on the directory affect the outcome, if the id running the command is not the owner or not even in the group of the owner?
I am adding the command to do rm -rf in a shell script I am working on and need help in understanding the difference between the above commands.
rm -rf dir/*
Removes files within the directory (without removing the directory itself).
Note, hidden files won't be removed.
rm -rf dir/
Trailing slash indicates that dir is a directory. If it was a file, it wouldn't get removed. In your case this is identical to rm -rf dir, but in general it differs (see below)
rm -rf dir
In your case, identical to the one above.
In general, tools such as rm usually follow IEEE/OpenGroup standards when it comes to pathname resolution, which means that dir/ is equivalent to dir/.. One implication of that is that if diris a symlink to a directory rm -rf dir/ will remove the content of the directory (including the hidden files) but not the link or the directory itself, whereas rm -rf dir will only remove the symlink.
You need to have write permissions on a file or directory that you are removing, plus exec permissions on a directory that rm needs to traverse to remove files. You can read more about Unix filesystem permissions here.

What's the DRYest way to replace a file with a symlink in bash

I have an existing file which I'm replacing with a symlink to another file. So I basically need to do this:
rm orig
ln -s /var/better orig
I DRYed the above to this:
{rm,ln\ -s\ /var/better}\ orig\;
But it no longer works. The shell now complains:
-bash: rm orig;: command not found
Is there a way to make the DRY form work?
You can just use "-f".
ln -sf /var/better orig
From man ln
-f, --force
remove existing destination files
All you need is cp:
cp -sf /var/better orig

Prevent parent directories from being tarred

Basically I just want to tar all the files in a directory, but not get all the parent directories in the archive.
I've tried -C, but I guess I'm not using it right.
tar -cjf archive.tar.bz2 -C /var/some/log/path ./*
This results in tar trying to add all the files in the CWD. Using the full path as last argument doesn't prevent the dirs from being added.
Seems simple enough, but can't figure it out. Somehow tar does not tar ./* as being relative to -C, although it should change to that dir.
Help appreciated.
The parent directory (/var/some/log) is included, since /var/some/log/path/.. is included when you do ./*. Try just doing
tar -cjf archive.tar.bz2 -C /var/some/log/path .
Test run:
$ find tmp/some_files
tmp/some_files
tmp/some_files/dir1
tmp/some_files/dir1/dir1file
tmp/some_files/hello
tmp/some_files/world
tmp/some_files/dir2
tmp/some_files/dir2/dir2file
$ tar -cvjf archive.tar.bz2 -C tmp/some_files/ .
./
./dir1/
./dir1/dir1file
./hello
./world
./dir2/
./dir2/dir2file
$ cd tmp/unpacked
/tmp/unpacked$ mv /home/aioobe/archive.tar.bz2 .
/tmp/unpacked$ tar -xvjf archive.tar.bz2
./
./dir1/
./dir1/dir1file
./hello
./world
./dir2/
./dir2/dir2file
/tmp/unpacked$ ls
archive.tar.bz2 dir1 dir2 hello world
/tmp/unpacked$
There's a much easier way to do this:
cd down to the directory you wish to be top level, i.e...
cd /var/lib/mysql
Remove parent directories from your tar command
/var/lib/mysql/DATABASE_NAME becomes just DATABASE_NAME
More details can be found in this blog writeup.

Resources