Score:0

How to delete matched filenames from a list

in flag

I want to scan for filenames in a source directory and all its sub-directoris then print the output to a file like:

ls -hR >> del.txt

(I, however, don't know how to not list folders)

I don't need to save the file's list to a file but I think that will make things safe and clear.

Then, I want a "command" to search a certain destination directory and its sub-directories and delete all the matching files in del.txt excluding folders.

The files are very different in names and extensions,so using regex won't solve the problem since I want to delete only these files. Also,the directory where I should delete files form has other required files, that I want to keep, with the same extensions as files from the source directory.

hr flag
It would likely be simpler to use the `find` command to search for and delete the files directly
lee genny avatar
in flag
@steeldriver i don't know that's 21000+ files,do you mean using regex cause there is no certain pattern for them?
hr flag
OK so please [edit] your question with some context describing what files you are trying to delete. Otherwise there's a chance that this becomes an [XY problem](https://meta.stackexchange.com/a/66378)
lee genny avatar
in flag
ok i will try to be more clear.
Raffa avatar
jp flag
1) Does `find . -type f -printf "%f\n" > del.txt` Give you the output you want? … 2) Do you need to save the list to a file first? Or is it okay to both search for filenames in the source directory and just pass them to another command to be searched for and deleted from the target directory on-the-fly?
mpboden avatar
do flag
As @steeldriver suggests, I think using the `find` command is best to find only files without including directories. You can create a script that compares new directory against the old and delete on a match.
Raffa avatar
jp flag
3) Do you need the subdirectories under the target directory searched for matching filenames and delete them as well or just the parent directory's immediate contents?
lee genny avatar
in flag
@Raffa 3) yes,i want the sub-directories to be searched as well.and yes that output is what i desire.please go on,you are right on the money. and no i don't need to save them to a file but i think that will make things safe and clear.
Score:0
jp flag

You can build the list of filenames from the source directory and save them to the del.txt file as null delimited entries like so:

find . -type f -printf "%f\0" > del.txt

Then dry-run test replacing /full/path/to/destination/ with the actual full path to the destination directory like so:

cat del.txt | xargs -0 -I {} find /full/path/to/destination/ -type f -name "{}"

If you are happy with the output, then delete the files with:

cat del.txt | xargs -0 -I {} find /full/path/to/destination/ -type f -name "{}" -delete

Alternatively, you can put the two command in a pipeline to build the list of file names, search for them and print a dry-run on-the-fly like so:

find . -type f -printf "%f\0" | xargs -0 -I {} find /full/path/to/destination/ -type f -name "{}"

And delete like so:

find . -type f -printf "%f\0" | xargs -0 -I {} find /full/path/to/destination/ -type f -name "{}" -delete

Notice: A null delimited list of filenames might not be easy to read by humans but is a much safer way for filenames that have a new line in them for example ... You can build a human friendly list for review purposes(i.e. for you to read and keep ... Don't use this with the above commands), like so:

find . -type f -printf "%f\n" > my_list.txt
Raffa avatar
jp flag
And why might be that **-1** down-vote ... if you would enlighten me ... Please :-)?
lee genny avatar
in flag
xd def not me,why the hell i would do that to someone give a ... to my problem? but that's so internet attitude you will be thrown as an outcast and you will never know why.i will try your solution when i switch to Ubuntu,cause using git-bash right now,and i don't know how to deal with the "full path" in win 10
lee genny avatar
in flag
cat del.txt | xargs -0 -I {} find /f/Fallout New Vegas/Fallout New Vegas/Data/ -type f -name "{}" xargs: argument line too long am i missing something?
Raffa avatar
jp flag
@leegenny Your destination directory path has spaces ... You need to quote it `"..."` e.g. `... find "/f/Fallout New Vegas/Fallout New Vegas/Data/" ...`
lee genny avatar
in flag
quote and double quote both doesn't work,i think i have to switch to ubuntu
Raffa avatar
jp flag
@leegenny You didn't use `-printf "%f\n"` did you? ... That's why you see `xargs: argument line too long` ... Use `find . -type f -printf "%f\0" > del.txt` for the command to work ... The other one as I said in the answer is "for review purposes" i.e for you to read and keep **not** to be used with the above commands.
lee genny avatar
in flag
xd thnx senior Raffa you made me look stupid,it's not a string issue,but the other method worked like a charm.maybe you should reverse the solutions,remove it,or debug it and i will check it if you want. any way i will mark this as a solution and case closed. thanks for time Raffa,that was fun.
Raffa avatar
jp flag
@leegenny Files created/edited or even sometimes opened on Windows will most likely not work in Linux due to the windows-style line endings `CRLF` vs. Linux `LF` ... That could be a reason also ... The two methods are identical except the second method doesn't use a file ... I am glad I helped ... You are welcome :-)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.