Delete all files except a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete all files except a pattern
# 1  
Old 02-07-2012
Delete all files except a pattern

Hello,

Have a folder with lots of files & directories.
Want to delete all stuff except a particular pattern say "abc*".
Pattern will be in base directory & descending the structure is not required.

Can easily do it with looping or "grep -v", but wondering if there is an elegant solution.
# 2  
Old 02-07-2012
Try this:

Code:
 
# ls -ltrR
.:
total 24
-rw-r--r--  1 root root    0 Feb  7 13:51 bin.sh
drwxr-xr-x  2 root root 4096 Feb  7 14:10 bneo
-rw-r--r--  1 root root    0 Feb  7 14:15 script.txt
-rw-r--r--  1 root root    0 Feb  7 14:15 neo
-rw-r--r--  1 root root    0 Feb  7 14:15 eon.sh
./bneo:
total 8
-rw-r--r--  1 root root 0 Feb  7 14:10 neo.sh
-rw-r--r--  1 root root 0 Feb  7 14:10 bin.sh

# find . -maxdepth 1 ! \( -name "bin*" -o -name '.' -o -name '..' \) -print
./neo
./script.txt
./eon.sh
./bneo

Instead of print, if you will pass
Code:
-exec rm {} \;

; it will delete the files in the present directory. Mind it that since I have not passed rm -rf, the directory will not be deleted.

So if you want to delete everything (non matching files n folders) , do this:
Code:
 
# find . -maxdepth 1 ! \( -name "bin*" -o -name '.' -o -name '..' \) -exec rm -rf {} \;
# ls -ltr
total 4
-rw-r--r--  1 root root 0 Feb  7 13:51 bin.sh

# 3  
Old 02-07-2012
Checkout extended globbing in ksh93/bash
Code:
ls !(abc*)

In bash first execute the following command if extended globbing is not on by default
Code:
shopt -s extglob

You can check if it's on with the command shopt

Last edited by Scrutinizer; 02-07-2012 at 05:50 AM..
# 4  
Old 02-07-2012
Thanks

I may be mistaken, but believe that "find" may take subsequent time in making the list & passing it off to the -exec call. Though we have given the maxdepth option.

Directories what i plan to cleanup belong in range of 300GB.

Do correct if mistaken.

---------- Post updated at 03:24 PM ---------- Previous update was at 03:23 PM ----------

Quote:
Originally Posted by Scrutinizer
Checkout extended globbing in ksh93/bash
Code:
ls !(abc*)

In bash first execute the following command if extended globbing is not on by default
Code:
shopt -s extglob

You can check if it's on with the command shopt
Yup, I tried that & was pretty effective.
Limitation being it works with only a single pattern.

Is there a way to make it work for more than 1 pattern?
# 5  
Old 02-07-2012
try:
Code:
ls !(abc*|xyz*)

# 6  
Old 02-08-2012
Thanks that works.

Thoughts on which will be the better approach.
Find or the shopt.

My vote is to shopt Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete all lines before a particular pattern when the pattern is defined in a variable?

I have a file Line 1 a Line 22 Line 33 Line 1 b Line 22 Line 1 c Line 4 Line 5 I want to delete all lines before last occurrence of a line which contains something which is defined in a variable. Say a variable var contains 'Line 1', then I need the following in the output. ... (21 Replies)
Discussion started by: Soham
21 Replies

2. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

3. Shell Programming and Scripting

If first pattern is found, look for second pattern. If second pattern not found, delete line

I had a spot of trouble coming up with a title, hopefully you'll understand once you read my problem... :) I have the output of an ldapsearch that looks like this: dn: cn=sam,ou=company,o=com uidNumber: 7174 gidNumber: 49563 homeDirectory: /home/sam loginshell: /bin/bash uid: sam... (2 Replies)
Discussion started by: samgoober
2 Replies

4. Shell Programming and Scripting

sed pattern to delete lines containing a pattern, except the first occurance

Hello sed gurus. I am using ksh on Sun and have a file created by concatenating several other files. All files contain header rows. I just need to keep the first occurrence and remove all other header rows. header for file 1111 2222 3333 header for file 1111 2222 3333 header for file... (8 Replies)
Discussion started by: gary_w
8 Replies

5. Shell Programming and Scripting

Delete files based on specific MMDDYYYY pattern in filename

Hi Unix gurus, I am trying to remove the filenames based on MMDDYYYY in the physical name as such so that the directory always has the recent 3 files based on MMDDYYYY. "HHMM" is just dummy in this case. You wont have two files with different HHMM on the same day. For example in a... (4 Replies)
Discussion started by: shankar1dada
4 Replies

6. Shell Programming and Scripting

Help!! Script to delete files other than specified pattern

Hi Experts, I want to delete all the files in a folder other than files with .zip extension using shell script. please suggest the code. Neeraj Chourasiya (3 Replies)
Discussion started by: nchourasiya
3 Replies

7. Shell Programming and Scripting

Find required files by pattern in xml files and the change the pattern on Linux

Hello, I need to find all *.xml files that matched by pattern on Linux. I need to have written the file name on the screen and then change the pattern in the file just was found. For instance. I can start the script with arguments for keyword and for value, i.e script.sh keyword... (1 Reply)
Discussion started by: yart
1 Replies

8. UNIX for Dummies Questions & Answers

copying a pattern of files in one directory into other with new pattern names...

Hi, I have to copy a set of files abc* in /path/ to /path1/ as abc*_bkp. The list of files appear as follows in /path/: abc1 xyszd abc2 re2345 abcx .. . abcxyz I have to copy them (abc* files only) into /path1/ as: abc1_bkp abc2_bkp abcx_bkp .. . (6 Replies)
Discussion started by: new_learner
6 Replies

9. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies

10. UNIX for Dummies Questions & Answers

delete pattern files in sub directories

Hello friends, I am compiling some set of SQL scripts in a set of sub directories demoed as below. After compiling log files are being created. Each and every time after compiling time I had to go subdir by subdir to delete the log files. I am sure there should be simple way to look for all log... (4 Replies)
Discussion started by: adurga
4 Replies
Login or Register to Ask a Question