Home Man
Search
Today's Posts
Register

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

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 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.
The Following User Says Thank You to MadeInGermany For This Useful 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
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script for field wise record count for different Files .csv files Kirands Shell Programming and Scripting 14 01-21-2016 04:06 PM
Unzip all the files with subdirectories present and append a part of string from the main .zip files Ravi Kishore Shell Programming and Scripting 0 04-22-2015 05:07 AM
Find all files in the current directory excluding hidden files and directories ksailesh1 UNIX for Advanced & Expert Users 7 02-03-2014 05:09 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 07:14 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
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
How to retrieve all the linked script files/ctl files/sql files? franksubramania Shell Programming and Scripting 1 05-04-2009 01:47 PM


All times are GMT -4. The time now is 02:39 PM.

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