Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find most recent file of defined pattern, rename, delete others? 1

Status
Not open for further replies.

bazil2

Technical User
Feb 15, 2010
148
DE
(Elementary user)

My goal is to make a script that will:
1) Look for all files in a single directory that begin with plans*
2) Find the one with the most recent time stamp (in my example 18:48) and rename it to plans.pdf
3) Delete all the other files beginning with plan*

[root@sgi subtempl]# ls -al plans*
-rw-rw-rw- 1 root apache 1141692 Mar 3 18:45 plans-1.pdf
-rw-rw-rw- 1 root apache 1141692 Mar 3 18:48 plans-2.pdf
-rw-rw-rw- 1 root apache 1269504 Mar 3 18:44 plans.pdf

Is it possible to do this using a bash script that I could execute with the crontab?

Best regards
 
Hi

If there are plans* files with other extensions than .pdf :
Code:
[navy]latest[/navy][teal]=[/teal][green][i]"$( ls -t plans* | head -1 )"[/i][/green]
mv [green][i]"$latest"[/i][/green] [green][i]'.latest-plans.pdf'[/i][/green]
rm plans[teal]*[/teal]
mv [green][i]'.latest-plans.pdf'[/i][/green] [green][i]'plans.pdf'[/i][/green]
If all plans* files have .pdf extension ( so only the middle part differs ) :
Code:
[navy]latest[/navy][teal]=[/teal][green][i]"$( ls -t plans* | head -1 )"[/i][/green]
mv [green][i]"$latest"[/i][/green] [green][i]'plans.pdf'[/i][/green]
rm plans[teal]?*.[/teal]pdf
[small][maroon]Warning[/maroon] The above code was not tested[/small]

Feherke.
 
Thank you very much Fehrerke, it was very kind of you to help me; the script works perfectly!

Best regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top