Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Rm all files but two

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-12-2017   -   Original Discussion by mohtashims
mohtashims's Unix or Linux Image
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 3 November 2017, 12:11 PM EDT
Posts: 779
Thanks: 204
Thanked 7 Times in 7 Posts
Hammer & Screwdriver Rm all files but two

I am on Solaris using bash profile.

I wish to delete all files under /tmp/data/ except output.txt and hello.txt

I prefer using the one line command like rm

I tried this command but it is failing.


Code:
rm -rf /tmp/data/* !(@(output.txt|hello.txt))
bash:  !: event not found.

Can you please suggest. I would preferably want a command that works on both Linux and Solaris.
Sponsored Links
    #2  
Old Unix and Linux 07-12-2017   -   Original Discussion by mohtashims
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 13 December 2017, 9:11 PM EST
Location: Simplicity
Posts: 3,864
Thanks: 320
Thanked 1,289 Times in 1,167 Posts
Turn on extended globbing first:

Code:
shopt -s extglob

Sponsored Links
    #3  
Old Unix and Linux 07-12-2017   -   Original Discussion by mohtashims
mohtashims's Unix or Linux Image
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 3 November 2017, 12:11 PM EDT
Posts: 779
Thanks: 204
Thanked 7 Times in 7 Posts
Quote:
Originally Posted by MadeInGermany View Post
Turn on extended globbing first:

Code:
shopt -s extglob

Still, I cannot see it working. Below is the error.


Code:
bash-3.2$ shopt -s extglob
 bash-3.2$ rm -rf /tmp/data/* !(@(output.txt))
rm: cannot read directory enav/output: Permission denied
rm: Unable to remove directory enav: File exists
rm: Unable to remove directory hsperfdata_u543267: Permission denied
rm: Unable to remove directory hsperfdata_vbarne: Permission denied

Strangely along with the error the /tmp/data/ folder itself gets deleted.

Last edited by mohtashims; 07-12-2017 at 04:04 PM..
    #4  
Old Unix and Linux 07-12-2017   -   Original Discussion by mohtashims
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 13 December 2017, 9:11 PM EST
Location: Simplicity
Posts: 3,864
Thanks: 320
Thanked 1,289 Times in 1,167 Posts
I think your pattern is wrong.
Should be

Code:
ls -ld /tmp/data/!(output.txt|hello.txt)

If not 100% confident, test with ls -ld first, then run rm -rf.
The Following User Says Thank You to MadeInGermany For This Useful Post:
mohtashims (07-12-2017)
Sponsored Links
    #5  
Old Unix and Linux 07-12-2017   -   Original Discussion by mohtashims
mohtashims's Unix or Linux Image
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 3 November 2017, 12:11 PM EDT
Posts: 779
Thanks: 204
Thanked 7 Times in 7 Posts
Quote:
Originally Posted by MadeInGermany View Post
I think your pattern is wrong.
Should be

Code:
ls -ld /tmp/data/!(output.txt|hello.txt)

If not 100% confident, test with ls -ld first, then run rm -rf.

You are right !! The syntax was incorrect. This works Linux
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Append string to all the files inside a directory excluding subdirectories and .zip files Ravi Kishore Shell Programming and Scripting 4 04-23-2015 03:58 AM
How to create zip/gz/tar files for if the files are older than particular days in UNIX or Linux? Mallikgm Shell Programming and Scripting 4 10-03-2013 01:10 PM
write a program in c in unix that display the files(includ sub-direc and files within) in a sorted pappu kumar jha UNIX for Dummies Questions & Answers 1 01-26-2011 02:06 PM
How to extract data from indexed files (ISAM files) maintained in an unix server. devina Shell Programming and Scripting 2 12-12-2010 09:24 AM
How to retrieve all the linked script files/ctl files/sql files? franksubramania Shell Programming and Scripting 1 05-04-2009 02:47 PM



All times are GMT -4. The time now is 12:20 AM.