remove files other than selected


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting remove files other than selected
# 1  
Old 11-23-2007
Error remove files other than selected

I wanna remove a set files other than some selected files.

Eg.

SmilieSmilieSmilie

a directory contains n files like

test1.dat
test2.dat
test3.dat
test4.dat
out5.dat
out1.dat

i wanna remove all files which doesnot name like *test*

I want to use this in shell script, so please make sure that the cmd is independent of the working directory... !!! Smilie

Thanks
# 2  
Old 11-23-2007
Code:
find path | while read N
do
    case `basename $N`
      dontDeleteMe | orMe | orMeEither )
          ;;
      * )
          rm "$N"
          ;;
    esac
done

# 3  
Old 11-23-2007
MySQL Got it.. !

got it..!!! SmilieSmilieSmilie


ls /tmp/ | grep -v test | xargs -i ksh -c 'rm /tmp/"{}" '



can anyone lemme know if it is fool proof..!! Smilie
# 4  
Old 11-23-2007
Quote:
Originally Posted by freakygs
can anyone lemme know if it is fool proof..!! Smilie
/tmp is a shared directory, other people could have all kinds of stuff in there.
# 5  
Old 11-23-2007
ksh:

Code:
rm !(*test*)

bash:

Code:
shopt extglob
rm !(*test*)

zsh (require setopt extendedglob):

Code:
rm  *~*test*

or:

Code:
rm ^*test*


But be careful Smilie
# 6  
Old 11-23-2007
Try this

rm out?

rm *out*
# 7  
Old 11-27-2007
Quote:
Originally Posted by porter
/tmp is a shared directory, other people could have all kinds of stuff in there.


well.... . m not bothered about the directory.... i mentioned it as example.... !! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

unzip selected files

Hi, In file zip folder i have many files but i want to extract onlu .LOG file from the zip. How can i achive this Rajesh (3 Replies)
Discussion started by: guddu_12
3 Replies

2. Shell Programming and Scripting

Shell to remove a newline char from selected rows in a file.

Greetings! Can we automate the process of removing a newline char from selected rows in a fixed width file using a shell? Input is like abcd1234 xyzd1234 abcd a1b2c3d4 abcd1234 xyzd1234 xx abcd1234 Expected output - abcd1234xyzd1234 abcda1b2c3d4abcd1234xyzd1234 xxabcd1234 ... (3 Replies)
Discussion started by: mailme0205
3 Replies

3. UNIX for Dummies Questions & Answers

Remove a newline char from selected rows.

Greetings! Can we automate the process of removing a newline char from selected rows in a fixed width file using a shell? Input is like abcd1234 xyzd1234 abcd a1b2c3d4 abcd1234 xyzd1234 xx abcd1234 Expected output - abcd1234xyzd1234 abcda1b2c3d4abcd1234xyzd1234 xxabcd1234 ... (2 Replies)
Discussion started by: mailme0205
2 Replies

4. Shell Programming and Scripting

Script to find and email selected files

I am trying to come up with a script that will search for selected files and then email them to me. For example, say I have a directory that has the following files: AA_doug.txt AA_andy.txt BB_john.txt APPLE_mike.txt GLOBE_ed.txt GLOBE_tony.txt TOTAL_carl.txt what is the best way to... (2 Replies)
Discussion started by: coach5779
2 Replies

5. Shell Programming and Scripting

compare two files, selected columns only

hi! i have two files that looks like this file 1: ABS 123 456 BCDG 124 542 FGD 459 762 file 2: ABS 132 456 FGD 459 762 output would be: from file1: ABS 132 456 BCDG 124 542 from file 2: ABS 132 456 (4 Replies)
Discussion started by: kingpeejay
4 Replies

6. Shell Programming and Scripting

gzip selected files that do not contain the string...

Solaris 10 Hi All, Can anyone give me a simple way of gzipping select files that end with *.dmp and where the filename does not contain the string "nocompress" I had a go but failed miserably :-( Thanks in advance Satnam (3 Replies)
Discussion started by: satnamx
3 Replies

7. Shell Programming and Scripting

trying to print selected fields of selected lines by AWK

I am trying to print 1st, 2nd, 13th and 14th fields of a file of line numbers from 29 to 10029. I dont know how to put this in one code. Currently I am removing the selected lines by awk 'NR==29,NR==10029' File1 > File2 and then doing awk '{print $1, $2, $13, $14}' File2 > File3 Can... (3 Replies)
Discussion started by: ananyob
3 Replies

8. Shell Programming and Scripting

concatenating selected lines of multiple files

Hi, I would like a shell script that reads all files in a directory and concatenate them. It is not a simple concatenation. The first few lines of the files should not be included. The lines to be included are the lines from where 'START HERE' appears up to the end of the file. For example, I... (4 Replies)
Discussion started by: laiko
4 Replies

9. Linux

Unzip selected files in .tar.gz file

Hi All, By accident, i deleted some files. Fortunately I have a backup backup.tar.gz files (750GB). It's too big for me to untar to get the file Is it possible that i could get the selected files in backup.tar.gz if i know exactly where the files are located. Thanks. Ken (1 Reply)
Discussion started by: trongkhuongsg
1 Replies

10. UNIX for Dummies Questions & Answers

ftp selected jpeg files from unix filesystem

Hi All We have hp-ux 11iv1 system running with oracle8i database. We have around 350,000 users, each user uploaded their own signatures and are stored in unix filesystems department wise. A database is maintained to keep their particulars with a path to link their signature files. Now... (3 Replies)
Discussion started by: mhbd
3 Replies
Login or Register to Ask a Question