OS X: listing and deleting files with spaces

I would like to do this on OS X:

ls -rt | xargs rm -i 

However, rm choking on the fact that some files have spaces.

I mention OS X because the BSD ls version does not have the -Q flag.

Is there a way to do this without using find -print0 ?

+7
bash macos
Jan 12 '13 at 5:14
source share
5 answers

try it

 while read -u 3 do rm -i "$REPLY" done 3< <(ls -rt) 
0
Jan 12 '13 at 5:22
source share
 [sgeorge@sgeorge-ld staCK]$ touch "file name"{1..5}.txt [sgeorge@sgeorge-ld staCK]$ ls -1 file name1.txt file name2.txt file name3.txt file name4.txt file name5.txt [sgeorge@sgeorge-ld staCK]$ ls -rt | xargs -I{} rm -v {} removed `file name5.txt' removed `file name4.txt' removed `file name3.txt' removed `file name2.txt' removed `file name1.txt' 

OR

 [sgeorge@sgeorge-ld staCK]$ ls -1 file a file b file c file d [sgeorge@sgeorge-ld staCK]$ OLDIFS=$IFS; IFS=$'\n'; for i in `ls -1` ; do rm -i $i ; done ; IFS=$OLDIFS rm: remove regular empty file `file a'? y rm: remove regular empty file `file b'? y rm: remove regular empty file `file c'? y rm: remove regular empty file `file d'? y 
+2
Jan 12
source share

If you want to delete all files, what happened to rm -i * ?

Do not disassemble ls

+1
Jan 12 '13 at 11:28
source share

Just find delete it for you.

 find . -print -depth 1 -exec rm -i {} \; 

It is more flexible, it should be in order with spaces.

0
Jan 12 '13 at 5:22
source share

You have two options. You can either call xargs with the -0 option, which splits the input into arguments using the NUL characters ( \0 ) as delimiters:

 ls -rt | tr '\n' '\0' | xargs -0 rm -i 

or you can use the -I option to split the input only on new lines ( \n ) and call the desired command once for each input line:

 ls -rt | xargs -I_ rm -i _ 

The difference is that the first version only calls rm once, with all arguments presented as one list, and the second calls rm separately for each line in the input.

0
Sep 05 '16 at 17:37
source share



All Articles