how to remove all except one file? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Unix/Linux Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

how to remove all except one file?

UNIX for Dummies Questions & Answers


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-29-2008
praveen_b744 praveen_b744 is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 24 August 2010, 9:15 AM EDT
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Error how to remove all except one file?

How can I remove all files and folders in a directory except one perticular file?

Suppose in a folder we have files f1,f2,f3.In this I want to delete all files except f1.

can any one tel me?



thanks in advance..
Sponsored Links
    #2  
Old Unix and Linux 01-29-2008
pt14 pt14 is offline
Registered User
 
Join Date: Nov 2005
Last Activity: 18 June 2011, 5:29 PM EDT
Posts: 31
Thanks: 2
Thanked 0 Times in 0 Posts
ls *|grep -v f1|xargs rm -rf
Sponsored Links
    #3  
Old Unix and Linux 01-29-2008
vino's Unix or Linux Image
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 12 March 2015, 5:33 AM EDT
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 13 Times in 13 Posts
Or copy f1 to /tmp, rm all the files in the current directory and move back /tmp/f1 to the current directory.
    #4  
Old Unix and Linux 01-29-2008
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 23 March 2015, 5:18 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,689
Thanks: 184
Thanked 623 Times in 581 Posts
Quote:
Originally Posted by praveen_b744 View Post
How can I remove all files and folders in a directory except one perticular file?

Suppose in a folder we have files f1,f2,f3.In this I want to delete all files except f1.
[...]
[ksh]


Code:
rm !(f1)

[zsh]


Code:
rm ^f1

[bash]


Code:
shopt -s extglob
rm !(f1)

Sponsored Links
    #5  
Old Unix and Linux 03-01-2009
Tuche Tuche is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 18 March 2009, 7:12 PM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry for bumping this, but . . .

if i wanted to delete all files and/or folders inside a specific folder but keep the files and folders that start with p and m, i should do . . .

rm -R *.* !(p*) !(m*)

is that correct ?

Thanks !
Sponsored Links
    #6  
Old Unix and Linux 03-01-2009
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 23 March 2015, 5:18 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,689
Thanks: 184
Thanked 623 Times in 581 Posts
After enabling the extended glob you could try something like this:


Code:
rm -r -- !(@(m|p)*)

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to remove blank lines in a file and save the file with same name? vel4ever Shell Programming and Scripting 6 01-10-2012 02:31 AM
rm: cannot remove file: Read-only file system figaro UNIX for Dummies Questions & Answers 4 12-23-2011 01:14 AM
Remove duplicate lines from first file comparing second file gani_85 Shell Programming and Scripting 3 10-21-2011 07:43 AM
how to copy a file without remove the contents of the target file? ahmedamer12 UNIX for Dummies Questions & Answers 2 04-22-2011 05:19 PM
Batch file to remove double quotes from a file akashtcs Shell Programming and Scripting 11 01-11-2010 10:05 AM



All times are GMT -4. The time now is 06:16 AM.