LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-13-2010, 12:57 PM   #1
Drigo
Member
 
Registered: May 2009
Posts: 111

Rep: Reputation: 17
Angry 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!!

Last edited by pixellany; 04-13-2010 at 03:24 PM. Reason: title tweak
 
Old 04-13-2010, 01:48 PM   #2
modprob
Member
 
Registered: Jul 2009
Location: Montreal
Distribution: Slackware
Posts: 35

Rep: Reputation: 17
I suggest you read the man page of find. Particularly the use of the exec option.
 
0 members found this post helpful.
Old 04-13-2010, 02:09 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Code:
shopt -s nullglob
for file in wx*
do
    <whatever>
done
 
Old 04-13-2010, 03:30 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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.
 
Old 04-24-2010, 09:05 AM   #5
cola
Senior Member
 
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,031

Rep: Reputation: 63
Quote:
Originally Posted by catkin View Post
Code:
shopt -s nullglob
for file in wx*
do
    <whatever>
done
Why is shopt command for?
 
Old 04-24-2010, 09:07 AM   #6
cola
Senior Member
 
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,031

Rep: Reputation: 63
Quote:
Originally Posted by Drigo View Post
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.
 
Old 04-24-2010, 09:36 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by cola View Post
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
 
Old 04-24-2010, 10:07 AM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
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.

Last edited by MTK358; 04-24-2010 at 10:09 AM.
 
Old 04-24-2010, 10:33 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by MTK358 View Post
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 ... ?
 
Old 04-24-2010, 11:19 AM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by catkin View Post
BTW I think the & in &> is superfluous ... ?
No.

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Using BASH redirects and "prompt" files to interact with commands/scripts ?? swedish_lunacy Linux - Newbie 22 07-27-2006 08:33 AM
what is the plural of linux? da_zombie General 7 12-08-2004 12:43 PM
Copying linked files, replacing directories in bash scripts? ta0kira Programming 2 10-10-2004 04:46 AM
bash scripts and running .sh files xviddivxoggmp3 Programming 9 08-14-2004 05:33 AM
Bash backup script - If multiple files starting with a exist problem demoncheese Programming 2 07-29-2004 10:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:06 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration