#!/bin/sh for i in `find . -regex '.*\._.*'`; do rm $i; echo "removing $i"; done
If you save that as "/home/$USER/bin/cleanup" and do a "chmod 777 /home/$USER/bin/cleanup" it will be available next time you open a terminal by just typing "cleanup".
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)
#!/bin/sh for i in `find . -regex '.*\._.*'`; do rm $i; echo "removing $i"; done
find . -regex '.*\._.*' -exec rm {} \;
find . -regex '.*\._.*' -print0 | xargs -0 rm
for file in `find . -name ._*`; do echo "Removing $file"; rm $file; done;
You need to create an account or log in to post comments to this site.
» Compiling GNU coreutils on M... in shell osx mac bash unix linux compile man manpage path info gnu seq coreutils infopath
» Compiling GNU sed 4.1.4 on M... in shell osx mac bash unix linux sed gnu case insensitive
» Compile & install git on Mac... in version shell osx mac bash unix linux xcode git control rudix gpg gettext
» ASCII art demo using AAlib in shell osx mac bash unix linux installer ASCII art demo aalib pkg
» MPlayer on Mac OS X in shell osx mac bash line unix linux mplayer file array video macports youtube audio ln
» Reverse DNS from command line in shell osx unix linux
Snippets (source code soon to be available) developed by Peter Cooper and powered by Ruby On Rails
expand to:
Which will attempt to do things to files my, song, 1 and 2, none of which exist. It's not hard to imagine that file names with spaces might result in other files being deleted if their names happen to correspond in part.
It will also fall over with big directories, as the find command expands to an argument list for the for command, and may fairly easily wind up too long.
It could also be dangerous with certain file names e.g. if you were daft enough to have a file name containing the string ";\ rm\ -rf\ /", say.
You can partially solve the punctuation issue and fix the large directory problem using this type of approach instead:
This pipes the output of the find operation, which may now be of arbitrary length, into the while loop, and n is assigned by the read command to the whole file name in each case. $n is placed in quotes so that when n is expanded, punctuation and spaces are protected from further shell interpretation.
This approach doesn't fail with single quotes in filenames (quite common in e.g. MP3 file names, as an apostrophe), but it doesn't defend you against file names with forward slashes in them, since the forward slash "escapes" the next character, even in quotes - so if you have a file named "file\one", the above will attempt to remove a file called "fileone" instead. To fix this, you need to escape the escape character:
Note how you have to escape the escape character in the argument to sed.
There might be issues with other special-meaning characters I haven't thought of, too!