Ezen elvéreztem:
rm `find $WORKDIR -name '*.doc' -o -name '*.xls'`
Gyönyörűen menne ha nem lennének szóközzel tagolt fájl és könyvtár neveim a munka könyvtárban. Próbálkoztam ezzel, de nem jött be:
rm `find $WORKDIR -name '*.doc' -o -name '*.xls' | sed -e 's/\\/\ /g'`
Valakinek van ötlete?
- 1470 megtekintés
Hozzászólások
find $WORKDIR \( -name \*.doc -or -name \*.xls \) -exec rm "{}" \;
- A hozzászóláshoz be kell jelentkezni
Köszi, Ő lesz a nyerő. A sima "rm -R" -re megkaptam egyébként hogy "Nincs ilyen fájl vagy könyvtár", holott volt a struktúrában bőven (az ls is érdekes dolgokat művelt). Lehet hogy érdemes lenne kipróbálni melyik alkönyvtár (vagy fájl) neve zizzenti meg. Na majd ha lesz egy kis időm..
A Zahy megoldásán elgondolkodtam egy picit, de gyanítom picit lassabb lenne mint a find közvetlen rm meghívása.
- A hozzászóláshoz be kell jelentkezni
> A Zahy megoldásán elgondolkodtam egy picit, de gyanítom picit lassabb lenne mint a find közvetlen rm meghívása.
Ezt azert gondold at meg1x. Az xargs lenyege, hogy olyan hosszu parancssorokat allit ossze, amekkorat az adott rendszer lehetove tesz (kiveve, ha nem irod elo neki maskent, erre jo (FreeBSD alatt) a -n vagy -L opcio) - azaz annyi parametert ad at a parancsnak, amennyit tud - raadasul erosen ketlem, hogy sh-n keresztul tenne, tehat zavarna a fajlnevben levo szokoz.
Azaz:
"find -exec" eseten: 1 find processz es n db. rm processz.
"find | xargs" eseten: 1 db find processz, 1 db xargs processz, es 1 (esetleg 2, esetleg 3) rm processz (2, 3, ... csak akkor ha *nagyon* sok torlendo van). Azaz maga a torlo parancs sokkal kesobb kezd elindulni (gyulnek a parameterek), de akkor aztan egyszerre kitorol tobb szaz/ezer fajlt akar. Ebben az altalunk irt esetben a find a nagyon lassu (ez mind a ket megoldasban benn van), ezt koveti a fork es exec ideje ami az rm-programok inditasahoz kell. Viszont 2 (3, 4, ..) f/e all szemben rossz esetben tobb ezer f/e -kel. (Nyilvan ha csak 5-10 ilyen fajlod van, akkor nem fogod erzekelni a kulonbseget latvanyosan.)
De ettol meg a find -exec pont ugyan ugy hasznalhato, es jo (csak nem jobb) marad, nyugodtan hasznald.
- A hozzászóláshoz be kell jelentkezni
Hmmm.... -exec rm "{}" +
-exec command ;
Execute command; true if 0 status is returned. All following
arguments to find are taken to be arguments to the command until
an argument consisting of `;' is encountered. The string `{}'
is replaced by the current file name being processed everywhere
it occurs in the arguments to the command, not just in arguments
where it is alone, as in some versions of find. Both of these
constructions might need to be escaped (with a `\') or quoted to
protect them from expansion by the shell. See the EXAMPLES sec-
tion for examples of the use of the `-exec' option. The speci-
fied command is run once for each matched file. The command is
executed in the starting directory. There are unavoidable
security problems surrounding use of the -exec option; you
should use the -execdir option instead.
-exec command {} +
This variant of the -exec option runs the specified command on
the selected files, but the command line is built by appending
each selected file name at the end; the total number of invoca-
tions of the command will be much less than the number of
matched files. The command line is built in much the same way
that xargs builds its command lines. Only one instance of '{}'
is allowed within the command. The command is executed in the
starting directory.
- A hozzászóláshoz be kell jelentkezni
rm *.doc
rm *.xls ?
debian gnu/linux @ linux-2.6.22.24-op1 | patch
info
- A hozzászóláshoz be kell jelentkezni
find $WORKDIR -name '*.doc' -o -name '*.xls' -print0 | xargs -0 rm
nem tetszik?
- A hozzászóláshoz be kell jelentkezni
Plusz idézőjelek közé kell tenni a $WORKDIR-t.
- A hozzászóláshoz be kell jelentkezni
find $WORKDIR -name '*.doc' -o -name '*.xls' -delete
Ha nem lenne ez az opció, és újsorkarakterek nincsenek a fájlnevekben: find $WORKDIR -name '*.doc' -o -name '*.xls' | while read $filename; do rm $filename; done
- A hozzászóláshoz be kell jelentkezni
find --version
GNU find version 4.1.20
Sajnos ebben még nincs delete opció, pedig ez lenne a legfrappánsabb ahogy nézem. Kösz a tippet!
- A hozzászóláshoz be kell jelentkezni
És a leginkább gnufind-specifikus megoldás...
- A hozzászóláshoz be kell jelentkezni
rm $filename
helyett
rm "$filename"
különben szhatja, mert a névben levő szóközök miatt az rm nem 1, hanem 2 (3, 4, stb) db fájlnévnek fogja tekinteni az átadott 1 db. paramétert
- A hozzászóláshoz be kell jelentkezni
Ja, valóban.
- A hozzászóláshoz be kell jelentkezni
Bocs a kotozkodesert, de most lattam csak, hogy ezen kivul nem
while read $filename
, hanem
while read filename
- de az elv tokeletes, a kivitelezes lett csak pongyola ;-)
- A hozzászóláshoz be kell jelentkezni
Jaja :)
- A hozzászóláshoz be kell jelentkezni