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

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 .

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

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?

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

Try

[ -e $(ls -tr /folder/ABC* | tail -1) ] && echo file exists || echo file missing
file exists

will this work?

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

Also quick question why when I do this

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?

Did you try your if statement above? If you did try it, what did it do? If you didn't try it, why not? You said you wanted an echo if the file is there and an echo if there is no matching file. I don't see anything in that if command that would echo anything if no matching file is found.

In post #2 in this thread, the first request I made was: "... please tell us ... what operating system you're using". Without an answer to that question we don't know whether sh on your system is a legacy Bourne shell (traditional sh ), a 1988 vintage Korn shell with POSIX extensions (frequently called ksh ), bash , dash , zsh , or some other less common shell.

If the if statement above is working, we know that you are not using a traditional Bourne shell.

There are several things wrong with the variable assignment statement above, none of which would complain about non-existent /folder/ABC* with any of the shells that would be installed with the name sh on common UNIX systems, Linux systems, or BSD systems. These problems include, but might not be limited to:
There cannot be any spaces around the = .
A traditional Bourne shell command substitution (which would be accepted by any of the above shells) uses back-quote characters ( ` ) not single-quote ( ' ). (And, unless you are using a traditional Bourne shell, the back-quote method of of performing command substitutions ( `command` ) is obsolete and should never be used: use the $(command) form of command substitution instead).

When asking for help, please ALWAYS show us the exact diagnostic message produced by the system and the exactly command that you typed into the shell that produced the error message.

With the command:

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

the error that would be expected on most systems would be something like:

-sh: FILE: not found

With an incorrectly configured Apple OS X operating system (where the standard utilities are installed in a case-insensitive filesystem) you might get something like:

=:                                cannot open `=' (No such file or directory)
ls -ltr /folder/ABC* | tail -n 1: cannot open `ls -ltr /folder/ABC* | tail -n 1' (No such file or directory)

because with spaces around the assignment operator, this is an attempt to run the FILE command with the two operands:

=

and

ls -ltr /folder/ABC* | tail -n 1

You might also get an error like that on a Cygwin subsystem running on a Windows operating system, but I haven't tried this on Cygwin.