Removing file from folder in linux - linux

Linux os
Hello
i have downloaded the file using wget. the file downloaded with some messy name like
index.html?format=csv&timezone=Asia%2FKolkata&use_labels_for_header=true.
now i am trying to remove this file using rm, but its not getting removed.
Linux os
sudo rm -f Unconfirmed\sudo rm -f Unconfirmed\index.html?format=csv&timezone=Asia%2FKolkata&use_labels_for_header=true
how can i removed it from folder.

It works as below. Make sure you have permission for that file/folder to edit(delete or update). You have used sudo that is correct.
To remove the folder with all its contents(including all interior
folders):
rm -rf /path/to/directory
To remove all the contents of the folder(including all interior folders) but not the folder itself:
rm -rf /path/to/directory/* or
rm -rf /path/to/directory/{*,.*}
if you want to make sure that hidden files/directories are also removed.
To remove all the "files" from inside a folder(not removing interior folders):
rm -f /path/to/directory/{*,.*}
Where:
rm - stands for "remove"
-f - stands for "force" which is helpful when you don't want to be asked/prompted
if you want to remove an archive, for example.
-r - stands for "recursive" which means that you want to go
recursively down every folder and remove everything.

rm -rf "yourfilename" put double quotes over your file name.
Also check if you have permission to delete this file.

make notifies you of the deletion by printing an rm -f command that specifies the file it is deleting.
Eg:
Here MergeSort is the file that I need to remove.

Related

move linux directory unless it already exists

I need a way to move a linux directory and all of it's contents only if it doesn't currently exist in the target location. If it does currently exist (including all sub-folders and files) then the source folder can just can just be removed recursively.
I currently use the following framework but wish to expand it to meet the above criteria.
mv /source/* /target
Thanks
rsync -av --remove-source-files source/ destination/ && rm -rf source/
Replace source/ and destination/ accordingly.
Source
Gnu mv has the -n or --no-clobber option. Unfortunately, it seems to return with an successful exit status even if the mv was a no-op due to the --no-clobber option, however it seems that in your use case, you can simply do the --no-clobber move and then clear the source if the move succeeds regardless of whether or not it did anything.

Unable to delete folder in linux with special character

I have a folder which is:
/var/www/xxx/html/'.
It was created accidentally. I try to delete it but it comes up,
No such files or directory
I have tired
rm -f /var/www/xxx/html/'.
rm -f "/var/www/xxx/html/'."
rm -f /var/www/xxx/html/*'*.
It is because of the '.
I have been looking around for a few hours but no joy.
cd /var/www/xxx/html
rm -rf \'.
worked for me.
Try this :
rmdir /var/www/xxx/html/\'

How to remove a file with special characterictics

Hi I just created a file by mistake, doing a tar actually, anyway the problem I have is that I can't remove that file. It is called --exclude-tag-under=hey.txt
I am trying to use rm -rf command but it doesn't do the trick. this is the output
[root]# rm -rf '--exclude-tag-under\=hey.txt'
rm: unrecognized option '--exclude-tag-under\=hey.txt'
Try 'rm --help' for more information.
the problem here is that the command rm is recognizing the file as a flag and thats a problem, I've tried also
rm -rf *hey.txt
but it doesnt work neither
I've also tried to change the name of the file but its the same problem
Prepend ./ like this: rm ./--exclude-tag-under\=hey.txt
When in doubt, check the man pages.
Running man rm will give you the rm man page, which, on Linux and OpenBSD (the ones I have tested) at least, will have a section saying:
To remove a file whose name starts with a '-', for example '-foo', use
one of these commands:
rm -- -foo
rm ./-foo
Use rm -- --exclude-tag-under=hey.txt
$ ls
--exclude-tag-under=hey.txt
test
$ rm -- --exclude-tag-under=hey.txt
$ ls
test

zsh: how to delete contents in folder without deleting the folder?

I am using zsh and I want to delete contents of a folder without deleting the folder itself. What is the best way to go about this?
rm -r myfolder/* will delete all files in that folder that do not begin with a dot.
Really the simplest solution is rm -rf myfolder && mkdir myfolder.
You can just use rm -r path/to/dir/*.

Unix command deleted every directory even though not specified

I am very new to the unix. I ran the following command.
ls -l | xargs rm -rf bark.*
and above command removed every directory in the folder.
Can any one explained me why ?
The -r argument means "delete recursively" (ie descend into subdirectories). The -f command means "force" (in other words, don't ask for confirmation). -rf means "descend recursively into subdirectories without asking for confirmation"
ls -l lists all files in the directory. xargs takes the input from ls -l and appends it to the command you pass to xargs
The final command that got executed looked like this:
rm -rf bark.* <output of ls -l>
This essentially removed bark.* and all files in the current directory. Moral of the story: be very careful with rm -rf. (You can use rm -ri to ask before deleting files instead)
rm(1) deleted every file and directory in the current working directory because you asked it to.
To see roughly what happened, run this:
cd /etc ; ls -l | xargs echo
Pay careful attention to the output.
I strongly recommend using echo in place of rm -rf when constructing command lines. Only if the output looks fine should you then re-run the command with rm -rf. When in doubt, maybe just use rm -r so that you do not accidentally blow away too much. rm -ir if you are very skeptical of your command line. (I have been using Linux since 1994 and I still use this echo trick when constructing slightly complicated command lines to selectively delete a pile of files.)
Incidentally, I would avoid parsing ls(1) output in any fashion -- filenames can contain any character except ASCII NUL and / chars -- including newlines, tabs, and output that looks like ls -l output. Trying to parse this with tools such as xargs(1) can be dangerous.
Instead, use find(1) for these sorts of things. To delete all files in all directories named bark.*, I'd run a command like this:
find . -type d -name 'bark.*' -print0 | xargs -0 rm -r
Again, I'd use echo in place of rm -r for the first execution -- and if it looked fine, then I'd re-run with rm -r.
The ls -l command gave a list of all the subdirectories in your current present-working-directory (PWD).
The rm command can delete multiple files/directories if you pass them to it as a list.
eg: rm test1.txt test2.txt myApp will delete all three of the files with names:
test1.txt
test2.txt
myApp
Also, the flags for the rm command you used are common in many a folly.
rm -f - Force deletion of files without asking or confirming
rm -r - Recurse into all subdirectories and delete all their contents and subdirectories
So, let's say you are in /home/user, and the directory structure looks like so:
/home/user
|->dir1
|->dir2
`->file1.txt
the ls -l command will provide the list containing "dir1 dir2 file1.txt", and the result of the command ls -l | xargs rm -rf will look like this:
rm -rf dir1 dir2 file1.txt
If we expand your original question with the example above, the final command that gets passed to the system becomes:
rm -rf di1 dir2 file1.txt bark.*
So, everything in the current directory gets wiped out, so the bark.* is redundant (you effectively told the machine to destroy everything in the current directory anyway).
I think what you meant to do was delete all files in the current directory and all subdirectories (recurse) that start with bark. To do that, you just have to do:
find -iname bark.* | xargs rm
The command above means "find all files in this directory and subdirectories, ignoring UPPERCASE/lowercase/mIxEdCaSe, that start with the characters "bark.", and delete them". This could still be a bad command if you have a typo, so to be sure, you should always test before you do a batch-deletion like this.
In the future, first do the following to get a list of all the files you will be deleting first to confirm they are the ones you want deleted.
find -iname bark.* | xargs echo
Then if you are sure, delete them via
find -iname bark.* | xargs rm
Hope this helps.
As a humorous note, one of the most famous instances of "rm -rf" can be found here:
https://github.com/MrMEEE/bumblebee-Old-and-abbandoned/commit/a047be85247755cdbe0acce6f1dafc8beb84f2ac
An automated script runs something like rm -rf /usr/local/........., but due to accidentally inserting a space, the command became rm -rf /usr /local/......, so this effectively means "delete all root folders that start with usr or local", effectively destroying the system of anyone who uses it. I feel bad for that developer.
You can avoid these kinds of bugs by quoting your strings, ie:
rm -rf "/usr/ local/...." would have provided an error message and avoided this bug, because the quotes mean that everything between them is the full path, NOT a list of separate paths/files (ie: you are telling rm that the file/folder has a SPACE character in its name).

Resources