Deleting the file except one type of file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Deleting the file except one type of file
# 1  
Old 12-18-2008
Deleting the file except one type of file

I need the unix command which is used to delete the file except one the of file format.

example:
In folder
a.txt
b.txt
c.prn
d.st
g.lg

Output :

except .st files remaining all are deleted from the folder.
# 2  
Old 12-18-2008
Code:
ls -1| grep -v \.st$| xargs -I {} rm -f {}

# 3  
Old 12-18-2008
i got the below error while give ur command

kganeshb@its04489:~/rm $ ls -1| grep -v \.st$| xargs -1 {} rm -f {}
xargs: invalid option -- 1
Usage: xargs [-0prtx] [-e[eof-str]] [-i[replace-str]] [-l[max-lines]]
[-n max-args] [-s max-chars] [-P max-procs] [--null] [--eof[=eof-str]]
[--replace[=replace-str]] [--max-lines[=max-lines]] [--interactive]
[--max-chars=max-chars] [--verbose] [--exit] [--max-procs=max-procs]
[--max-args=max-args] [--no-run-if-empty] [--version] [--help]
[command [initial-arguments]]
# 4  
Old 12-18-2008
Try letting "-I {}" out of it. If it still doesn't work, try
Code:
man xargs

# 5  
Old 12-18-2008
Thank it is working after i removing the -I {}
# 6  
Old 12-18-2008
Hi Zaxxon,
I executed the following command

ls -l|grep '\.txt$'|xargs -I {} rm -f {}

but it shows error like following

rm: illegal option -- w
rm: illegal option -- -
rm: illegal option -- -
rm: illegal option -- -
rm: illegal option -- -
rm: illegal option -- -
rm: illegal option -- -
rm: illegal option -- -
rm: illegal option --
rm: illegal option --
rm: illegal option --
rm: illegal option -- 1
rm: illegal option --
rm: illegal option -- b
rm: illegal option -- a
rm: illegal option -- s
rm: illegal option -- u
rm: illegal option -- a
rm: illegal option --
rm: illegal option --
rm: illegal option --
.
.


Thanks.
# 7  
Old 12-18-2008
You copied it incorrectly. It should be

Code:
ls -1 (as in the number one), not ls -l (letter l)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Deleting file basing on the timestamp substring in the file name

Hello, I have in my backup folder, files with names convention like this : randomFileNames_13-02-2014_23h13m09+1392333189 randomFileNames_14-02-2014_02h13m09+1392343989 randomFileNames_14-02-2014_04h13m09+1392351189 etc.... Base on timestamp at end of the filename, I would to delete all the... (7 Replies)
Discussion started by: thuyetti
7 Replies

2. Shell Programming and Scripting

Shell script to read file and check file type

Hi, I have a file with few values in it. I need script help to read file line by line and check: 1/if it's a file (with extension eg .java .css .jar etc ) or 2/if it's a file without extension and treat it as a directory and then check if the directory exists in working copy else create one... (6 Replies)
Discussion started by: iaav
6 Replies

3. Shell Programming and Scripting

[solved] File type error (not a regular file)

Hi friend, i have written script as below to check the file existance. but i got error path="/k/p1100/users/jewel/Output" FILENAME=`ls -lrt $path/*HT|tail -1|cut -d "/" -f 8` if ; then echo "$FILENAME is available " chmod 755 $path/$FILENAME /usr/bin/scp... (0 Replies)
Discussion started by: Jewel
0 Replies

4. Shell Programming and Scripting

awk - writing matching pattern to a new file and deleting it from the current file

Hello , I have comma delimited file with over 20 fileds that i need to do some validations on. I have to check if certain fields are null and then write the line containing the null field into a new file and then delete the line from the current file. Can someone tell me how i could go... (2 Replies)
Discussion started by: goddevil
2 Replies

5. Shell Programming and Scripting

Deleting all files recursively from directories while ignoring one file type

Hi, Seems like I need help again with a problem: I want to delete all files from my lets say "Music" Directory inkluding all of the subfolders except for .mp3 and .MP3 files. I tried it with globalignoring mp3 files, finding and deleting all other files, which resulted in all files... (3 Replies)
Discussion started by: pasc
3 Replies

6. Shell Programming and Scripting

Deleting a type of file in a directory immediately it occurs

Hi All, I have to maintain few servers and in those servers in a specific directory some special jobs keeps on running ....... The directory is having many kind of files like.....acd*, hud*, rca*....bla bla bla....... My aim is that if due to any job-any time a file having initials like... (4 Replies)
Discussion started by: jitendra.pat04
4 Replies

7. UNIX for Advanced & Expert Users

Deleting older files of a particular type

hi This should be easy but i'm obviously missing something obvious. :) I'm looking to delete files from yesterday and older of extension .txt and there a range of subfolders with these files in them. The command runs but doesn't delete anything. SUSE 10. find /testfolder -maxdepth 2 -type f... (6 Replies)
Discussion started by: cmap
6 Replies

8. UNIX for Advanced & Expert Users

File System Type From File Path?

I'm writing a gui for the core utility shred. I want to be able to warn the user if they are about to shred a file that is on a journaled file system. In order to do this, I must learn the file system type of the path they are about to shred. Is there a way in Unix to: 1. query a specific... (3 Replies)
Discussion started by: codecellar
3 Replies

9. Shell Programming and Scripting

Deleting lines inside a file without opening the file

Hi, Just consider there are around 10 lines in a file. Now is it possible to delete the first 2 lines in the file without opening the file. No matter whatever the content of the file is, I just wanna delete the first 2 lines without opening the file. Is that possible? If so, please help me out.... (3 Replies)
Discussion started by: toms
3 Replies

10. UNIX for Dummies Questions & Answers

deleting files by type (symbolic links)

How do I delete just the symbolic links in a directory? I have files that I wish to keep that have similar names, length and date/time. Can I use file size? Thanks kyle (4 Replies)
Discussion started by: kryan_toolboy
4 Replies
Login or Register to Ask a Question