Deletion of Linux Files with Spaces of is not a directory

  • 2021-07-18 09:41:01
  • OfStack

Everyone usually comes into contact with documents without spaces in their work. In this way, it is relatively simple to delete the operation. But sometimes we come into contact with files with spaces. How should we delete such files?

First of all, we demonstrate that find command combined with xargs command to delete files without spaces under 1


[root@ELK-chaofeng test]# touch 1.txt 2.txt
[root@ELK-chaofeng test]# ls
1.txt 2.txt
[root@ELK-chaofeng test]# find . -type f | xargs
./1.txt ./2.txt
[root@ELK-chaofeng test]# find . -type f | xargs rm -rf
[root@ELK-chaofeng test]# ls
[root@ELK-chaofeng test]#

Next, we demonstrate deleting files with spaces


[root@ELK-chaofeng test]# touch 1.txt 2.txt '1 2.txt'
[root@ELK-chaofeng test]# ls
1 2.txt 1.txt 2.txt
[root@ELK-chaofeng test]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 14 12:24 1 2.txt
-rw-r--r-- 1 root root 0 Feb 14 12:24 1.txt
-rw-r--r-- 1 root root 0 Feb 14 12:24 2.txt
[root@ELK-chaofeng test]# find . -type f -print0 | xargs -0 rm -rf
[root@ELK-chaofeng test]# ls

The above parameter-print0, compared to the default-print, the output sequence is not separated by spaces, but by null characters. xargs also has a parameter-0, which can accept input streams spaced by null instead of spaces.


Related articles: