Right method for removing a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Right method for removing a file
# 1  
Old 07-02-2013
Right method for removing a file

Code:
if [  -f /52/bip_log_1.txt ]
then
  `rm /52/bip_log_1.txt`
  echo "file bip_eg.txt removed"
fi

I am using above code to remove a temorary log file

Code:
if [  -e /52/bip_log_1.txt ]
then
  `rm /52/bip_log_1.txt`
  echo "file bip_eg.txt removed"
fi

The file - e is showing error. WHY?
# 2  
Old 07-02-2013
Difference between -f and -e option:

[ -f FILE ] expression is true if file exists and is a regular file.

[ -e FILE ] expression is true if file exists.

By the way this backtick is not required:
Code:
`rm /52/bip_log_1.txt`

Quote:
Originally Posted by rafa_fed2
The file - e is showing error. WHY?
You didn't tell us what error you are getting?
# 3  
Old 07-02-2013
Quote:
Originally Posted by Yoda
Difference between -f and -e option:

[ -f FILE ] expression is true if file exists and is a regular file.

[ -e FILE ] expression is true if file exists.

By the way this backtick is not required:
Code:
`rm /52/bip_log_1.txt`

You didn't tell us what error you are getting?


Hi yoda,,
i am getting aruments needed error..
backtick is working fine.so no issues in that i guess.i think backtick is optional
# 4  
Old 07-02-2013
Quote:
Originally Posted by rafa_fed2
i am getting aruments needed error..
What system are you on and what shell are you using?
Code:
uname -a 
echo $SHELL

# 5  
Old 07-02-2013
Quote:
Originally Posted by Yoda
What system are you on and what shell are you using?
Code:
uname -a 
echo $SHELL

Code:
SunOS abcde 5.10 Generic_120011-14 sun4u sparc SUNW,Sun-Fire-480R

/usr/bin/ksh

---------- Post updated at 07:20 PM ---------- Previous update was at 07:18 PM ----------

Quote:
Originally Posted by rafa_fed2
Code:
SunOS abcde 5.10 Generic_120011-14 sun4u sparc SUNW,Sun-Fire-480R

/usr/bin/ksh

Is my method for checking and removing the file correct.I want to know that
# 6  
Old 07-02-2013
Quote:
Originally Posted by rafa_fed2
Is my method for checking and removing the file correct.I want to know that
Your method is correct and it should work.
Code:
#!/usr/bin/ksh

if [ -e /home/yoda/file ]
then
        print "File Exists"
        print "Removing file"
        rm -f /home/yoda/file
fi

Code:
$ ./script.ksh
File Exists
Removing file

Code:
$ uname -a
SunOS xxxxx 5.10 Generic_147440-22 sun4v sparc sun4v

This User Gave Thanks to Yoda For This Post:
# 7  
Old 07-02-2013
Quote:
Originally Posted by Yoda
Your method is correct and it should work.
Code:
#!/usr/bin/ksh

if [ -e /home/yoda/file ]
then
        print "File Exists"
        print "Removing file"
        rm -f /home/yoda/file
fi

Code:
$ ./script.ksh
File Exists
Removing file

Code:
$ uname -a
SunOS xxxxx 5.10 Generic_147440-22 sun4v sparc sun4v


-f is working and i am using it.Is this correct?
when i use -e it does not work
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

File Management: Removing of files from Server2 IF the same file is removed from Server1.

Hi Folks, I have a requirement of file management on different servers. Source Server is SERVER-A. Two servers will fetch files from SERVER-A: SERVER1 and SERVER2. 4th SERVER is SERVER-B, It will fetch files from SERVER1. If SERVER1 goes DOWN, SERVER-B will fetch pending files from... (2 Replies)
Discussion started by: Raza Ali
2 Replies

2. Red Hat

Different method to transfer file to windows

Hi, What are the methods to pull out a file with size 200 mb from Linux to windows server? I tried using Winscp but didn't work out, it was hanging in the middle with message below. The file does exist in the below directory ... (8 Replies)
Discussion started by: Maddy123
8 Replies

3. Shell Programming and Scripting

Need an automated pre-defined file pickup method

Hi Gurus, I need to develop a script which picks the files in a pre-defined order. The files from monday-friday will be named as abc_01_20130923 as monday's file and abc_02_20130924 as tuesday's..so..so forth till friday's which will be named as abc_05_20130927.It repeats over for the... (3 Replies)
Discussion started by: vikramgk9
3 Replies

4. Shell Programming and Scripting

Efficient method of determining if a string is in a file.

Hi, I was hoping someone could suggest an alternative to code I currently have as mine takes up far too much processor time and it to slow. The situation: I have a programme that runs on some files just before they are zipped up and archived, the program appends a one line summary of the... (4 Replies)
Discussion started by: RECrerar
4 Replies

5. Shell Programming and Scripting

Grabbing the newest file, cleaner method?

Greetings, I'm doing a process whereby I need to search for all filenames containing a given bit of text and grab the newest file from what may be 20 results. In a script I'm writing, i've got a monster line to do the sort as follows: find /opt/work/reports/input -name "*$searchtarget*" |... (4 Replies)
Discussion started by: Karunamon
4 Replies

6. Solaris

svc:/network/physical:default: Method "/lib/svc/method/net-physical" failed with exit status 96. [ n

After a memory upgrade all network interfaces are misconfigued. How do i resolve this issue. Below are some out puts.thanks. ifconfig: plumb: SIOCLIFADDIF: eg000g0:2: no such interface # ifconfig eg1000g0:2 plumb ifconfig: plumb: SIOCLIFADDIF: eg1000g0:2: no such interface # ifconfig... (2 Replies)
Discussion started by: andersonedouard
2 Replies

7. UNIX for Dummies Questions & Answers

Best method for inserting a field from a file into another file?

Hello, Hoping someone can help! I have a file (fileA) with a potentially different number of | delimited fields the file looks like: fileA A|B|C|D|E|F|G| A|B|C|D|E|F|G| This file could have 100+ fields and have 2million+ records I have another file (fileB) which contains an 8 digit... (3 Replies)
Discussion started by: dendright
3 Replies

8. Shell Programming and Scripting

File transformation - what is most efficient method

I've done quite a bit of searching on this but cannot seem to find exactly what I'm looking for. Say I have a | delimited input file with 6 columns and I need to change the value of a few columns and create an output file. With my limited knowledge I can do this with many lines of code but want... (5 Replies)
Discussion started by: 1superdork
5 Replies

9. UNIX for Dummies Questions & Answers

Shadow file encryption method

Hi all, I'd like to use the encryption method used to generate the /etc/shadow passwords. The goal is to write a script that get a plain-text password as argument and returns an encrycped one. Can you help me, please? (10 Replies)
Discussion started by: nisant
10 Replies

10. Shell Programming and Scripting

Removing lines from large files.. quickest method?

Hi I have some files that contain be anything up to 100k lines - eg. file100k I have another file called file5k and I need to produce filec which will contain everything in file100k minus what matches in file 5k.. ie. File100k contains 1FP 2FP 3FP File5k contains 2FP I would... (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question