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
Related
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.
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 6 years ago.
Improve this question
This is my directory structure.
dirOut
├── dirIn1
│ ├── temp1
│ └── temp2
├── dirIn2
└── dirIn3
├── temp1
├── temp2
├── temp3
└── temp4
dir is directory and temp is file.
I want to find files which contain specific string "Hello".
How do I use command "find" to find.
Use grep, not a find, when you find files base on the content.
grep -lr Hello .
-l : Normally grep print matching lines, with -l option, it just print the matched filenames.
-r : recursively find files under the directory.
find dirOut -type f -exec grep -l Hello {} +
The -l option tells grep to just list the filename if it finds a match, rather than showing all the matching lines.
You could also do it using the -R option to grep to search a directory recursively, rather than using find.
grep -R -l Hello dirOut
find . -type f -name \* -exec grep -l "hello" {} \;
Execute this command while in dirOut.
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 \;
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).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I list all the files in a directory and all its sub-directories?
tree can accomplish this job:
$ tree
.
├── dir
│ └── f1
└── f2
1 directory, 2 files
But I want files to be listed in this format:
dir/f1
f2
Pass options -i and -f to tree:
tree -if
Option -i disables the printing of the indentation lines, option -f prints a path prefix for each file. This will however still list non-leaf directories.
Use the find command.
find . -type f
Apart from above mentioned solution, you can list the file in directory and its subdirectory using "ls" command with "-R" option.
ls -lR
One way to get this listing is using printf and ls -r
printf "%s\n" "$(ls -r)"