Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Match filename pattern with -f

Shell Programming and Scripting


Tags
bash

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-06-2017   -   Original Discussion by looney
looney looney is offline
Registered User
 
Join Date: Oct 2015
Last Activity: 22 October 2017, 2:07 PM EDT
Posts: 90
Thanks: 62
Thanked 11 Times in 11 Posts
Match filename pattern with -f

Hello All,
I have two issues.
1).I want to check if directory exists and inside that if file exists with today's date minus one. I can check directory exists but how can i check only a pattern of filename in that directory.Name of file is files-20170105-09.gz.
2).Also i want to exit immediately if directory is not present. Is exit 0 correct. Please help.
Code:
dir=`date "+%Y%m%d"`
filename="files-`date -d '-1 day' '+%Y%m%d'`"
echo "$filename"

[[ -d "$dir" ]] && echo "Dir exists $dir" || echo "dir $dir doesnot exists" ||  exit 0

[[ -f '$dir/$filename*' ]] && echo "Filename $filename is correct" || echo "Filename is not correct"


Last edited by looney; 01-06-2017 at 07:14 AM.. Reason: bash
Sponsored Links
    #2  
Old Unix and Linux 01-06-2017   -   Original Discussion by looney
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 November 2017, 1:35 PM EST
Location: Aachen, Germany
Posts: 11,638
Thanks: 320
Thanked 3,618 Times in 3,323 Posts
For your second request, try a "group command":

Code:
[[ -d "$dir" ]] && echo "Dir exists $dir" || { echo "dir $dir doesnot exists"; exit 0; }

For your first question, what do you mean by "check only a pattern of filename"? A substring?
Sponsored Links
    #3  
Old Unix and Linux 01-06-2017   -   Original Discussion by looney
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 21 November 2017, 12:00 PM EST
Location: Simplicity
Posts: 3,826
Thanks: 319
Thanked 1,282 Times in 1,160 Posts
I usually prefer if-then-else-fi

Code:
if [[ -d "$dir" ]]; then echo "Dir exists $dir"; else echo "dir $dir doesnot exists"; exit 0; fi

and think that multi-line is better readable and maintainable.
Problem #2 "test for matching files" can be solved with a file_exists function.

Code:
file_exists(){
  for _i do
    [ -f "$_i" ] && return
  done
  return 1
}
if [[ -d "$dir" ]]
then
  echo "Dir exists $dir"
  if file_exists "$dir/$filename"*
  then
    echo "Filename $filename is correct"
  else
    echo "Filename is not correct"
  fi
else
  echo "dir $dir doesnot exists"
  exit 0
fi

In such a structure I can often omit an exit or return.
The Following User Says Thank You to MadeInGermany For This Useful Post:
looney (01-07-2017)
    #4  
Old Unix and Linux 01-06-2017   -   Original Discussion by looney
looney looney is offline
Registered User
 
Join Date: Oct 2015
Last Activity: 22 October 2017, 2:07 PM EDT
Posts: 90
Thanks: 62
Thanked 11 Times in 11 Posts
Quote:
Originally Posted by RudiC View Post
For your second request, try a "group command":

Code:
[[ -d "$dir" ]] && echo "Dir exists $dir" || { echo "dir $dir doesnot exists"; exit 0; }

For your first question, what do you mean by "check only a pattern of filename"? A substring?

Hello RudiC,
In the directory I will get filename say files-20170105-09.gz. I have to check if file exists also if exists then it should have today's date minus one. So I have to search this part 20170105. Rest of the part of file , I will not be knowing. That is the reason to check it I was using wildcard *

---------- Post updated at 07:36 AM ---------- Previous update was at 07:34 AM ----------

Hello MIG , let me try it.
Sponsored Links
    #5  
Old Unix and Linux 01-06-2017   -   Original Discussion by looney
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 November 2017, 1:35 PM EST
Location: Aachen, Germany
Posts: 11,638
Thanks: 320
Thanked 3,618 Times in 3,323 Posts
Still not clear what you want.

Just guessing: [[ -f '$dir/$filename*' ]] does not supply your filename.
man bash:
Quote:
Word splitting and pathname expansion are not performed on the words between the [[ and ]];
You could try to use [ ... ]. But, some caveats are to be considered:
Within single quotes, no variable expansion will be done - use double quotes instead. And, within quotes, the * loses its wildcard meaning - put it outside the quotes. And, make very sure that the expansion yields one single filename only to avoid a bash error.

Does it fly?

Last edited by RudiC; 01-06-2017 at 10:59 AM..
The Following User Says Thank You to RudiC For This Useful Post:
looney (01-07-2017)
Sponsored Links
    #6  
Old Unix and Linux 01-07-2017   -   Original Discussion by looney
looney looney is offline
Registered User
 
Join Date: Oct 2015
Last Activity: 22 October 2017, 2:07 PM EDT
Posts: 90
Thanks: 62
Thanked 11 Times in 11 Posts
Quote:
Originally Posted by MadeInGermany View Post
I usually prefer if-then-else-fi
In such a structure I can often omit an exit or return.
Thanks MIG, I have applied your function and it is working fine. Could you please explain it. Also what if i simply use below code
Code:
if ls 20170108/file* ; then
echo "file is present"
else 
"file is not present"
fi

Sponsored Links
    #7  
Old Unix and Linux 01-09-2017   -   Original Discussion by looney
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 21 November 2017, 12:00 PM EST
Location: Simplicity
Posts: 3,826
Thanks: 319
Thanked 1,282 Times in 1,160 Posts
You can do it with ls -d , but the external program means some overhead, and you must discard stdout and stderr, and there are some border cases if special files would match.
--
The function lets the shell expand the arguments; the for loop cycles through them, tests them, and returns immediately with status 0 ("true") if one is present.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Rearrange or replace only the second line after pattern match or pattern match invinzin21 Shell Programming and Scripting 2 09-22-2014 02:00 PM
Getting filename for Nth line pattern match puni Shell Programming and Scripting 14 08-26-2010 06:11 AM
Filename pattern match and appending pipe nss280 Shell Programming and Scripting 3 01-20-2010 08:49 AM
Does Filename Match Pattern msb65 Shell Programming and Scripting 10 12-16-2008 08:00 PM
To identify filename in which having match PATTERN cedrichiu Shell Programming and Scripting 14 03-23-2008 09:05 PM



All times are GMT -4. The time now is 03:17 PM.