Need to check last file with a starting name


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Need to check last file with a starting name
# 1  
Old 06-16-2016
Need to check last file with a starting name

Hello,

I need help on finding with an if statement or with a command a file that exist in lets suppose /folder with the name ABC* but i need to get the last file generated with that name and fi the file exist then do an echo and if not do another echo

Please help
# 2  
Old 06-16-2016
Is this a homework assignment? Homework and coursework questions can only be posted in the Homework & Coursework Questions forum following special homework rules.

If this is a homework assignment, please refile your request in the proper forum with a completely filled out homework assignment template (from the special homework rules link above).

If not, please tell us:
  1. what operating system you're using,
  2. what shell you're using,
  3. whether you are looking for a file named ABC* or a filename that matches the pattern ABC*, and
  4. whether you are looking for a file only in the directory /folder or for a file in any directory in the file hierarchy rooted in /folder.
# 3  
Old 06-17-2016
Hello,
This is not a homework. I'm just learning UNIX and that is why I posted the question here.
I'm using the sh shell
I'm looking for the last file that matches the first 3 letters ABC. There could be more than 1 file with the name ABC so I need the last one.
So I want to see if that file exist in just the folder /folder

I want to do a simple if file exist then do an echo
if not then do an echo that it doesnt
# 4  
Old 06-17-2016
Well, the last one by what? Alphanumeric sort? Access/change/modification time?

And, help me out with the logics - if a resulting last file is found, then it should exist and a test for existence would be pointless, no?
# 5  
Old 06-17-2016
The last one modification time with the name ABC*

I'm going to do more thing after that but for now I just want to see how to check if a file exist and do echos.

I'm new at this that is why I'm just asking for that
# 6  
Old 06-17-2016
Try
Code:
[ -e $(ls -tr /folder/ABC* | tail -1) ] && echo file exists || echo file missing
file exists

# 7  
Old 06-17-2016
will this work?

Code:
if [ -e  $(ls -tr /folder/ABC* | tail -1) ] 
then
  echo 'File found'
fi

Also quick question why when I do this

Code:
FILE = 'ls -ltr /folder/ABC* | tail -n 1'

I get an error if there is no ABC* files on the folder, do you know why?
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample code segments, sample input files, and sample output files (as required by forum rules.

Last edited by Don Cragun; 06-17-2016 at 10:42 PM.. Reason: Add CODE and ICODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep a log file starting from a specific time to the end of file

I have a log file which have a date and time at the start of every line. I need to search the log file starting from a specific time to the end of file. For example: Starting point: July 29 2018 21:00:00 End point : end of file My concern is what if the pattern of `July 29 2018 21:00:00`... (3 Replies)
Discussion started by: erin00
3 Replies

2. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies

3. Shell Programming and Scripting

Get the last string of a file starting from .

The file names are as below a.txt.gz a.txt How to get the output as .txt.gz .txt (1 Reply)
Discussion started by: vedanta
1 Replies

4. UNIX for Advanced & Expert Users

How to delete a* file with out deleting file name starting with a?

Hi, I have a query: I have a bunch of files starting letter with 'a' (example: abhi,all,anand,animal,a1.txt,allow.java,a*) here i want to delete/remove only a* folder but not other files and folders. and a* folder is present in so many other folders. what is unix command to delete... (9 Replies)
Discussion started by: rajanikanth86
9 Replies

5. UNIX for Dummies Questions & Answers

Count of rows starting with 01 in a file

Hi I am very new to unix and please help me in solving the below problem I have a file with 50000+ rows. Each row(line) start with 01 or 02 or 03. Now i have to get the count of rows starting with 01 only, Thanks in advance (3 Replies)
Discussion started by: nmakkena
3 Replies

6. Shell Programming and Scripting

if statement to check files with different ending but same starting name

I am trying to check if files staring with filename but ending with diffent dates e.g. filename.2011-10-25. The code I am using is below if It works find only if one file is present but returns binary operator expected when there are mulptiple files. Please help me correcting it. I... (5 Replies)
Discussion started by: ningy
5 Replies

7. Shell Programming and Scripting

Get all File names starting with letter P

Hi, I have lets say 10 files , I need to process them one by one. So I need a command to get one file name at a time to process it into a variable Example Files P1111.dat P3344.dat S344.dat ... v_file_name = 'p111.dat' .. I will rename it to something after processing ... (1 Reply)
Discussion started by: prassu
1 Replies

8. UNIX for Advanced & Expert Users

Check EOF char in Unix. OR To check file has been received completely from a remote system

Advance Thanks. (1) I would like to know any unix/Linux command to check EOF char in a file. (2) Or Any way I can check a file has been reached completely at machine B from machine A. Note that machine A ftp/scp the file to machine B at unknown time. (5 Replies)
Discussion started by: alexalex1
5 Replies

9. Shell Programming and Scripting

cat a file starting with ~|

helo all I have a file where lines start with ~|. Given a sample line-> ~|21|123|1232|ABC|2135.... So when i use the command -----cat $file | mailx -s "Rejects : $envid" $recip.dat------ When unix cats the file which holds lines starting with ~| it seems to be it takes those lines... (2 Replies)
Discussion started by: KenJo
2 Replies

10. UNIX for Dummies Questions & Answers

Script to check for a file, check for 2hrs. then quit

I wish to seach a Dir for a specific file, once the file is found i will perform additional logic. If the file is not found within two hours, i would like to exit. Logically, I'm looking for the best way to approach this Thanks for any assistance in advance. Note: I'm using a C shell and... (2 Replies)
Discussion started by: mmarsh
2 Replies
Login or Register to Ask a Question