LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-23-2011, 01:26 AM   #1
RCCollins
LQ Newbie
 
Registered: May 2008
Posts: 4

Rep: Reputation: 0
Problem-Simple bash script to copy the latest .jpg file


Hello,
I can not seem to get this simple script to work. I am trying to copy the most recently created .jpg file in a directory, I believe the problem is that that the .jpg file has parenthesis in its name, for example 00000(aaa)11111.jpg

Code:
cp "00000(aaa)11111.jpg" /var/www/html/archive/
Works fine, however my script does not

Code:
#!/bin/bash
cd /var/www/html/ftp/
LATEST= ls -t | head -1
cp "$LATEST" /var/www/html/archive/
exit
Any help would be appreciated
 
Old 11-23-2011, 01:33 AM   #2
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
It looks like that LATEST= line will print the result but not set LATEST. Try

Code:
LATEST=$(ls -1 | head -1)
Actually it is setting LATEST to "". It would be the same if you did

LATEST="" ls -l | head -1

Last edited by dive; 11-23-2011 at 01:34 AM.
 
1 members found this post helpful.
Old 11-23-2011, 01:46 AM   #3
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 193Reputation: 193
There are a number of ways to assign the output of a command to a variable in bash.

Code:
 LATEST=$(ls -t | head -1)
Code:
 LATEST=`ls -t | head -1`
will both have the same effect, for this example.
 
Old 11-23-2011, 02:01 AM   #4
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
Be aware though that `` is deprecated in favour of $()
 
1 members found this post helpful.
Old 11-23-2011, 09:57 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
You really shouldn't be parsing the output of ls either.

http://mywiki.wooledge.org/ParsingLs
http://mywiki.wooledge.org/BashFAQ/087

Probably the safest way to find the newest (or largest, etc.) file is to run through the directory in a simple loop.

Code:
for file in *; do
	[[ -f $file && $file -nt $NEWEST ]] && NEWEST=$file
done

echo "$NEWEST"
Since it uses globbing, it's perfectly suitable for use with filenames containing whitespace, even newlines.

You also don't usually need head just to grab the first line of an input text. The built-in read can do it just as well. Use it in combination with process substitution to grab the output of a command instead of a file.

Code:
read -r topline < <( command )
echo "$topline"
 
Old 11-23-2011, 10:37 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Well if we are looking at alternatives ... this one is a bit choppy but does the job:
Code:
find /path -type f -name '*.jpg' -printf '%T@\t%p\n' | sort -k1,1n | tail -n1 | cut -f2-
Of course if only looking in current directory I would set maxdepth as well.
 
Old 11-24-2011, 02:24 PM   #7
RCCollins
LQ Newbie
 
Registered: May 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks to everyone for the great input!
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script to search and copy jpg to Another Folder. ajayan Linux - Newbie 9 09-23-2012 06:46 AM
Bash Script to Copy Modification Date from a file to his folder pjgm Programming 12 07-31-2011 08:33 AM
Bash script to copy specified extension file from a directory? sokha Programming 11 03-15-2010 02:51 AM
bash script to copy a file from the internet AgentD Programming 3 01-23-2009 11:29 PM
problem with a simple script bash stockerz Linux - Newbie 1 09-21-2004 11:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:21 AM.

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