Match filename pattern with -f


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match filename pattern with -f
# 1  
Old 01-06-2017
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
# 2  
Old 01-06-2017
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?
# 3  
Old 01-06-2017
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.
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 01-06-2017
Quote:
Originally Posted by RudiC
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.
# 5  
Old 01-06-2017
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..
This User Gave Thanks to RudiC For This Post:
# 6  
Old 01-07-2017
Quote:
Originally Posted by MadeInGermany
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

# 7  
Old 01-09-2017
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

2. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

3. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

4. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

5. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

6. Shell Programming and Scripting

Getting filename for Nth line pattern match

Hi, I have many scripts in particular directory. And few of the scripts have exit 0 in second line. Now i wanted to list out the scripts name which has the exit 0 in its second line I tried many options , but i can not get the filename along with the nth line pattern match :mad:. Can anyone... (14 Replies)
Discussion started by: puni
14 Replies

7. Shell Programming and Scripting

Filename pattern match and appending pipe

Hi, I have a directory with around 100k files and files with varying sizes(10GB files to as low as 5KB). All the files are having pipe dilimited records. I need to append 7 pipes to the end of each record, in each file whose name contains _X3_ and need to append 10 pipes to the end of each... (3 Replies)
Discussion started by: nss280
3 Replies

8. Shell Programming and Scripting

Match first pattern first then extract second pattern match

My input file: <accession>Q91G55</accession> <name>043L_IIV6</name> <protein> <recommendedName> <location> <position position="294"/> </location> <fullName>Uncharacterized protein 043L</fullName> <accession>P18556</accession> <name>1106L_ASFB7</name> <protein> <recommendedName>... (5 Replies)
Discussion started by: patrick87
5 Replies

9. Shell Programming and Scripting

Does Filename Match Pattern

Hi, I am writing a BASH script. I have a list of files and I would like to make sure that each is of a specific pattern (ie *.L2). If not I would like to remove that file. How do I test whether a filename matches a given pattern? Thanks a lot. Mike (10 Replies)
Discussion started by: msb65
10 Replies

10. Shell Programming and Scripting

To identify filename in which having match PATTERN

Hi, Any idea to identify bunch of files( gz format) in which having match PATTERN wanted and print out those files ? :) Regards, (14 Replies)
Discussion started by: cedrichiu
14 Replies
Login or Register to Ask a Question