Why ln -sf does not overwrite existing link to directory [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
According to documentation, command ln -f removes existing destination file. Does this mean that if I create a symlink, -f should remove of overwrite any existing symlink at destination?
I have a symlink, say, L, pointing to DIR1 and type ln -sf DIR2 L. But L still points to DIR1. Only after rm L this command creates a link pointing to DIR2.
With symlinks to files it behaves as expected.
What's wrong with links to directories?
(bash 4.3.48 on Ubuntu 16.04.2 LTS and Windows WSL)

When you run:
ln -sf DIR2 L
This is creating a symlink inside DIR1 cause L points to DIR1 and ln dereferences it, creating L/DIR2 -> DIR1.
The following:
rm -fr DIR1 DIR2 L
mkdir DIR1 DIR2
ln -v -s DIR1 L
ls -la L
ln -v -f -s DIR2 L
ls -la L
will output:
'L' -> 'DIR1'
lrwxrwxrwx 1 runner runner 4 Oct 21 18:13 L -> DIR1
'L/DIR2' -> 'DIR2'
lrwxrwxrwx 1 runner runner 4 Oct 21 18:13 L -> DIR1
To handle that, use the --no-dereference option as indicated in answer in this thread on superuser.com.

Related

what's the difference between rm -rf and rm -r? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
In linux, what is the difference between 'rm -rf' and 'rm -r', both seem to do the same things (delete an entire directory).
Here is a few commands that I ran to test it:
mohammad#mohammad-ThinkPad-E570:~/testerr$ ls
mohammad#mohammad-ThinkPad-E570:~/testerr$ mkdir foo1 foo2
mohammad#mohammad-ThinkPad-E570:~/testerr$ touch foo1/main.java foo2/main.java
mohammad#mohammad-ThinkPad-E570:~/testerr$ tree
.
├── foo1
│   └── main.java
└── foo2
└── main.java
2 directories, 2 files
mohammad#mohammad-ThinkPad-E570:~/testerr$ rm -r foo1
mohammad#mohammad-ThinkPad-E570:~/testerr$ ls
foo2
mohammad#mohammad-ThinkPad-E570:~/testerr$ rm -rf foo2
mohammad#mohammad-ThinkPad-E570:~/testerr$ tree
.
0 directories, 0 files
mohammad#mohammad-ThinkPad-E570:~/testerr$
-f option is there to remove the prompts.
-r option is there to work recursively.
Let's say that we have a folder named stackoverflow with the contents of image.jpg otherimage.jpg mydog.doc
Upon typing rm -r stackoverflow terminal may say: rm: descend into write-protected directory 'stackoverflow'? and if you say y it will ask you for new questions.
rm: remove write-protected regular file stackoverflow/image.jpg'?
rm: remove write-protected regular file stackoverflow/otherimage.jpg'?
rm: remove write-protected regular file stackoverflow/mydog.doc'?
Basically, it will ask every step if you want to do this operation or not.
Now let's try with rm -rf stackoverflow
No questions will be asked this time and, all the content inside the folder is now deleted.
rm -rf ignores non-existent files, and never prompt before removing.
rm -r removes directories and their contents recursively.
https://www.computerhope.com/unix/urm.htm

Move all files whose names contain a capital letter from source directory to target directory? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
How to write this linux command?
Move all files whose names contain a capital letter from source directory to target directory?
If I read your question correctly, use this:
mv src/*[A-Z]* target/
The obvious but wrong solution is
mv src/*[A-Z]* dest
However, the order of letters is locale dependent. [A-Z] therefore can contain lower case letters:
$> touch abc aBc
$> export LC_ALL=C
$> ls *[A-Z]*
abc
$> LC_ALL=en_US
$> ls *[A-Z]*
aBc abc
so make sure to set LC_ALL properly.
export LC_ALL=C
mv src/*[A-Z]* dest
BTW: *[A-Z]* is evaluated by the shell, not mv. Therefore the following does not work:
LC_ALL=C mv rc/*[A-Z]* dest ## does not work
This version ensure that only "Files" in root source folder are moved to target directory:
find /source/*[A-Z]* -maxdepth 1 -type f -exec mv {} /target \;

What does "cd .." and "pwd" really mean when you have softlinks involved? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
See the Unix commands below.
When we are dealing with soft-links, there can be multiple paths to the root from each directory.
So in this case, how is pwd and cd .. calculated? It means that directory paths are no longer stateless, right?
$ cd ~
$ mkdir a b
$ cd a
$ ln -s ~/b b
$ cd b
$ pwd
/home/myuser/a/b
$ cd ..
$ pwd
/home/myuser/a
Look at this example:
[myuser#test ~]$ pwd
/home/myuser
[myuser#test ~]$ mkdir a b
[myuser#test ~]$ cd a
[myuser#test a]$ ln -s ~/b b
[myuser#test a]$ cd b
[myuser#test b]$ pwd
/home/myuser/a/b
[myuser#test b]$ pwd -P
/home/myuser/b
[myuser#test b]$ echo $$
2432
[myuser#test b]$ ls -l /proc/2432/cwd
lrwxrwxrwx 1 myuser myuser 0 Oct 4 04:10 /proc/2432/cwd -> /home/myuser/b
[myuser#test b]$
[myuser#test b]$
[myuser#test b]$ pwd
/home/myuser/a/b
[myuser#test b]$ cd -P ..
[myuser#test ~]$ pwd
/home/myuser
[myuser#test ~]$
[myuser#test ~]$ env | grep "PWD"
PWD=/home/myuser
OLDPWD=/home/myuser/a/b
See option -P to cd from bash manual:
-P If set, the shell does not follow symbolic links when executing commands such as cd that
change the current working directory. It uses the physical directory structure instead.
By default, bash follows the logical chain of directories when performing commands which
change the current directory.
As you can see, the current dir keeps by kernel is your real dir (/proc/2432/cwd -> /home/myuser/b) but bash can do whatever wants follow symbolic links or not,
because cd is bash internal command.
The commands are always computed based on the actual(resolved) directory behind the soft link.
When you do
$ cd b ; you end up in the directory pointed by b
Any command from here is resolved based on this new location

Renaming a '.' file in Ubuntu [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I downloaded a file using rsync and accidentaly provided it the destination as '.' (I thought it is the directory to download into). So it downloaded the multi-gig file but named it '.'.
drwxr-sr-x 2 root apache 4096 May 7 00:42 .
drwxr-sr-x 7 me apache 4096 May 7 00:25 ..
-rw-r--r-- 1 1006 1006 2008805206 Apr 5 04:49 .
-rw------- 1 root apache 1675 May 7 00:25 somefile
-rw------- 1 root apache 392 May 7 00:26 anotherfile.txt
How do I rename the 2GB+ '.' file to something meaningful. Nothing I do seems to work (i've tried mv, rename, etc.) but they all say
Device or resource busy
You can use this mv:
mv ./.[[:blank:]]* myfile
Or else try this find:
find . -type f -maxdepth 1 -name '. *' -exec mv '{}' myfile \;
Yes its a superuser question. But I found a solution elsewhere, so thanks everyone for trying. We could do this using:
find . -type f | (let i=0; while read f; do mv "$f" "$f"-$i ; let i=$i+1; done)
Not the most elegant way and probably very insecure too (as there is no undo).
Type this from the directory where it is located.
mv ./. newfile

how to remove a file named "?" in linux? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 years ago.
Improve this question
I created a file named "?", is anybody know how to delete it?
It seems that ? is a special character in linux, I use Redhat as my OS.
I have already tried
rm ?
rm "?"
rm \?
They all failed and I got the error indicated that the file doesn't exist.
find the inode of the file:
ls -li
then delete the file using inode:
find . -inum <inode-number> -exec rm -i {} \;
BTW, rm ? works for me fine. here is my bash version:
# bash --version
GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)
rm \? and rm "?" are both perfectly good ways to delete a file named ?. If they didn't work, and you still seem to have a file name ?, then it is most likely that the ? being shown is not really a ?, but rather the result of substituting an unprintable character with a ?. To see what the file is really called (with GNU ls) try:
ls --quoting-style=escape
Use this rm command to remove a file named ?:
rm ./\?
OR from another directory:
rm /path/to/\?
You can delete the file by its inode number. see the output bellow:
alplab:~/cad# ls -il
63051 -rw-r--r-- 1 root root 0 Nov 12 11:48 ?
alplab:~/cad# find . -inum 63051 -exec rm -i {} \;
I used the "find" command to delete the file with the inode number 63051 (the inode belonging to my "?" file).

Resources