If file pattern exists in directory then continue


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers If file pattern exists in directory then continue
# 1  
Old 11-15-2019
If file pattern exists in directory then continue

he below looks in $dir for any pattern of fileone. As is, it executes but only returns File found if the exact format in the script exsists. Why isn't a pattern of fileone being looked for and if it is in [CODE]$dir[/ICODE], File found. I think that is what should happen. Thank you Smilie.


Code:
dir=/path/to
if ls "$dir"/fileone.csv* > /dev/null 2>&1
then
    echo "File not found!" >> "$dir"/log && exit 0
else
    echo "File was found, moving on " >> "$dir"/log
fi


Last edited by cmccabe; 11-15-2019 at 04:37 PM.. Reason: fixed format
# 2  
Old 11-15-2019
Don't understand. Above will echo "File not found!" if any file name beginning with fileone.csv exists in dir, like
Code:
fileone.csv
fileone.csv12
fileone.csvacbdefaj
fileone.csv_sdfwegf

If none of those or alike exist, it will echo "File was found, moving on ".
This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-15-2019
If any of the below are in $dir then File found was found, moving on.

Code:
fileone.csv
fileOne.csv
Fileone.csv
FileOne.csv

If none of those or alike exist, it will echo "File was found and exit. The pattern will always be fileone.csv just the case may be different. Maybe there is a better way? Thank you Smilie.
# 4  
Old 11-16-2019
Quote:
Originally Posted by cmccabe
... If none of those or alike exist, it will echo "File was found and exit ...
Please take care when formulating your request. Misleading specifications may lead to unsatisfactory proposals.





Try
Code:
if ls "$dir"/[Ff]ile[Oo]ne.csv > /dev/null 2>&1
  then  echo "File was found, moving on "
  else  echo "File not found!"
fi

This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-16-2019
Define a file_exists function.
Then you can do
Code:
if file_exists "$dir"/[Ff]ile[Oo]ne.csv
then
...

This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 11-18-2019
Thank you both Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check that at least one file exists in the directory.

There are some files with suffix dates like abc_20032019.dat abc_17032019.dat If at least one file exists then perform some operation else exit from execution. Korn shell ---------------------------------- array=($inputdir/abc*.dat) If ] ] then echo " file exits" else echo " file does... (10 Replies)
Discussion started by: Rajesh123
10 Replies

2. Shell Programming and Scripting

Search if file exists for a file pattern stored in array

Hi experts, I have two arrays one has the file paths to be searched in , and the other has the files to be serached.For eg searchfile.dat will have abc303 xyz123 i have to search for files that could be abc303*.dat or for that matter any extension . abc303*.dat.gz The following code... (2 Replies)
Discussion started by: 100bees
2 Replies

3. UNIX for Dummies Questions & Answers

Outputting 1 file per row if pattern exists between files

I have many files that can have various amounts of rows. I essentially want to output each row into a new file if a pattern is matched between two files. I have some code that does something similar but I want it to output every single input row from every file into a separate output file; that... (5 Replies)
Discussion started by: verse123
5 Replies

4. Shell Programming and Scripting

Check whether file exists in directory

Hi guys, I am beginner trying to learn unix. So any help is welcomed. My requirement is to check whether is a file exists in a particular directory or not. The directory path and filename are taken dynamically with user interaction. So the program should continue only if the $filename... (1 Reply)
Discussion started by: maris_markur
1 Replies

5. Shell Programming and Scripting

How to check if the file exists in directory?

Hi Gurus, I have a requests to find if all the file in the filelist exist in certain directory. example: my filelist abc def ddd cde afg how can I find these 5 files exists at director /home/abc Thanks in advance (7 Replies)
Discussion started by: ken6503
7 Replies

6. Shell Programming and Scripting

Check for Pattern if exists write to file

Hi ! All I just want to search and write to new file if pattern is found in text file following are my text files by which I want to search Month and last column number my text file1 15-Jan-2011 25 ARTS 1255 125 125 178 198 15-Jan-2011 25 ARTS 1255 125 125 178 198 15-Jan-2011 25... (3 Replies)
Discussion started by: nex_asp
3 Replies

7. Shell Programming and Scripting

How to check if a file exists in a directory?

I want to perform SQL *Loader operation only if a file named "load.txt" exists in a directory "/home/loc/etc". Please help how to check this with a if condition. (8 Replies)
Discussion started by: vel4ever
8 Replies

8. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

9. Shell Programming and Scripting

check if directory and file exists

cp $PATHLOGS/$DATE/*.* $TMP/logs_tmp/ cp $PATHLOGS/$DATE1/*.* $TMP/logs_tmp/ Before copying the files I have to check if the directory $DATE1 and $DATE2 exists. If directory exists then, check if the folder contains some files. if the file exists then, check if the file size is greater... (3 Replies)
Discussion started by: sandy1028
3 Replies

10. Shell Programming and Scripting

check if file exists with pattern matching

Hello friends, I am writing a simple shell script which will copy one particular type of files to backup folder if files exists. If files doesn't exists, mv command should not be executed. My file pattern is like wcm-spider-maestro.log.2009-07-15, wcm-spider-maestro.log.2009-07-16 etc.. I... (6 Replies)
Discussion started by: sreenu.shell
6 Replies
Login or Register to Ask a Question