Rm all files but two

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 07-12-2017
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.
# 2  
Old 07-12-2017
Turn on extended globbing first:
Code:
shopt -s extglob

# 3  
Old 07-12-2017
Quote:
Originally Posted by MadeInGermany
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 07-12-2017
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.
This User Gave Thanks to MadeInGermany For This Post:
mohtashims (07-12-2017)
# 5  
Old 07-12-2017
Quote:
Originally Posted by MadeInGermany
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 Smilie
Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
SBATCH trinity for multiple files and rename/move the output files @man Shell Programming and Scripting 1 08-23-2016 12:23 PM
Shell script for field wise record count for different Files .csv files Kirands Shell Programming and Scripting 14 01-21-2016 05:06 PM
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
Read files in shell script code and run a C program on those files shoaibjameel123 Shell Programming and Scripting 2 09-17-2013 05:48 AM
need a shell script to extract the files from source file and check whether those files existonserve muraliinfy04 Shell Programming and Scripting 13 02-22-2012 08:14 AM
Need help creating a script to FTP files to a server and then delete the files that were transfered. jmalfhs Shell Programming and Scripting 4 01-18-2012 10:55 AM
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
Compare 2 folders to find several missing files among huge amounts of files. jiapei100 Shell Programming and Scripting 1 05-10-2010 04:00 PM
How to Pull out multiple files from DB table and redirect all those files to a differetn directory? ss3944 Shell Programming and Scripting 3 11-21-2009 02:08 AM
Delete all files if another files in the same directory has a matching occurence of a specific word premier_de Shell Programming and Scripting 2 11-20-2009 05:48 AM
copying of files by userB, dir & files owned by userA Hangman2 UNIX for Advanced & Expert Users 2 08-07-2009 12:41 PM
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 09:31 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?