LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash Scripts if files (plural) exist (https://www.linuxquestions.org/questions/linux-newbie-8/bash-scripts-if-files-plural-exist-801790/)

Drigo 04-13-2010 12:57 PM

Bash Scripts if files (plural) exist
 
So, I've been looking at many forums and I dont find an answer for this. I want to check if some files starting lets say with wx exist or not and use a conditional to check.

For example, having in a folder...

wx123.tar
wx003.tar
wx323.tar
wx523.tar
df321.tar
df542.tar
cd123.tar
vf213.tar

I want to check if there exists files starting with x and move then to another directory in bash scrtipt.

So..I tried:

.
.
.
if [ -f "wx* ] then
...
fi


but the * sign doesnt seem to work. Any suggestion or help?

Thanks!!

modprob 04-13-2010 01:48 PM

I suggest you read the man page of find. Particularly the use of the exec option.

catkin 04-13-2010 02:09 PM

Code:

shopt -s nullglob
for file in wx*
do
    <whatever>
done


pixellany 04-13-2010 03:30 PM

I removed "PLEASE HELP" from the title---Since you posted the question, we know you want help.

wx* expands to a string including everything in the directory that starts with "wx". That is not what test ([) wants to see. This is why you need a loop.

If there is only one file starting with wx, then it works.

cola 04-24-2010 09:05 AM

Quote:

Originally Posted by catkin (Post 3934387)
Code:

shopt -s nullglob
for file in wx*
do
    <whatever>
done


Why is shopt command for?

cola 04-24-2010 09:07 AM

Quote:

Originally Posted by Drigo (Post 3934299)
So, I've been looking at many forums and I dont find an answer for this. I want to check if some files starting lets say with wx exist or not and use a conditional to check.

For example, having in a folder...

wx123.tar
wx003.tar
wx323.tar
wx523.tar
df321.tar
df542.tar
cd123.tar
vf213.tar

I want to check if there exists files starting with x and move then to another directory in bash scrtipt.

So..I tried:

.
.
.
if [ -f "wx* ] then
...
fi


but the * sign doesnt seem to work. Any suggestion or help?

Thanks!!

You need to use loop statements.See catkin's post.

catkin 04-24-2010 09:36 AM

Quote:

Originally Posted by cola (Post 3946292)
Why is shopt command for?

In case there are no files matching wx*. Without the shopt, wx* would expand to exactly that, wx*. With the shopt it expands to null so the loop is not executed. That's the intention, anyway :)

MTK358 04-24-2010 10:07 AM

Code:

if ls wx* &> /dev/null
then
    # do stuff
fi

ls will fail and return a nonzero value if the file does not exist. the "&> /dev/null" keeps ls's output from being printed.

catkin 04-24-2010 10:33 AM

Quote:

Originally Posted by MTK358 (Post 3946347)
Code:

if ls wx* &> /dev/null
then
    # do stuff
fi

ls will fail and return a nonzero value if the file does not exist. the "&> /dev/null" keeps ls's output from being printed.

Unless shopt -s nullglob is in effect :)

BTW I think the & in &> is superfluous ... ?

MTK358 04-24-2010 11:19 AM

Quote:

Originally Posted by catkin (Post 3946367)
BTW I think the & in &> is superfluous ... ?

No.

If not for the "&", ls would print an error message if wx* does not exist.


All times are GMT -5. The time now is 10:08 AM.