Answer the question
In order to leave comments, you need to log in
BASH: how to delete files whose names contain spaces?
There is a directory /mnt/hdd-backup/APPOLO/Backup Job APPOLO containing files with names like
Backup Job APPOLO2018-08-22T125035.vbk
find "$path" -type f -mmin +9 | xargs rm
rm: cannot remove '/mnt/hdd-backup/APPOLO/Backup': No such file or directory
rm: cannot remove 'Job': No such file or directory
rm: cannot remove 'APPOLO/Backup': No such file or directory
rm: cannot remove 'Job': No such file or directory
rm: cannot remove 'APPOLO.vbm': No such file or directory
oldfiles=$(find "$path" -type f -mmin +9)
for oldfile in $oldfiles
do
rm $oldfile 2>> $flog
done
rm: cannot remove '/mnt/hdd-backup/APPOLO/Backup': No such file or directory
rm: cannot remove 'Job': No such file or directory
rm: cannot remove 'APPOLO/Backup': No such file or directory
rm: cannot remove 'Job': No such file or directory
rm: cannot remove 'APPOLO.vbm': No such file or directory
Answer the question
In order to leave comments, you need to log in
1. change the delimiter for xargs and find to the null character, both of these commands know about it:
2. Delete the files by the find command itself:
3. Save the list of files to a variable and read line by line:
A=$(find "$path" -type f -mmin +9)
while read; do rm "$REPLY";done<<<"$A"
I execute:
find "$path" -type f -mmin +9 | xargs rm
...
Yup. xargs can't handle spaces with paths.
Idiom for handling filenames with spaces/whatever:find ... -print0 | xargs -0 ...
-- i.e. use the null character as a field separator when output from find and when input to xargs.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question