LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-26-2008, 11:23 PM   #1
hocheetiong
Member
 
Registered: Jul 2007
Location: Penang , Malaysia.
Distribution: red hat linux
Posts: 133

Rep: Reputation: 15
Smile How to write a simple BASH script to "test if have folowing files, than delete."?


Hi, i need to write a BASH script to test if have folowing files(1.txt, 1.doc, 2.txt, 2.doc) then delete them.I have try to write:

#!/bin/bash
....
if [ -e 1.*,2.* ]; then
rm 1.* 2.*
fi
....

But got errors?
 
Old 09-26-2008, 11:41 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You could simply delete them. If they don't exist, you just get an error message which you can send to /dev/null.

if [ -f 1.txt ]; then rm 1.txt; fi

You could also use:
[ -f 1.txt ] && rm 1.txt

The command after && (and) is executed if the last command or test before it returned true.

There are many tests. Type in "help test" in the terminal. One thing you need to know. The [ character is actually a command. This is a posix requirement. It is a built in and a command in /usr/bin/[. Enter "which [" and "type [" to see for yourself. This means that you need a space after the [ character. Otherwise bash will look for a "[-f" command which does't exist.

The -e or -f tests take only one argument. If you want to delete these files only if they all exist, you could do:
[ -e 1.txt -a -e 2.txt -a -e 3.txt ] && rm 1.txt 2.txt 3.txt

If you want to delete any of these that exists then you can iterate over the files:
for file in {1..3}.txt; do
[ -f $file ] && rm $file
done

Last edited by jschiwal; 09-27-2008 at 09:24 AM. Reason: typo
 
Old 09-27-2008, 12:12 AM   #3
hocheetiong
Member
 
Registered: Jul 2007
Location: Penang , Malaysia.
Distribution: red hat linux
Posts: 133

Original Poster
Rep: Reputation: 15
Smile rm 2.txt > /dev/null ,if the file 2.txt is not exist, i dont want to show error msg,

Thank jschiwal reply my question.

I have write a script:

rm 2.txt > /dev/null

If i have this file "2.txt" than will not prompt out error message, but if dont have this file "2.txt" then prompt out error message: rm: remove regular empty file '2.cap' , I already put >/dev/null why still prompt out error message from screen?
 
Old 09-27-2008, 01:49 AM   #4
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
Can you just explain the question a bit more. Is it:
  • delete any of the files, 1.txt, 1.doc, 2.txt, 2.doc..., that exist
or
  • if, and only if, a complete set of files from 1.txt, 2.txt,...n.txt exist, delete all of that set (and similarly ?.doc)

I've got the impression that you can say that if n.txt exists, n.doc will always exist, but that may be me over-interpreting.

Also, are these the only files in the directory, so could you, theoretically, delete *.txt (or ?.txt) and would that be safe? (Presumably, however, you do want to do something to prevent your script being applied to a different directory where thse conditions may not apply.)

And, given that this part of your problem, at least, is easy enough to do in bash is there a good reason that you are doing it in basic?
 
Old 09-27-2008, 04:04 AM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Add the -f option to rm. The message prompt you are seeing is printed on STDERR (which has not been redirected).

Last edited by Mr. C.; 09-30-2008 at 11:26 AM.
 
Old 09-30-2008, 09:14 AM   #6
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
Quote:
rm 2.txt > /dev/null

If i have this file "2.txt" than will not prompt out error message, but if dont have this file "2.txt" then prompt out error message: rm: remove regular empty file '2.cap' , I already put >/dev/null why still prompt out error message from screen?
To redirect the error output you would need to write:
Quote:
rm 2.txt 2> /dev/null
The 2> represents stderr output redirection. I hope this makes it a little clearer what is happening. The detail is HERE.
 
Old 09-30-2008, 01:10 PM   #7
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

that's not enough. Additionally (look at the post from Mr.C.) you have to add the -f option. The stderr output you see is a prompt - rm wants to know, if you want to remove a file, for which you don't have write permission. You then have to confirm to remove the file. The -f (force) option prevents you from being asked.

Jan
 
Old 09-30-2008, 05:58 PM   #8
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
I don't think you actually need a script to do what you wanted to do, Consider this:
Code:
$ cd tmp
$ touch 1.txt 2.txt 3.txt
$ ls *.txt
1.txt  2.txt  3.txt
$ ls ?.txt
1.txt  2.txt  3.txt
$ ls [0-9].txt
1.txt  2.txt  3.txt
$ rm -f [0-9].txt
$ ls *.txt
ls: cannot access *.txt: No such file or directory
 
Old 10-02-2008, 07:31 AM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
There is one form that you might want to become familiar with. Suppose that you want to copy or move files in a script, from one directory to another, but don't want to overwrite any files. You can use the -i option and automatically supply an answer:
cp -i dir1/*.mp3 dir2/ < <(yes n)
 
Old 10-01-2009, 08:39 AM   #10
pedro.branco
LQ Newbie
 
Registered: Oct 2009
Posts: 1

Rep: Reputation: 0
Quote:
Originally Posted by hocheetiong View Post
Hi, i need to write a BASH script to test if have folowing files(1.txt, 1.doc, 2.txt, 2.doc) then delete them.I have try to write:

#!/bin/bash
....
if [ -e 1.*,2.* ]; then
rm 1.* 2.*
fi
....

But got errors?

What errors are you getting?

if [ -f $1 ]; then
echo "File exists"
else
echo "File not Found"
fi

Works..

Another way.. use test.
 
Old 10-01-2009, 12:17 PM   #11
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by pedro.branco View Post
. . .
Another way.. use test.
Just a F.Y.I.: On many systems, " [ " is an alias for "test," and " ] " is ignored. (Note that the blanks around the square brackets are part of the alias command name.) On almost all other systems, the " [ " and " ] " are hard-coded into bash, and "test" is provided a a separate program for those (very rare) occasions when "test" is needed from some non-bash program.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
DO anyone know "How to write a Test case for LDAP Single Master Replication???" gopiindian86 Linux - Server 1 08-16-2008 05:25 AM
Simple bash script "unexpected end of line error" snowman81 Programming 11 11-11-2007 09:31 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
Simple "service" script for dealing with rc files arobinson74 Slackware 0 09-06-2004 11:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration