Home » Questions » Computers [ Ask a new question ]

How do I remove files downloaded to a directory that are now mixed in with other files without deleting the other in Linux

How do I remove files downloaded to a directory that are now mixed in with other files without deleting the other in Linux

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

Asked by: Guest | Views: 370
Total answers/comments: 2
bert [Entry]

"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 ~/Downloads/, which contains the files file 4 and file 5. You actually wanted to download a set of files (file 1, file 2, and file 3) to a subdirectory ~/Downloads/subdir/. You accidentally downloaded those files to ~/Downloads/, then you downloaded them correctly to ~/Downloads/subdir/ as well. So, your current situation looks like this:

$ tree ~/Downloads/
.
|-- file 1
|-- file 2
|-- file 3
|-- file 4
|-- file 5
`-- subdir
|-- file 1
|-- file 2
`-- file 3

To delete the right files, try this:

$ cd ~/Downloads
$ { pushd subdir; find -mindepth 1 -maxdepth 1 -print0; popd; } |\
xargs --null -I'{}' trash '""{}""

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 undo Linux's rm? for more info on the trash command, which I highly recommend.) If you'd rather live dangerously, replace trash with rm. If you want to live even less dangerously, replace trash with echo trash. That will simply print the commands that will delete your files.

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 subdir. pushd and popd are just like cd, only with a back button. (Look up stacks if you wonder where ""push"" and ""pop"" come from.) The options to find tell it to only list things in subdir and not, for example, subdir/another subdir/third subdir. They also tell it not to list ., the name for the current directory itself. You don't want to remove that. Lastly, the -print0 option tells find to print the files it finds, but separate them with null characters instead of whitespace. This is how we deal with spaces.

Likewise, in the third line, the --null option to xargs tells it to read the filenames from find with null characters separating them. The rest of the third line constructs the command that will delete your files.

The lesson in general is that you need to process files that might have spaces, you generally need to use a command that involves find -print0 | xargs --null."
Nkmhqp [Entry]

lipitor 20mg pills <a href="https://lipiws.top/">atorvastatin 80mg brand</a> buy lipitor sale