0

I have text files listing a lot of files that are on my linux system that have been identified as needing to be deleted. Some of these files were created on Windows and contain '1. ' or '2. ' (i.e. with literal space after the period) etc. etc.

The rm command deletes them if I enter rm -f command with the file name in quotes. But entering this command 700,000 times in not going to happen. I tried inputting the "list" files into the command but it skips the ones with the irregular name.

Is there anyway to put the delete commands into a script to perform it in an automatized way?

5
  • 2
    Welcome to the site. It would appear from your code that it is not actually the . (as implied by your question title) but rather the embedded whitespace in the filenames that poses the problem. If so, please edit the question title to make this more prominent. Also, if you can give a few examples of what your list file looks like, it would make it easier for contributors to help you with your problem.
    – AdminBee
    Jan 15, 2020 at 9:51
  • Dot is not special. Unix does not have file extensions, they are just files that have a dot in them. (Microsoft's windows does not have them anymore ether. But file-explorer still passes the end of the file name, to decide what to do. ) Jan 15, 2020 at 10:03
  • Please show what you have tried. What works, what does not. There seems to be two problems in the question: Why you need to use -f, and automation. Therefore it should be two questions (one question per question policy) Jan 15, 2020 at 10:04
  • @Archemar are you sure the formatting edit was correct? I had the impression there was a whitespace behind the dot, i.e. 1. and 2. . @ctrl-alt-delor That might change the meaning of the question significantly.
    – AdminBee
    Jan 15, 2020 at 10:11
  • @AdminBee you are right. I include a space after dot (e.g. one dot space), unfortunatly makedown rendering seems to remove it. I rollback my edit.
    – Archemar
    Jan 15, 2020 at 10:15

2 Answers 2

1

I think you have just encountered one of the reasons why usually you will be told not to parse the output of ls.

That said, if your list.txt contains the filenames to be deleted, one per line, and with linux-style end-of-line, the following approach should also work:

while IFS= read filename
do
  rm "$filename"
done < list.txt

although you would be well-advised to test this first (e.g. by creating dummy files using touch "1. this is a first test.txt", touch "2. my second_file.bla" and then creating a dummy list.txt that includes these filenames).

You can either include the above code in a bash script or type it directly on the command-line, which would have to look line

user@host$ while IFS= read filename; do rm "$filename"; done < list.txt
0

interactive removal can be done using ? (question mark) as a wildcard. e.g.

rm 1.?

will delete 1.

the downside is it will also delete any 1.a file.

if you list of file is list.txt with one file per line, you can use

awk '{printf "rm \"%s\"\n",$0}' file.txt 

to generate command

where

  • $0 is the whole line (including any trailling or leading space)

if it's fine, then use

awk '{printf "rm \"%s\"\n",$0}' file.txt |bash

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .