LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-22-2005, 03:49 PM   #1
newbie_st
LQ Newbie
 
Registered: Aug 2005
Posts: 2

Rep: Reputation: 0
shell scripting help


Hi,

I would like some help on some shell scripting that I am trying to do.

Basically, I would like to write a shell script to ftp a file and then rename

the file to something else.

I already have a shell script to ftp a file if the file name does not change.
However, the file I want to ftp now has a file name change to the last
part of the file name every month.

For example:

newfile.20050801.gz will be newfile.20050901.gz next month to reflect
that the file has been updated and was created in september.

I want to write some lines that will look into the directory of interest and
grab the file name of the newest file there and insert it into a variable.

Then I want to rename the file.

Also, as an aside how do I capture the date a file was last saved in a variable?

How would I do this??

thanks very much!!
 
Old 08-22-2005, 04:19 PM   #2
flower.Hercules
Member
 
Registered: Aug 2005
Distribution: Gentoo
Posts: 228

Rep: Reputation: 31
You might try using sed and regular expressions.

Not sure what you mean by 'catpure' but something like

$variable > filename

will take the variable and place it in a file, not sure if that is what you wanted.

HTH

Last edited by flower.Hercules; 08-22-2005 at 04:25 PM.
 
Old 08-22-2005, 05:34 PM   #3
zedmelon
Member
 
Registered: Jun 2004
Location: colorado, USA
Distribution: slack, oBSD
Posts: 119

Rep: Reputation: 24
Re: shell scripting help

Quote:
Originally posted by newbie_st
I would like to write a shell script to ftp a file and then rename the file to something else.
I would recommend renaming the file locally. You may overwrite remote files otherwise. If you keep a local copy of everything that exists remotely (or at least a file containing a list of it), you can track what's there without connecting to the remote machine just to list files, and you'll never accidentally rename a file to overwrite an existing one.

Well, not never, but you know... ;)

Quote:
Originally posted by newbie_st
I want to write some lines that will look into the directory of interest and
grab the file name of the newest file there and insert it into a variable.

Also, as an aside how do I capture the date a file was last saved in a variable?
The 'ls' command can list files in a directory in order of modification time. It will also list mod time with the -l flag. Man ls for more info.

Try
ls -ltra

Play with the below commands in the directory, and see what happens when you change the number 6 to 7 or 8:
ls -laF | awk '{print $6}'
ls -ltra | tail -5 | awk '{print $7}'

[edit:] One more hint:
FILENAME=`ls -ltra | tail -1 | awk '{print $8}'`
will set $FILENAME to name the last modified file. Careful, because it is likely to be the working directory.

Last edited by zedmelon; 08-22-2005 at 05:38 PM.
 
Old 08-23-2005, 11:16 AM   #4
newbie_st
LQ Newbie
 
Registered: Aug 2005
Posts: 2

Original Poster
Rep: Reputation: 0
Hi,

Thanks for the suggestions.

I tried the command below and it gave me a time of day. I tried to remove the r and it gave me the year of the oldest file.
FILENAME=`ls -ltra | tail -1 | awk '{print $8}'`


I am confused because the first thing I am trying to do is to capture the name of the newest file from the
site I am trying to grab a file of.

On my own directory I am trying to grab the time stamp of a file that I do know the name of.

I tried the other examples but I seem to be getting parts of a date I acutually want the whole date.

thanks very much!!
 
Old 08-23-2005, 03:18 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
I would (if possible) use scp if I were you

scp <local-file> <user>@<host>:<remote-file>

If you set-up passowrd less authentication for ssh
scripting it is trivial.
 
Old 08-28-2005, 02:52 PM   #6
zedmelon
Member
 
Registered: Jun 2004
Location: colorado, USA
Distribution: slack, oBSD
Posts: 119

Rep: Reputation: 24
Sorry, I haven't had time to check my mail.
Quote:
Originally posted by newbie_st
I tried the command below and it gave me a time of day. I tried to remove the r and it gave me the year of the oldest file.
FILENAME=`ls -ltra | tail -1 | awk '{print $8}'`
The year of the oldest file. Correct. So the time of day was the last time the NEWEST file in the directory was modified. The different flags on this usage of ls are:
a - all files
l - long listings
t - sort by last modification time
r - reverse order

Piping the list (or anything else) through tail gives the "tail end" of the output. The default is ten lines, and we ask for one line with "-1." Piping THAT through this simple usage of awk (I am not an awk guru) will simply give you a field (in this case the 8th) in the line. Playing around with that number should show you a bit more.

Try eliminating the tail and/or printing more fields. Depending on how many files are in your directory, you should get a long list.
'ls -ltra | awk '{print $5" "$6" "$7}'

Quote:
Originally posted by newbie_st
I am confused because the first thing I am trying to do is to capture the name of the newest file from the
site I am trying to grab a file of.
Well, depending on how you connect, this may become far more complicated.

To do it locally, start simple:
Code:
# ls -latr
(files listed in mod time order, old to new)
# ls -ltra | tail -1
(gives you the newest file)
#ls -1tra  <-- note the change from l (L) to 1 (one)
(lists JUST filenames)
#ls -1tra | tail -1
(lists JUST the newest filename)
With the awk stuff, you can see exactly when each file was modified. I suppose you wouldn't need awk

Quote:
Originally posted by newbie_st
On my own directory I am trying to grab the time stamp of a file that I do know the name of.

I tried the other examples but I seem to be getting parts of a date I acutually want the whole date.

thanks very much!!
By the way, I strongly agree with Tinkster. SSH is a far better alternative to FTP if you don't have a strong reason to NOT use a secure connection and transfer.

Also, depending on your situation, you might do a bit of research on the rsync command. It will synchronize a remote directory with a local one (using your choice as the master) and can use the ssh protocol as well.

Above all, playing with stuff is how you learn. Copy a bunch of your important files into a new experimental directory so you know you have duplicates. Move things. Rename them. List them and sort them. Man tar. The more you ask yourself "I wonder what happens if I...?" the more you'll learn about things that will prove useful sooner or later. If you have everything backed up, you can be adventurous knowing you won't break anything that can't be restored.

I initially assumed you wanted to UPload this file. I realize now that I was probably mistaken about that. I was going to clarify a bit more correct spelling, clumsy phrasing, etc), but I just got a call that my son is sick, so I'm stopping now because posting what's here will help more than no reply. I hope this helps.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM
Shell Scripting again yaadhav Linux - Newbie 8 08-04-2005 10:21 AM
shell scripting pcdebb Linux - Newbie 17 11-20-2003 03:49 PM
shell scripting markus1982 Linux - General 2 05-20-2003 07:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration