Help us understand the problem. What is going on with this article?

パスに半角スペースが含まれるfindの結果をgrepしてxargsで削除する方法

More than 1 year has passed since last update.

結論

パスに半角スペースが含まれるファイルをfindして任意の文字でgrepして一括削除する
$ find {ディレクトリ} -type f | grep -i {ファイル名の一部} | sed -e 's/ /\\ /g' | xargs rm

Macのディレクトリには半角スペースが含まれたものがあります

$ cd ~
$ find Library/ -maxdepth 1 -type d | grep " "
Library//Application Support
Library//Autosave Information
Library//Saved Application State
Library//Address Book Plug-Ins
Library//PDF Services
Library//Application Scripts
Library//Keyboard Layouts
Library//Internet Plug-Ins
Library//Mobile Documents
Library//Group Containers
Library//Fonts Disabled
Library//Screen Savers
Library//Input Methods
Library//Mobile Documents.1780041924

スペースを含むディレクトリからファイル名でgrepしたりします

$ find Library/Application\ Support/com.apple.sharedfilelist/ -type f | grep -i xcode
Library/Application Support/com.apple.sharedfilelist//com.apple.LSSharedFileList.ApplicationRecentDocuments/com.apple.dt.xcode.sfl
Library/Application Support/com.apple.sharedfilelist//com.apple.LSSharedFileList.ApplicationRecentDocuments/com.apple.dt.xcode.sfl2

そのgrepしたファイルを削除したいです

試行錯誤する

工夫をしないでxargを使うと半角スペースでパスが分断されてうまくいきません

$ find Library/Application\ Support/com.apple.sharedfilelist/ -type f | grep -i xcode | xargs rm
rm: Library/Application: No such file or directory
rm: Support/com.apple.sharedfilelist//com.apple.LSSharedFileList.ApplicationRecentDocuments/com.apple.dt.xcode.sfl: No such file or directory
rm: Library/Application: No such file or directory
rm: Support/com.apple.sharedfilelist//com.apple.LSSharedFileList.ApplicationRecentDocuments/com.apple.dt.xcode.sfl2: No such file or directory

find--print0を付けてみてもうまくいきません

manの説明
-print  This primary always evaluates to true.  It prints the pathname of the current file to standard output.  If none of -exec, -ls,
     -print, -print0, or -ok is specified, the given expression shall be effectively replaced by ( given expression ) -print.
$ find Library/Application\ Support/com.apple.sharedfilelist/ -type f -print0 | grep -i xcode | xargs rm
rm: Binary: No such file or directory
rm: file: No such file or directory
rm: (standard: No such file or directory
rm: input): No such file or directory
rm: matches: No such file or directory

$ find Library/Application\ Support/com.apple.sharedfilelist/ -type f -print0 | grep -i xcode
Binary file (standard input) matches

更にgrepxargs -0をつけてみたらファイル名でのgrepではなくなった

$ find Library/Application\ Support/com.apple.sharedfilelist/ -type f -print0 | xargs -0 grep -i xcode
Binary file Library/Application Support/com.apple.sharedfilelist//com.apple.LSSharedFileList.ApplicationRecentDocuments/com.apple.dt.xcode.sfl matches
Binary file Library/Application Support/com.apple.sharedfilelist//com.apple.LSSharedFileList.ApplicationRecentDocuments/com.apple.dt.xcode.sfl2 matches
# ↓このファイルは削除対象にしたくない
Binary file Library/Application Support/com.apple.sharedfilelist//com.apple.LSSharedFileList.RecentApplications.sfl2 matche

$ find Library/Application\ Support/com.apple.sharedfilelist/ -type f | xargs -0 grep -i xcode
grep: Library/Application Support/com.apple.sharedfilelist//com.apple.LSSharedFileList.RecentApplications.sfl
Library/Application Support/com.apple.sharedfilelist//com.apple.LSSharedFileList.RecentDocuments.sfl2
# 省略:削除対象にしたくないファイルがいっぱい
: File name too long

xargs-Iをつけてダブルクォーテーションで囲ってみたがうまくいかない

-print0とか-0を止めてxargsで置き換えをやってみる

manの説明
 -I replstr
         Execute utility for each input line, replacing one or more occurrences of replstr in up to replacements (or 5 if no -R flag is speci-
         fied) arguments to utility with the entire line of input.  The resulting arguments, after replacement is done, will not be allowed to
         grow beyond 255 bytes; this is implemented by concatenating as much of the argument containing replstr as possible, to the con-
         structed arguments to utility, up to 255 bytes.  The 255 byte limit does not apply to arguments to utility which do not contain
         replstr, and furthermore, no replacement will be done on utility itself.  Implies -x.
# イメージと合っているか確認する
$ find Library/Application\ Support/com.apple.sharedfilelist/ -type f | grep -i xcode | xargs -I{} echo \"{}\"
"Library/Application Support/com.apple.sharedfilelist//com.apple.LSSharedFileList.ApplicationRecentDocuments/com.apple.dt.xcode.sfl"
"Library/Application Support/com.apple.sharedfilelist//com.apple.LSSharedFileList.ApplicationRecentDocuments/com.apple.dt.xcode.sfl2"

# 削除してみる
$ find Library/Application\ Support/com.apple.sharedfilelist/ -type f | grep -i xcode | xargs -I{} rm \"{}\"
rm: "Library/Application Support/com.apple.sharedfilelist//com.apple.LSSharedFileList.ApplicationRecentDocuments/com.apple.dt.xcode.sfl": No such file or directory
rm: "Library/Application Support/com.apple.sharedfilelist//com.apple.LSSharedFileList.ApplicationRecentDocuments/com.apple.dt.xcode.sfl2": No such file or directory

# ダブルスラッシュがだめなのかと思ってスラッシュに置換してみる
$ find Library/Application\ Support/com.apple.sharedfilelist/ -type f | grep -i xcode | sed s#\//\#\/#g
Library/Application Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.ApplicationRecentDocuments/com.apple.dt.xcode.sfl
Library/Application Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.ApplicationRecentDocuments/com.apple.dt.xcode.sfl2

# やっぱりうまくいかない
$ find Library/Application\ Support/com.apple.sharedfilelist/ -type f | grep -i xcode | sed s#\//\#\/#g | xargs -I{} rm \"{}\"
rm: "Library/Application Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.ApplicationRecentDocuments/com.apple.dt.xcode.sfl": No such file or directory
rm: "Library/Application Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.ApplicationRecentDocuments/com.apple.dt.xcode.sfl2": No such file or directory

sedでスペースにバックスラッシュをつけたらうまくいった

さんざん試行錯誤してから思いついた。
シンプルにスペースにエスケープをつければよいのでは?と。

# イメージと合っているか確認する
$ find Library/Application\ Support/com.apple.sharedfilelist/ -type f | grep -i xcode | sed -e 's/ /\\ /g'
Library/Application\ Support/com.apple.sharedfilelist//com.apple.LSSharedFileList.ApplicationRecentDocuments/com.apple.dt.xcode.sfl
Library/Application\ Support/com.apple.sharedfilelist//com.apple.LSSharedFileList.ApplicationRecentDocuments/com.apple.dt.xcode.sfl2

# 削除してみる
$ find Library/Application\ Support/com.apple.sharedfilelist/ -type f | grep -i xcode | sed -e 's/ /\\ /g' | xargs rm

# 本当に削除されたかgrepしてみる
$ find Library/Application\ Support/com.apple.sharedfilelist/ -type f | grep -i xcode
$ 
ponsuke0531
びっくりするほど物覚えが悪いが、エンジニアを目指しています。
http://ponsuke-tarou.hatenablog.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away