LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   terminal question (I think it has to do with grep) (https://www.linuxquestions.org/questions/linux-newbie-8/terminal-question-i-think-it-has-to-do-with-grep-443610/)

itz2000 05-10-2006 05:47 PM

terminal question (I think it has to do with grep)
 
Hello

If I want to delete all the other files then *.avi
I can do this
Code:

mkdir temp
mv *.avi temp
rm -f *.*
mv temp/*.avi temp/../
rm -rf temp


but... if there another option? i think there's something with grep or ugrep.. can u help me out to find another thing? thanks!

ioerror 05-10-2006 06:10 PM

Well, I use zsh, so I'd just do

Code:

rm *~*.avi
but that aint gonna work in bash.

How about this:

Code:

rm $(find . -not -name "*.avi")

muha 05-11-2006 02:33 AM

With grep you could do this:
Code:

rm `ls .|grep -v avi$`
Not recursive and can only be used in the dir in which you want to delete the files.

@ioerror: Maybe a selection on files would be nice to add to your suggestion (to not delete directories)
(although rm finds this out by itself as well: rm: cannot remove `.' or `..'):
Code:

rm $(find . -not -name "*.avi" -type f)
The advantages of using find is that it's recursive and the directory (.) can be replaced by any other dir like so:
Code:

rm $(find ./testdir -not -name "*.avi" -type f)

itz2000 05-11-2006 02:52 AM

Quote:

Originally Posted by muha
With grep you could do this:
Code:

rm `ls .|grep -v avi$`
Not recursive and can only be used in the dir in which you want to delete the files.

@ioerror: Maybe a selection on files would be nice to add to your suggestion (to not delete directories)
(although rm finds this out by itself as well: rm: cannot remove `.' or `..'):
Code:

rm $(find . -not -name "*.avi" -type f)
The advantages of using find is that it's recursive and the directory (.) can be replaced by any other dir like so:
Code:

rm $(find ./testdir -not -name "*.avi" -type f)

Code:

[zuki@localhost Desktop]$ rm $(find . -not -name "*.torrent" -type f)
bash: /bin/rm: Argument list too long

huh?

muha 05-11-2006 03:08 AM

Quote:

Originally Posted by itz2000
Code:

[zuki@localhost Desktop]$ rm $(find . -not -name "*.torrent" -type f)
bash: /bin/rm: Argument list too long

huh?

Try to echo the output of the $(..) to see what's going on:
Code:

echo $(find . -not -name "*.torrent" -type f)
/also i can recommend using the alias for rm to be interactive for each remove while testing:
Code:

alias rm='rm -i'

ioerror 05-11-2006 06:01 AM

Quote:

Originally Posted by muha
@ioerror: Maybe a selection on files would be nice to add to your suggestion (to not delete directories)
(although rm finds this out by itself as well: rm: cannot remove `.' or `..'):

Yes, good point muha, I was assuming there would be no subdirectories, but using -type f is more generally applicable.

Quote:

[zuki@localhost Desktop]$ rm $(find . -not -name "*.torrent" -type f)
bash: /bin/rm: Argument list too long
Ok, so the argument list is too long. You'll have to turn it around:

Code:

find . -not -name "*.torrent" -type -f -exec rm {} \;
though this will be slower as it calls rm for every file.

There is a maximum size the command line arguments can have, which you can see via

Code:

getconf ARG_MAX
On my system, it's 131072, i.e. 128K. If your argument list is longer than that, you'll need to find another way to write the command. With find, you can use the -exec option, another alternative is xargs.


All times are GMT -5. The time now is 05:04 AM.