You are not logged in.
Pages: 1
This is a script I wrote, which clears the current working folder, apart from the files listed as parameters.
e.g.:
[sweiss@raviv2 gambas]$ ls
filelist gambas-1.0.14-1.pkg.tar.gz gambas-1.0.14.tar.bz2 pkg PKGBUILD src
[sweiss@raviv2 gambas]$ irm PKGBUILD gambas-1.0.14-1.pkg.tar.gz
[sweiss@raviv2 gambas]$ ls
gambas-1.0.14-1.pkg.tar.gz PKGBUILD
I thought I'd share it with you.
Important: This script is written in BASH, and I am not at all an expert. It also does not yet handle files/directories inside subdirectories - If you try doing `irm ./src/file`, the script will quit with an error message and do nothing.
Also, it may be inefficient - If it is, please let me know how to correct it. I suggest reviewing the code in the script before running it - it's not much.
Anyway, here it is:
#!/bin/sh
LS=$(which ls)
WC=$(which wc)
FIND=$(which find)
GREP=$(which grep)
RM=$(which rm)
SED=$(which sed)
AWK=$(which awk)
FILENAME=
GREP_COMMAND=
# Removes any leading
prep_filename()
{
if ! [ -n "$1" ]
then
return
fi
FILENAME="$1"
if [ $(echo "$FILENAME" | cut -c1-2) = "./" ]
then
FILENAME=$(echo "$FILENAME" | cut -c3-)
prep_filename "$FILENAME"
fi
if [ $(echo "$FILENAME" | rev | cut -c1) = "/" ]
then
FILENAME=$(echo "$FILENAME" | rev | cut -c2- | rev)
prep_filename "$FILENAME"
fi
}
IS_FIRST_FILE=1
for i in $(seq 1 $#)
do
filename=${!i}
if ! [ -e "$filename" ]
then
echo "No such file or directory: ${filename}"
exit 1
fi
prep_filename "$filename"
if [ $(echo "$FILENAME" | sed 's:/::g') != "$FILENAME" ]
then
echo irm does not yet support keeping directories/files inside subdirectories. Aborted.
exit 1
fi
if [ $IS_FIRST_FILE -eq 1 ]
then
GREP_COMMAND=${GREP_COMMAND}"$GREP -vx "$FILENAME""
IS_FIRST_FILE=0
else
GREP_COMMAND=${GREP_COMMAND}" | $GREP -vx "$FILENAME""
fi
done
# The icky sed part is for escaping the characters, if such exist.
FILES_TO_REMOVE=$(echo $(eval "$LS -1 -A . | ${GREP_COMMAND} | $SED 's:"'\'":"'\\'":g' | $AWK '{print "\""$0"\""}'"))
if ! [ -n "$FILES_TO_REMOVE" ]
then
echo No files removed.
exit 1
fi
eval "$RM -rf $FILES_TO_REMOVE"
Enjoy.
Some PKGBUILDs: http://members.lycos.co.uk/sweiss3
Offline
Incidentally, zsh has something like that built in.
You just use "rm ^foobar" (or "rm ^(foo1|foo2)") and it erases everything except that. And it's not limited to rm.
Offline
o/ zsh.
remember you have to had extended globbing turned on for that to work if I remember.
Offline
Incidentally, zsh has something like that built in.
You just use "rm ^foobar" (or "rm ^(foo1|foo2)") and it erases everything except that. And it's not limited to rm.
Awesome. Thanks for the tip. zsh++ 8)
·¬»· i am shadowhand, powered by webfaction
Offline
Awesome. Thanks for the tip. zsh++ 8)
Another great feature of zsh (which you're perhaps not familiar with) is "**", which navigates through all the subdirectories - for one, it's more handy than find when searching for filenames ;-)
% ls /lib/modules/2.6.18-ARCH/**/*nvidia*
/lib/modules/2.6.18-ARCH/kernel/drivers/char/agp/nvidia-agp.ko /lib/modules/2.6.18-ARCH/kernel/drivers/video/nvidia.ko
Sorry for hijacking the thread, sweiss. Couldn't resist sharing with my favourite features.
Offline
Not a problem at all. Perhaps I should too take the plunge and start using ZSH.
Does it have any noticeable drawbacks?
Some PKGBUILDs: http://members.lycos.co.uk/sweiss3
Offline
The only drawback is that it takes some time and effort to discover all the (great) features ;-)
Offline
Yeah, ZSH is really great if you have the right configuration for it. Takes some time to setup, but once you're used to it you'll never want to use a bash again
Offline
Gonna hijack a bit:
% ls /lib/modules/2.6.18-ARCH/**/*nvidia* /lib/modules/2.6.18-ARCH/kernel/drivers/char/agp/nvidia-agp.ko /lib/modules/2.6.18-ARCH/kernel/drivers/video/nvidia.ko
While this is a cool feature, the unintuitiveness and non-portability bothers me...
find /lib/modules/2.6.18-ARCH -name *nvidia*
works everywhere and gives you more options.... I mean, for example:
find /lib/modules/2.6.18-ARCH -iname *nvidia* -delete
iname is case-insensitive and -delete deletes them - try doing that with shell features (zsh OR bash) and it's a PITA
Offline
try doing that with shell features (zsh OR bash) and it's a PITA
setopt nocaseglob
rm /lib/modules/2.6.18-ARCH/**/*nvidia*
I dare say that once nocaseglob is set, it's even more simple/intuitive than find. To each it's own. Find is indeed a great and powerful tool, but zsh has its advantages too - ability to use globbing for _any_ CLI operation (ls, rm, grep et al.) is obviously a nice thing. And it allows to distunguish files with different rights/directories/symlinks as well, bringing it even closer to find's abilities. But actually - they're quite complementary.
Yay for choice!
Offline
Pages: 1