removing mutiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting removing mutiple files
# 1  
Old 05-17-2005
removing mutiple files

I have a script which removes files (if they exist)
Here is a cut down example of the script.
Variables file1,file2 etc have already been initialized

#!/bin/bash
if [ -f $file1 ]
then
\rm file1
fi
if [ -f $file2 ]
then
\rm file2
fi
if [ -f $file3 ]
then
\rm file3
fi
if [ -f $file4 ]
then
\rm file4
fi
if [ -f $file5 ]
then
\rm file5
fi
if [ -f $file6 ]
then
\rm file6
fi

.........and so on

Is there a simpler way to do this??
# 2  
Old 05-17-2005
How about this ?

Code:
for list in file1, file2, file3
if [ -f $list ] ; then
rm -f $list
fi;

Havnt tested it though.

Vino
# 3  
Old 05-17-2005
Code:
for list in $file1 $file2 $file3; do
[ -f $list ] && \rm $list 
done

# 4  
Old 05-18-2005
Reborg,

In you post,

why the $list1 , $list2 in

Code:
for list in $file1 $file2 $file3; do

and not

Code:
for list in file1 file2 file3; do

Isnt $list supposed to have whatever file1 holds and so on and so forth ?
# 5  
Old 05-21-2005
My variation on all this would be....
Code:
$ cat > rmlist
file1
file2
file3
file4
file5
file6
^D
$ while read filename; do
>   [ -f $filename ] && \rm -f $filename
> done < rmlist
$

while loops process faster than for loops...

Cheers
ZB
# 6  
Old 05-21-2005
Quote:
Originally Posted by vino
Reborg,

In you post,

why the $list1 , $list2 in

Code:
for list in $file1 $file2 $file3; do

and not

Code:
for list in file1 file2 file3; do

Isnt $list supposed to have whatever file1 holds and so on and so forth ?
Because I read his first post which said he already had initialized the filenames elsewhere in his script, from his post I had no way of knowing if they were used for other things previous to being removed and thus though it better to use his preexsisting variables.
# 7  
Old 05-26-2005
here's my version ...

skip the file exists check and just throw the file not found error to /dev/null ...
Code:
rm -f $file1 $file2 $file3 $file4 ... 2> /dev/null

or ... if you have the file list like what zazzy has ...
Code:
rm -f `< rmlist` 2> /dev/null

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split large xml into mutiple files and with header and footer in file

Split large xml into mutiple files and with header and footer in file tried below it splits unevenly and also i need help in adding header and footer command : csplit -s -k -f my_XML_split.xml extrfile.xml "/<Document>/" {1} sample xml <?xml version="1.0" encoding="UTF-8"?><Recipient>... (36 Replies)
Discussion started by: karthik
36 Replies

2. UNIX for Beginners Questions & Answers

Find and removing the old files and zipping the files using shell script

Hi, I am trying to removing the old files which were older than 10 days and same g zipping the files using the shell script. script was return as follows. find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f | xargs rm -f find /cer_skyliv??/log... (6 Replies)
Discussion started by: venkat918
6 Replies

3. Shell Programming and Scripting

paste mutiple files in a loop

file1.txt file2.txt file3.txt desired output is each file is in the same directory, hasthe same number of columns but different rows. i want to be able to paste them into one file. thanks! (5 Replies)
Discussion started by: johnkim0806
5 Replies

4. Shell Programming and Scripting

how to replace words in mutiple files under the same directory

I would like to get help to find how to replace word in files from command line instead of to vi to each file. This is the command i am running now. grep <old word> * vi (file with the word found in it) 1,$s/<old word>/<new word>/g It would very helpful if I can combine these in one... (2 Replies)
Discussion started by: ywu081006
2 Replies

5. UNIX for Advanced & Expert Users

Remove first line from mutiple files

How to remove the first line from multiple files and use it as source to the jobs. Only at the runtime it should remove the first line not in the file . (1 Reply)
Discussion started by: etldeveloper
1 Replies

6. Shell Programming and Scripting

Renaming mutiple files with hyphens in name

I have searched throught a host of threads to figure out how to rename mutiple files at once using a script. I need to convert 200+ files from: fKITLS_120605-0002-00001-000001.hdr to eStroop_001.hdr fKITLS_120605-0002-00002-000002.hdr to eStroop_002.hdr and so forth.... What is... (5 Replies)
Discussion started by: akenne3
5 Replies

7. UNIX for Dummies Questions & Answers

copy mutilple files to mutiple folders

Hi, I just started to learn shell progamming and just can't get my head around the following problem. I need to do the following: I have a folder which contains 100+ subfolders. Inside these subfolders there is one folder named 'Morph' and several jpg's. I need to copy all the files into... (4 Replies)
Discussion started by: M474746
4 Replies

8. Shell Programming and Scripting

Mutiple For loops - moving files to another directory

I need to clean out some application subdirectories from backup scripts we used to rename to various backup extensions just in case the script failed in production and we need to rollback. I will be moving these old scripts to a staging directory and then removing them after 30 days (I have the... (9 Replies)
Discussion started by: tekster757
9 Replies

9. Shell Programming and Scripting

Sending mutiple files thru email to lotus notes

Hi All, I am currently using the following script to send the single file to one/more email addresses. I need to send mutilple files at same time, are there anyway I could modify the script or write new one to accomplish the same. Script *************** #!/bin/ksh # Author: Manish... (4 Replies)
Discussion started by: lapisguy
4 Replies

10. Shell Programming and Scripting

Report with mutiple files.

Hi all, In the process of creating CPU reports. I've already used `sar` to create a daily file, then monthly reports for CPU usage (which is averaged across all 4 CPU's). I've now used `cpusar -P ?`(?=CPU#) to collect individual CPU data and have four files for each day which is great. The... (4 Replies)
Discussion started by: Cameron
4 Replies
Login or Register to Ask a Question