I'm a Linux user. I accidentally downloaded a bunch of files into the wrong directory. They're now mixed in with my other files. I then created a new subdirectory and downloaded all the files again, this time into the subdirectory. What is the best way to remove all of the files I accidentally downloaded to the directory without accidentally deleting any of my existing files. I'm new to Linux and need some help. I suppose it could be down by date, or it could be done by saying for each file in the subdirectory, delete a file in the directory with the same name. Thanks in advance! -- Larry
feedback
|
migrated from stackoverflow.com Sep 8 '09 at 20:01
This question came from our site for professional and enthusiast programmers.
|
If the file modification times are consistent, then you can use a variant on
This will list the files. When you're sure you've got the right files, you can revise the
Using the ' If the downloads preserved the modification times of the files on the remote system, you are probably hosed. However, that is rather unlikely to be a problem. | |||||||
feedback
|
|
Actually this is very easy to do.
What this does is list all of the contents in | |||||||||||||
feedback
|
|
To expand on Pynt's conceptually correct solution, you can handle files with spaces in their names by using the null-separator options of find and xargs: Suppose your downloads folder is
To delete the right files, try this:
That code will not delete the files. It will send them to the trash instead, as long as you have installed the trash-cli package. (See http://superuser.com/questions/32355/undo-linuxs-rm/32480#32480 for more info on the trash command, which I highly recommend.) If you'd rather live dangerously, replace Anyway, here's the explanation, since you should never trust code you don't understand. The first line simply moves into the Downloads directory. The second line finds and prints the names of all the files in Likewise, in the third line, the The lesson in general is that you need to process files that might have spaces, you generally need to use a command that involves | ||||
|
feedback
|
|
Simple bash solution
I haven't tested it, so you'd better test it before you run it for real :) | |||
|
feedback
|
|
Pynt and Peltier have this basic idea, but only Peltier's will handle files with whitespaces. This should work:
I show using "ls" to confirm the file list before removal, for sanity. But you could also move them to a temporary place (including a ZIP archive) if you're concerned about deleting the wrong stuff. Just replace the "rm" above with either of the following, then if you trust what happened, remove the ZIP or temporary directory:
| ||||
|
feedback
|
