find(1)
�ͥ��Ȥ����Τ⡢���Ū�˥ͥåȤ����䤫�ˤ�������ͥ��ˤʤ�ĤĤ���褦�Ǥ���
- ���ޤ���xargs�����������ĥ���Ƥߤ�
- find�ǥե����븡������ - builder by ZDNet Japan
�Ȥ����櫓�ǡ����ޤ���find��perl�������Τ褵��Ƴ�ǧ���Ƥߤ뤳�Ȥˤ��Ƥߤޤ���
���ܷ�
�ޤ��ϡ������Ф��Ƥ����ޤ��礦��
find [options and args of find] -print0 | perl -l0ne 'perl program'
find�����Ĥ����ե�������ĸ��Ĥ���ȡ�perl��$_
�ˤ��Υե�����̾������ΤǤ�����Ф��Ʋ��餫�ν�����Ԥ��ޤ���
perl¦��-l0ne
�Ȥ����Τϡ��ʲ���Ʊ���ˤʤ�ޤ���
BEGIN { $/ = "\n"; $\ = "\000"; } LINE: while (defined($_ = <ARGV>)) { chomp $_; do something with $_; }
-print0
������Ƥ���Τϡ��ե�����̾���Ѥʾ��Ǥ�Ŭ�ڤ˽������뤿�ᡣ�ܤ�����
�򻲾ȤΤ��ȡ�������xargs(1)
�Ȥϰۤʤꡢperl�ξ������Ȥʤ�Τϥե�����̾��"\n"�����äƤ�����ΤߤʤΤǡ��̾��
find [options and args of find] | perl -lne 'perl program'
�ǤۤȤ�����ꤢ��ޤ��󡣰ʲ������㡣
% find ~/Music/iTunes/iTunes\ Music/A* -type f | xargs echo xargs: unterminated quote % find ~/Music/iTunes/iTunes\ Music/A* -type f | perl -lne 'print "($_)"' (/Users/dankogai/Music/iTunes/iTunes Music/ABBA/Gold/01 Dancing Queen.m4a) (/Users/dankogai/Music/iTunes/iTunes Music/ABBA/Gold/02 Knowing Me, Knowing You.m4a) ...
xargs
��space(\x20)�ò°·¤ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Í¤Æ¤ï¿½ï¿½ï¿½Î¤ï¿½ï¿½Ð¤ï¿½ï¿½ï¿½perl�Ǥ�space��ޤ�Ǥ��Ƥâ¤ï¿½ï¿½ï¿½ï¿½È½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Æ¤ï¿½ï¿½ë¤³ï¿½È¤ï¿½ï¿½ï¿½Ç§ï¿½Ç¤ï¿½ï¿½Þ¤ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ä¤¬xargs
��ۤȤ�ɻȤ�ʤ���ͳ�ΰ�ĤǤ���
���ʤߤ�-lne
�������-lpe
�Ȥ���ȡ������ɤμ¹Ը��print $_
���Ƥ����Τǡ�"verbose mode"�Ȥ��ƻȤ����Ȥ�����ޤ���
perl��xargs������˻Ȥ����Ȥ�����
space�ʤɤ�ޤ���ե�����̾�ò¤¤ï¿½ï¿½ï¿½È°ï¿½ï¿½ï¿½ï¿½ï¿½È¤ï¿½ï¿½ï¿½ï¿½Î¤Ï¾ï¿½Ç½Ò¤Ù¤ï¿½ï¿½Ì¤ï¿½Ç¤ï¿½ï¿½ï¿½ï¿½ï¿½Â¾ï¿½Ë¤â¤¢ï¿½ï¿½Þ¤ï¿½ï¿½ï¿½
���ޤ���xargs�����������ĥ���Ƥߤ�xargs��Ȥä����˾��2�Ĥ���Ȥɤ��㤦���Ȥ����ȡ����ޥ�ɼ¹Ի��ΰ��������¤򵤤ˤ��Ĥġ�rm�˥��ꥮ��θĿ��Υե�����̾���Ϥ��ơ�ɬ�׺���¤β�������¹Ԥ��Ƥ���ޤ���
��������find | perl -lne unlink
�ʤ顢perl�μ¹Բ���ϥե����뤬������������1��Ǥ������Τ������ǡ�perl����xargs����ç¤ï¿½Ê¥×¥ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ç¤ï¿½ï¿½ï¿½Ë¤ï¿½Ø¤ï¿½é¤ºï¿½ï¿½ï¿½ï¿½ï¿½Î¤È¤ï¿½ï¿½ï¿½xargs�����Ψ���������Ȥ�¿���ΤǤ���
�⤢�碌�Ƥ�����������
����ˡ�xargs��¾��binutils��Ʊ�������Ķ��ˤ�äƻȤ��륪�ץ���󤬰ۤʤ�ʤɤ����꤬����Τ��Ф���perl�ξ��Ķ��������äȾ��ʤ��Ȥ����Τ�̥�ϤǤ���linux�Ǥ�BSD�Ǥ⡢����OS X�Ǥ�GNU (bin|core)utils��fink��darwin ports�ʤɤ����줿���Ǥ⡢����Ʊ�����ޥ�ɤǹԤ���Ȥ����Τ�̥�ϤǤ���
���㽸
- find �����ե��������
% find dir -type f -print0 | perl -l0ne unlink % find dir -type f -delete # BSD�ϤΤ�
- find ������Τ�ǥ��쥯�ȥ��ޤ���
find dir -depth | perl -nle 'unlink or rmdir'
find¦��
-depth
���դ��Ƥ���Τ��ݥ���ȡ����ξ�硢�ǥ��쥯�ȥ�ϺǸ��ɽ�������Τǡ��ե��������������Ǥ��줤��rmdir�Ǥ���櫓�Ǥ��� - �ե�������ư
% find dir ... -print0 | perl -MFile::Basename -l0ne 'rename $_, "/path/to/" . basename($_)' % find dir ... -print0 | perl -MFile::Copy -MFile::Basename -l0ne 'move($_, "/path/to/" . basename($_)'
��Ԥϥե����륷���ƥ��ޤ����äƤ�����ˤ�Ȥ��ޤ���
- ��ĥ�Ҥ��ѹ�
find dir -type f -name \*.jpeg | perl -nle '$o=$_;s/\.jpe?g/.jpg/; rename $o,$_'
- perl¦�Ǿ��򤵤�˹ʤ�
find dir -type f | perl -nle '/\jpe?g$/ or next; $o=$_;s/\.jpe?g/.jpg/; rename $o,$_'
��Ȥۤ�Ʊ�����Ȥ򤷤ޤ��������򤵤�˺٤����ʤ�Τ�next
�����ѤǤ��ޤ���
find2perl - ���ä�find���ޥ�ɤޤ�perl��
�����ʣ���ʤ��Ȥ��ꤿ�����ˤϡ�find | perl
�ǤϤʤ���find��ʬ��perl�ˤ�餻�Ƥ��ޤ��Ȥ����꤬�Ȥ��ޤ������λ��������ʤΤ���find2perl
���ޥ�ɡ�find
��ʬ��񤤤ơ�find
��find2perl
�ˤ�����ȡ�find�����Τ��Ȥ���perl������ץȤ���������ޤ���
% find2perl . -type f -name \*.pl -o -name \*.pm #! /usr/local/bin/perl -w eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}' if 0; #$running_under_some_shell use strict; use File::Find (); # Set the variable $File::Find::dont_use_nlink if you're using AFS, # since AFS cheats. # for the convenience of &wanted calls, including -eval statements: use vars qw/*name *dir *prune/; *name = *File::Find::name; *dir = *File::Find::dir; *prune = *File::Find::prune; sub wanted; # Traverse desired filesystems File::Find::find({wanted => \&wanted}, '.'); exit; sub wanted { my ($dev,$ino,$mode,$nlink,$uid,$gid); (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -f _ && /^.*\.pl\z/s || /^.*\.pm\z/s && print("$name\n"); }
���ȤϤ����Ŭ�����Խ����ƻȤ���OK������Ū���Խ���ɬ�פʤΤϡ�File::Find::find({wanted => \&wanted}, '.');
�Υǥ��쥯�ȥ�����wanted()
����ȤΤߤǤ���
����ˤ����������������
���Ȥ��С�perl��xargs��Ƽ�������Τ⤽��ۤ��񤷤����ȤǤϤ���ޤ����㤨��
�ʤɤ�����������
Enjoy!
Dan the Perl Monger
�ʤ���xargs(1)��binutils�ǤϤʤ���findutils�ΰ�����Ȼפ���