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
# 8  
Old 06-17-2016
Quote:
Originally Posted by RoronoaZoro
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.
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:
Code:
FILE = 'ls -ltr /folder/ABC* | tail -n 1'

the error that would be expected on most systems would be something like:
Code:
-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:
Code:
=:                                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:
Code:
=

and
Code:
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.
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