Finding the file name in a directory with a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding the file name in a directory with a pattern
# 1  
Old 08-02-2017
Finding the file name in a directory with a pattern

I need to find the latest file -filename_YYYYMMDD in the directory DIR. the below is not working as the position is shifting each time because of the spaces between(occuring mostly at file size field as it differs every time.)

please suggest if there is other way.



Code:
report =‘ls -ltr $DIR/filename_* 2>/dev/null | tail -1 | cut -d “ “ -f9’


Last edited by Don Cragun; 08-02-2017 at 11:20 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 08-02-2017
Quote:
Originally Posted by archana25
I need to find the latest file -filename_YYYYMMDD in the directory DIR. the below is not working as the position is shifting each time because of the spaces between(occuring mostly at file size field as it differs every time.)

please suggest if there is other way.



Code:
report =‘ls -ltr $DIR/filename_* 2>/dev/null | tail -1 | cut -d “ “ -f9'

There are lots of problems here:
  • there can't be a space before the <equals-sign> in an assignment statement,
  • you need to use ASCII <double-quote> characters to quote shell arguments; not a pair of Unicode <opening-double-quote> characters,
  • a command substitution needs to be surrounded by a pair of ASCI <back-quote> characters or (depending on what shell you're using) a $( at the start and a ) at the end; not by an ASCII <back-quote> at the start and an ASCII <apostrophe> at the end, and
  • there is no reason to use long format ls output when you just want the filename.
Maybe this will come closer to what you need:
Code:
report=`ls -t "$DIR"/filename_* 2>/dev/null | head -1`

# 3  
Old 08-03-2017
In addition to what Don has listed, you may still hit problems if there are lots of files that match the filename pattern. This is because the shell will expand the pattern to match the list of all files before passing it to ls to list them in modification time order (-t), newest first. Because of this, you might overstep the limit of the command line. You haven't suggested how many files this might match. Is it a lot?

This is a system specific limit and you may never hit the maximum command line length, but if this is the case, you might be into a rather painful process to read the timestamp on each file and sort them yourself. Perhaps something like this might do the trick:-
Code:
for file in "${DIR}"/filename_*
do
   stat -c '%Y %n' "${file}"
done | sort -n | tail -1 | cut -f2- -d" "

It's messy, I know, but it might get round the command line limit, if you were to hit that.

As well as using CODE tags as in the tutorial, please can you also include details of the OS and shell you are using on each thread. The output from uname -a and ps -o cmd= -p $$ should suffice, unless your question is hardware/devices in nature.


I hope that you don't need the above code, but it might help.

Kind regards,
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Finding the same pattern in three consecutive lines in several files in a directory

I know how to search for a pattern/regular expression in many files that I have in a directory. For example, by doing this: grep -Ril "News/U.S." . I can find which files contain the pattern "News/U.S." in a directory. I am unable to accomplish about how to extend this code so that it can... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

2. Shell Programming and Scripting

Finding file pattern in ksh 88

Hi, I've to find the file which has the pattern "Delete Report for History Tables" and need to search this file pattern from directory which has sub directories as well. I'm using ksh 88 Please suggest me which command will be used to find the file pattern . Thanks. (1 Reply)
Discussion started by: smile689
1 Replies

3. Shell Programming and Scripting

Finding the pattern and replacing the pattern inside the file

i have little challenge, help me out.i have a file where i have a value declared and and i have to replace the value when called. for example i have the value for abc and ccc. now i have to substitute the value of value abc and ccc in the place of them. Input File: go to &abc=ddd; if... (16 Replies)
Discussion started by: saaisiva
16 Replies

4. Shell Programming and Scripting

Finding duplicates in a file excluding specific pattern

I have unix file like below >newuser newuser <hello hello newone I want to find the unique values in the file(excluding <,>),so that the out put should be >newuser <hello newone can any body tell me what is command to get this new file. (7 Replies)
Discussion started by: shiva2985
7 Replies

5. Shell Programming and Scripting

Finding 4 current files having specific File Name pattern

Hi All, I am trying to find 4 latest files inside one folder having following File Name pattern and store them into 4 different variables and then use for processing in my shell script. File name is fixed length. 1) Each file starts with = ABCJmdmfbsjop letters + 7 Digit Number... (6 Replies)
Discussion started by: lancesunny
6 Replies

6. UNIX for Dummies Questions & Answers

Need help finding a file where a pattern exists and the file has a timestamp

So, I know how to do some of this stuff on an individual level, but I'm drawing a blank as to how to put it all together. I have a pattern that I'm looking for in a log file. The log file I know came in yesterday, so I want to limit the search to that day's listing of files. How would I do... (5 Replies)
Discussion started by: kontrol
5 Replies

7. Shell Programming and Scripting

Ksh - finding pattern in file and generating a new file

I am trying to find for the pattern in first 5 bytes of every line in a text file and then generate a new file with the pattern found. Example: expected pattern is '-' to be serached in first 5 bytes of file only. Input File ab-cd jan-09 ddddd jan09 cc-ww jan09 dsgdq jan-09 ... (2 Replies)
Discussion started by: net
2 Replies

8. Shell Programming and Scripting

help with finding & replacing pattern in a file

Hi everyone. Could u be so kind and help me with on "simple" shell script? 1. i need to search a file line by line for a pattern. example of a lines in that file 2947 domain = feD,id = 00 0A 02 48 17 1E 1D 39 DE 00 0E 00,Name Values:snNo = f10 Add AttFlag = 0 2. i need to find... (0 Replies)
Discussion started by: dusoo
0 Replies

9. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies

10. UNIX for Dummies Questions & Answers

finding file in a directory

hi can i know how to locate a file in a directory by using the "find" command? i want to know the exact path the files reside in in the result. thanks :confused: (4 Replies)
Discussion started by: legato
4 Replies
Login or Register to Ask a Question