Deleting files in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting files in awk
# 1  
Old 11-26-2009
Deleting files in awk

Hi All,

I have about 10000 files having names of the same pattern

I am using awk to match the pattern.

Inside the awk block how can i delete all the 10000 files..


Or is there any other way to delete files in bulk...


"rm" command fails with the message that argument list is too large
# 2  
Old 11-26-2009
You can use the find

Ex:

Code:
find dir_name -type f -exec rm -f {} \;

Awk doesn't haev any mechanism to delete file/files. However, you can make use of system("command").
# 3  
Old 11-26-2009
will this be able to delete 10000 files....

---------- Post updated at 05:40 AM ---------- Previous update was at 05:37 AM ----------

where is filename coming in the command that you have written
# 4  
Old 11-26-2009
in awk itself, you can use system(), assuming no spaces in files
Code:
awk '{cmd="rm " FILENAME; print cmd; system(cmd) }' file*

# 5  
Old 11-26-2009
Hi Abhinav

#!/sbin/sh
###################################################
# Write This Shell Script and run it. Edit As u Wish.
###################################################

# Give ur dirctory location
DIRLOC=
export DIRLOC

# Give ur file Pattern
# This is one eg.

FILEEXT=DB.*.log;
export FILEEXT

find $DIRLOC -name "$FILEEXT" -exec /bin/rm {} \;

echo "Deletion Completed"

#
#
##################################################
# 6  
Old 11-26-2009
Quote:
Originally Posted by vikas898
#!/sbin/sh
###################################################
# Write This Shell Script and run it. Edit As u Wish.
###################################################

# Give ur dirctory location
DIRLOC=
export DIRLOC

# Give ur file Pattern
# This is one eg.

FILEEXT=DB.*.log;
export FILEEXT

find $DIRLOC -name "$FILEEXT" -exec /bin/rm {} \;

echo "Deletion Completed"

#
#
##################################################
This is a very dangerous script because it makes no attempt to limit the find to the target directory, hence it will cherrfully delete it's way down all subdirectories.Another problem is that when using -exec it will launch rm as a new process every time it deletes a file. Better to write code that passes a list of file names to rm vi xargs.Better still is to write a function that explicitly tests - ownership, presence, writability and so on. You could also pass it parameters such as recursive = yes/no etc
# 7  
Old 11-27-2009
Thanks "steadyonabix"

Thanks Dude, Actually I am new in shell scripting and in UNIX environment.

If u possible plz give some solution, to overcome this problem.

Thanks,

---------- Post updated at 11:17 AM ---------- Previous update was at 11:11 AM ----------

#Suppose I have two different directory ...
#with some pattern logs as FILEEXT
#eg. as below

DIRLOC=/opt/logs/db/logs;
export DIRLOC
FILEEXT=DB.*.log;
export FILEEXT

DIR1LOC=/opt/logs/csv/logs;
export DIR1LOC
FILE1EXT=csv.*.log;
export FILE1EXT


find $DIRLOC -name "$FILEEXT" -exec /bin/rm {} \;
find $DIR1LOC -name "$FILE1EXT" -exec /bin/rm {} \;


# so how can i write code which passes a list of file names to rm vi xargs.

# Plz Help !!! :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mainain the files after deleting lines with awk

hello I want to delete the lines of many files (e.g. file1, file2, ..., file1111) in my directory (e.g. dir1) that they have the 2nd column grater than a value (e.g. 40) placing them to a different directory (e.g. newdir) and keeping the originals. I wrote the code which is functional awk '{... (3 Replies)
Discussion started by: phaethon
3 Replies

2. Shell Programming and Scripting

Bash script deleting my files, and editing files in subdirectories question

#!/bin/bash # name=$1 type=$2 number=1 for file in ./** do if then filenumber=00$number elif then filenumber=0$number fi tempname="$name""$filenumber"."$type" if (4 Replies)
Discussion started by: TheGreatGizmo
4 Replies

3. Shell Programming and Scripting

How to use awk for deleting columns?

Hi folks, How awk 'll help to do this file contains bunch of insert statement like below remove fourth column from all statement put it in new file INSERT INTO `tbl_medicalquestions` VALUES (1,'Is anyone waiting for an operation, post operative check up, any other hospital treatment or... (12 Replies)
Discussion started by: ashutoshverma
12 Replies

4. Shell Programming and Scripting

Deleting column using awk

How Can we delete a range of coloumns using awk? (or any other method is fine) If we have a file which has about 200 coloumns. I need to delete a particular range lets say for eg from $6 to $119 Can we do this using cut, if yes the cut command would also be helpful. many thanks in... (4 Replies)
Discussion started by: Sri3001
4 Replies

5. Shell Programming and Scripting

AIX system.... deleting files in remote directory after retrieving files

Hi Friends, I am new to this , I am working on AIX system and my scenario is to retrive the files from remote system and remove the files from the remote system after retreving files. I can able to retrieve the files but Can't remove files in remote system. Please check my code and help me out... (3 Replies)
Discussion started by: vinayparakala
3 Replies

6. Shell Programming and Scripting

Need help comparing two files and deleting some things in those files!

So I have two files: File1 pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2 pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2 ref4948 1.1 treehouse.txt 1.6 ref8573 1.5 ref3284 1.4 ref5838... (24 Replies)
Discussion started by: linuxkid
24 Replies

7. Shell Programming and Scripting

Deleting keys and values-Awk

key pair is 1st and 6th column ex:a20 : p10 or a20 : p11 For every key pair if the vlaue(4th column) is the same then delete all the lines who has keypair and the value ex: a20 : p10 has value 1 only then delete those but a20 : p11 has different values 1,2 and 3 and keep those. input a20 ... (8 Replies)
Discussion started by: ruby_sgp
8 Replies

8. Shell Programming and Scripting

Deleting every 3rd field using awk

I have a file whose format is like the following 350,2,16.2,195,2,8.0 every 3rd column of this file should be deleted. How can i achieve this tried with the following iostat -D -l 2 | /usr/xpg4/bin/awk ' NR>2 { for (i=0;i<=NF;i++)if(i%3==0)$i=""};' but no luck (3 Replies)
Discussion started by: achak01
3 Replies

9. Shell Programming and Scripting

printing and deleting using awk

I am trying to print and delete at the same time 0KB files... using following command a-> find . -type f | xargs ls -l | awk '{ if($5 == 0) {print $0;}}' | xargs rm $0 but am not successful. Can somebody tell me how to do this ... same time I need files to be printed as well deleted later. ... (4 Replies)
Discussion started by: vivek.gkp
4 Replies

10. Shell Programming and Scripting

deleting lines using awk-best way?

Hi all....I'm using awk to validate a csv file, but now I've been told to delete any invalid lines from the file.. Im not sure what the best way to do this is? Would it be to create a temp file say "csv_temp.tmp" file and print all the valid records to that temp file. Then delete the old file... (3 Replies)
Discussion started by: satnamx
3 Replies
Login or Register to Ask a Question