How to write a simple BASH script to "test if have folowing files, than delete."?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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?
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?
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.
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.
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)
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.