LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-10-2006, 05:47 PM   #1
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Rep: Reputation: 30
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!
 
Old 05-10-2006, 06:10 PM   #2
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
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")
 
Old 05-11-2006, 02:33 AM   #3
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
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)
 
Old 05-11-2006, 02:52 AM   #4
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Original Poster
Rep: Reputation: 30
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?
 
Old 05-11-2006, 03:08 AM   #5
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
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'

Last edited by muha; 05-11-2006 at 03:11 AM.
 
Old 05-11-2006, 06:01 AM   #6
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
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.

Last edited by ioerror; 05-11-2006 at 06:08 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep question djgerbavore Linux - Newbie 3 11-04-2004 10:53 AM
Question about Grep irfanhab Linux - Newbie 6 08-23-2004 02:57 PM
grep question Hockeyfan Linux - General 1 05-15-2004 02:09 AM
grep question vasanthraghavan Programming 3 04-23-2004 12:32 AM
grep question keyops Programming 2 04-09-2004 09:17 PM


All times are GMT -5. The time now is 04:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration