15
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2019-06-18

結論

パスに半角スペースが含まれるファイルを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
$ 
15
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?