Rm all files but two


Login or Register 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.
# 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 03: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 or Register to Reply

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

More UNIX and Linux Forum Topics You Might Find Helpful
Shell script for field wise record count for different Files .csv files Kirands Shell Programming and Scripting 14 01-21-2016 04: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 02:58 AM
Listing the file name and no of records in each files for the files created on a specific day Showdown Shell Programming and Scripting 5 05-07-2014 10:20 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 12:10 PM
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 07:14 AM
sort the files based on timestamp and execute sorted files in order saidutta123 Shell Programming and Scripting 1 09-18-2011 02:47 PM
AIX system.... deleting files in remote directory after retrieving files vinayparakala Shell Programming and Scripting 3 03-05-2011 04:41 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 01: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 08:24 AM
Apply 'awk' to all files in a directory or individual files from a command line ScKaSx Shell Programming and Scripting 4 11-04-2010 06:50 PM
How to safely copy full filesystems with large files (10Gb files) dragonov7 Solaris 14 09-27-2010 06:20 PM
Compare 2 folders to find several missing files among huge amounts of files. jiapei100 Shell Programming and Scripting 1 05-10-2010 03:00 PM
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 04:48 AM
How to retrieve all the linked script files/ctl files/sql files? franksubramania Shell Programming and Scripting 1 05-04-2009 01:47 PM