Extract the filename and write to .txt

I'm new to this forum and also to UNIX scripting.

I need a command to extract the filename from the path and write to .txt file.

Thanks in advance for your guidance.

Hello Ram Kumar_BE,

Welcome to forums, hope you will enjoy learning and sharing knowledge here. Following is an example of what you have asked for. Let's say we have a file named test43 in path /tmp and we want to look for it, following may help you then.

 find /tmp -type f -name "test43" 2>/dev/null > output_file
 

Hope this helps, you can learn more about command find in manual page of it by doing a man find .

Thanks,
R. Singh

Welcome Ram_Kumar_BE,

There will be several ways to do this depending on what you are working with. You might use basename which is easy, but slower than slicing up the path in your code. The cost (in wait time) might only be a problem if you are running it a lot though.

Can you show us how far you have got and we can suggest ways for you to try. Some context to help get a suitable solution would help too.

Kind regards,
Robin

Hi Ravinder,

Thanks for your code. However, let me expand my requirement, so that I may get a more direct answer.

I've a folder /tmp and in that folder everyday I receive 'n' number of files like SASApp_STPServer_2015-08-25_tmptcmsaslva2_12345.log SASApp_STPServer_2015-08-25_tmptcmsaslva2_67891.log & SASApp_STPServer_2015-08-25_tmptcmsaslva2_78967.log .

Only numeric digits before .log in the filename will vary alongside yyyy-mm-dd. I may get 1 to 20 files everyday.

My requirement is to search for current date-1 .log files in /tmp folder and write the file name to filenames.txt

e.g. if date is 2015-08-26, then I need to extract for yesterday's filenames (current date-1) in filenames.txt

Thanks for any help you offer me. I also started searching for the commands for my requirements and will look forward to learning with you people.

So, what you may be better doing is to:-

  • Work out yesterday's date in the correct format
  • List all the files that match yesterday's date into the file

The listing is quite easy in that you provide a pattern to match against. Working out yesterday's date can be more difficult, depending which OS and version you are running. Some have tools that will make it easier.

Can you provide the output of:-

uname -a

Feel free to overwrite the server name if you feel that is sensitive.

Robin

Hello Ram Kumar_BE,

Following is an example for same, I ran this following command in the directory itself where files were present if you want to run it from any other location then please put the complete path in loop as follows.

for i in "SASApp_STPServer_"$(date +%Y-%m-%d --date "1 day ago")"_tmptcmsaslva2_"*".log"
do
    echo $i
done
 

Output will be as follows(In my case I have created test files in /tmp ).

SASApp_STPServer_2015-08-25_tmptcmsaslva2_67891.log
SASApp_STPServer_2015-08-25_tmptcmsaslva2_78967.log
 

If you are happy with above command's results then you can re-direct the echo $i to a output filename
eg-> echo $i >> output_file.txt .

Thanks,
R. Singh

Hi Ravinder,

I appreciate your quick response and it makes my job easier. Lot of stuffs to learn.

My script location, .log file location and .txt file location is different.

Now I would like to know how to add the folder name in for loop to search for the file and to create a .txt file in /tmp/file/.

what is --date in "$(date +%Y-%m-%d --date "1 day ago") ?

Hello Ram_Kumar_BE,

Following may help you in same, you can Thank you button on left of every post to thank any user in this forum :b:

for i in /tmp/"SASApp_STPServer_"$(date +%Y-%m-%d --date "1 day ago")"_tmptcmsaslva2_"*".log"
do
     echo $i
done
/tmp/SASApp_STPServer_2015-08-25_tmptcmsaslva2_67891.log
/tmp/SASApp_STPServer_2015-08-25_tmptcmsaslva2_78967.log
 

EDIT: Following is the explanation for same.

for i in /tmp/"SASApp_STPServer_"$(date +%Y-%m-%d --date "1 day ago")"_tmptcmsaslva2_"*".log"     ######## it is a for loop where i is a variable and 
                                                                                                  ######## date +%Y-%m-%d --date "1 day ago" is LIKE--> date +%Y-%m-%d gives output (2015-08-26) in YYYY-MM-DD format, and complete command date +%Y-%m-%d --date "1 day ago" give 1 day before date
                                                                                                  ######## So i in /tmp/"SASApp_STPServer_"$(date +%Y-%m-%d --date "1 day ago")"_tmptcmsaslva2_"*".log"  means if any file satisfies the regex then condition is TRUE
do
     echo $i                                                                                      ######## If any results falls under above mentioned then print variable named i's value which is the filename
done
 

Thanks,
R. Singh

1 Like

Why not - assuming a recent shell - try

ls -1 SASApp_*$(printf "%(%F)T" $(($(printf "%(%s)T") - 86400)))* > /tmp/file.txt

Hi Ravinder,

When I ran the script below, I got error as syntax error near unexpected token `do

My script name is list_file.sh and I ran as sh list_file.sh

#!/bin/sh
for i in /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/"SASApp_STPServer_"$(date +%Y-%m-%d --date "1 day ago")"_tmptcmsaslva2_"*".log"
do
  echo $i
done

echo $i >> output_file.txt

Also how to get script log as .log file in same location?

Thanks in advance for your advise.

---------- Post updated at 03:47 AM ---------- Previous update was at 03:45 AM ----------

When I ran the following command in command prompt, I got error as printf: `(': invalid format character.

ls -1  /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_*$(printf "%(%F)T" $(($(printf "%(%s)T") - 86400)))* > file.txt

---------- Post updated at 03:48 AM ---------- Previous update was at 03:47 AM ----------

Hi RudiC,

When I ran the following command in command prompt, I got error as printf: `(': invalid format character.

ls -1 /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_*$(printf "%(%F)T" $(($(printf "%(%s)T") - 86400)))* > file.txt

Please use code tags as required by forum rules!

Do you use a "recent" shell? What be it's version? With GNU bash, version 4.3.30(1) , it yields

ls -1 SASApp_*$(printf "%(%F)T" $(($(printf "%(%s)T") - 86400)))*
SASApp_STPServer_2015-08-26_tmptcmsaslva2_67891.log

I'm using SSH secure shell and it's version is 3.2.9

That's the communication tool used to connect to the remote machine. On the remote machine, what shell are you running?

I don't know what you're asking. I open SSH secure shell and then I navigate to the tab 'open terminal window' to write/submit my script.

Don't you login to the "terminal window" supplying a username and a password? Or does it log you in automatically due to e.g. "public key authentication"? Doesn't it show you a command prompt, then?

I will give username and password while selecting profiles in SSH. Thereafter I can connect the terminal window without any credentials. I will get the command prompt once I connect the terminal window.

That command prompt is most probably issued by the shell on the remote node. What version is it? Try e.g. echo $SHELL

Im getting as

/bin/bash

Well then - what do you get for bash -- version ?

I'm getting only

/bin/bash

I'm not getting anything after running echo $SHELL