0

I have a large filelist of 7000+ files to delete from a Unix directory. I could have accomplished this using a while loop

while read file
do
rm $file
done < filelist

or just

cat filelist | xargs rm 

The challenge comes from deleting those files which have "Windows-like back slash \, colon : and spaces " in their filenames.

Eg.
C:\Sxxx Accr Vac 1111.txt
N:\Lxxx\Dxxx_FOLDER\Mxxx\Mxxx_DOWNLOAD_TEXTFILE_PATH\000000_Mxxx_Bxxx-Pxxx-H.txt
N:\Lxxx\Dxxx_FOLDER\Mxxx\Mxxx_DOWNLOAD_TEXTFILE_PATH\130607_Mxxx_Bxxx-Cxxx-L.txt
N:\Lxxx\Dxxx_FOLDER\Mxxx\Mxxx_DOWNLOAD_TEXTFILE_PATH\140103_Mxxx_Xxxx-Pxx-H.txt

To delete these, I had to enclose the entire filename in double quotes. I wanted to preview the rm commands for each file before actual deletion:

while read -r file
do
echo rm \"$file\"
done < filelist

The problem is that filenames with "\0000" in their strings

eg. N:\Lxxx\Dxxx_FOLDER\Mxxx\Mxxx_DOWNLOAD_TEXTFILE_PATH\000000_Mxxx_Bxxx-Pxxx-H.txt

always ended up with the "\0000" mysteriously truncated from the output.

Eg. For the above file, the rm commands shows up as:
rm "N:\Lxxx\Dxxx_FOLDER\Mxxx\Mxxx_DOWNLOAD_TEXTFILE_PATH00_Mxxx_Bxxx-Pxxx-H.txt"

None of the other files had this problem after passing through the loop.

Any clues?

jww
  • 97,681
  • 90
  • 411
  • 885
H S Mak
  • 11
  • 4
  • could you do it in 2 steps? First you get all the inode numbers of the files and then you delete the files using `find . -inum $INUM -exec rm -i {} \;` – Allan May 31 '18 at 08:03
  • I am only allowed to delete the files specified in the list. Any way to get the find command to refer to the filelist? – H S Mak May 31 '18 at 09:49
  • Perhaps the "\0000" being mysteriously truncated in the output has something to do with the echo command changing it? – pcjr May 31 '18 at 15:20
  • You mention removing files from a Unix directory but your pathnames are Windows pathnames. Are you running the command on Windows or on Unix? – pcjr May 31 '18 at 15:23
  • Those filenames with "Windows paths" are the actual names as seen from the Unix server. They were created by the user's application.. – H S Mak Jun 01 '18 at 00:26
  • you are also right in that the echo command is removing the "\0000" but why does it do that? – H S Mak Jun 01 '18 at 00:28
  • [Allowing punctuation characters in directory and file names in bash](https://stackoverflow.com/q/17143729/608639), [for name in `ls` and filenames with spaces](https://stackoverflow.com/q/8645546/608639), [for loop through files with spaces and some special characters](https://stackoverflow.com/q/33172934/608639), [How do I enter a file or directory name containing spaces or special characters in the terminal?](https://askubuntu.com/q/984801), etc. – jww Jun 06 '18 at 06:04

0 Answers0