LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-04-2009, 03:56 AM   #1
johnh10000
Member
 
Registered: Nov 2008
Distribution: Ubuntu Lucid Lynx
Posts: 541

Rep: Reputation: 33
bash script


Hi gang, a nice easy one I think!

i have found out that

ls -t | head -n 1

will produce the file in the directory that is the newest.

Now I have a cron job to delete my webcam snaps every hour.

What I need is it to delete everything except the last one and a link to it.

...So how can I use ls -t | head -n 1 in another command?

ie: cp "ls -t | head -n 1" bkup

then I can carry on. Putting ls output into a varible would be fine too
 
Old 10-04-2009, 04:07 AM   #2
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
This will show you the last one

Code:
ls -l --time-style="+%Y%m%d%H%M%S"  | sort -k 6 | tail -1
This will show you everything but the last one
Code:
ls -l --time-style="+%Y%m%d%H%M%S"  | sort -k 6 | head -n -1
 
Old 10-04-2009, 04:12 AM   #3
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Quote:
Originally Posted by johnh10000 View Post
ie: cp "ls -t | head -n 1" bkup
Hi johnh10000,

Hope u have to use: (back quotes instead of double quotes)

cp `ls -t | head -n 1` bkup
 
Old 10-04-2009, 04:15 AM   #4
johnh10000
Member
 
Registered: Nov 2008
Distribution: Ubuntu Lucid Lynx
Posts: 541

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by Admiral Beotch View Post
This will show you the last one

Code:
ls -l --time-style="+%Y%m%d%H%M%S"  | sort -k 6 | tail -1
This will show you everything but the last one
Code:
ls -l --time-style="+%Y%m%d%H%M%S"  | sort -k 6 | head -n -1
As I have just descovered the redirection operator can I do something like
:
Code:
cd /home/johnh10000/intranet/htdocs/webcam

tar cvf - *.jpg > $archive.tar
gzip $archive.tar
echo "Webcam archive file \"$archive.tar.gz\"."
then
Code:
rm -ls -l --time-style="+%Y%m%d%H%M%S"  | sort -k 6 | head -n -1
I know the last one won't work, but you get the idea?
 
Old 10-04-2009, 04:17 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Look at the xargs command. If the filenames may contain white space, then you need to pipe it through the "tr" command and use xarg's -0 argument.

eg: ls -t | head -n 1 | tr '\n' '\0' | cp -t bkup

However, your for example line doesn't do what you say you want to accomplish.

If there aren't too many files, you can use a list of files to supply arguments to a for loop:

for file in $(ls -t | head -n 10); do
#do something with "${file}"
done
 
Old 10-04-2009, 04:19 AM   #6
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
You'll have to use cut to slice out just the filename from the line. And as Vinaytp was illustrating, you'll either need to back-tick that command into your rm statement or wrap it in $().

For instance, consider:

ls *.jpg
echo *.jpg
ls $(echo *.jpg)
 
Old 10-04-2009, 04:44 AM   #7
johnh10000
Member
 
Registered: Nov 2008
Distribution: Ubuntu Lucid Lynx
Posts: 541

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by jschiwal View Post
Look at the xargs command. If the filenames may contain white space, then you need to pipe it through the "tr" command and use xarg's -0 argument.

eg: ls -t | head -n 1 | tr '\n' '\0' | cp -t bkup

However, your for example line doesn't do what you say you want to accomplish.
theentire job is as follows:

cron starts the script
archive this hours jpgs preff execluding the link
delete all jpgs except the most recent and the link
 
Old 10-04-2009, 04:56 AM   #8
johnh10000
Member
 
Registered: Nov 2008
Distribution: Ubuntu Lucid Lynx
Posts: 541

Original Poster
Rep: Reputation: 33
cp $(ls -t | head -n 1) last.jsh

thanks, now i can do a command in something, i recon i can do it

thanks
 
Old 10-04-2009, 05:09 AM   #9
johnh10000
Member
 
Registered: Nov 2008
Distribution: Ubuntu Lucid Lynx
Posts: 541

Original Poster
Rep: Reputation: 33
Code:
#!/bin/bash
#cd /home/johnh10000/intranet/htdocs/webcam

cp $(ls -t | head -n 1) last.jsh

tar cvf - *.jpg > ./bkup/hour.tar
gzip ./bkup/hour.tar
echo "Webcam archive file \"hour.tar.gz\"."
rm *.jpg
cp last.jsh last.jpg
well with you guys help here it is!
Anyone got any ideas as to put a hourly date stamp on the tgz filename?
the script will run every hour.

thanks
 
Old 10-05-2009, 02:36 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
hr=`date +%H`

man date
 
Old 10-05-2009, 03:16 AM   #11
johnh10000
Member
 
Registered: Nov 2008
Distribution: Ubuntu Lucid Lynx
Posts: 541

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by chrism01 View Post
hr=`date +%H`

man date
cheers, so if i've understand everything

Code:
tar cvf - *.jpg > ./bkup/$(date +%x%X)
gzip ./bkup/$(date +%x%X)
should work? I haven't got a book on bash yet. A visit to the library
is planned later. Unless you know of a good pdf or something.

how would I add a litteral?


tar cvf - *.jpg > ./bkup/($(date +%x%X)+'.tar')
 
Old 10-05-2009, 02:13 PM   #12
johnh10000
Member
 
Registered: Nov 2008
Distribution: Ubuntu Lucid Lynx
Posts: 541

Original Poster
Rep: Reputation: 33
new problem

#!/bin/bash
# file test.text exists
cp test.text $(date +%X%x).txt

##johnh10000@tux:~/bin$ ./tst1
##cp: cannot create regular file `19:03:0805/10/09.txt': No such file or directory

for a test, i want to cp test.text -> 19:03:0805/10/09.txt

now looking at it it's probably the / in the date
 
Old 10-05-2009, 02:21 PM   #13
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Rep: Reputation: 30
here's an ugly way to do that to get all the results except the new one:
couldn't remember the exact syntax of doing number-numberb and getting the results, so I've used bc to do it.

ls -t | tail -$(echo `ls | wc -l`-1 | bc)

but u can use something similar to that :
ls -t | tail -{ls | wc -l`-1} --> replace the bold stuff to fit the need.
 
Old 10-05-2009, 04:01 PM   #14
rholme
Member
 
Registered: May 2008
Posts: 47

Rep: Reputation: 15
cp `ls -t | head -n 1` bkup

i.e. back quotes should do it

Another way might be - using 2 commands

LIST=`ls -t | head -n 1`
cp $LIST bkup
 
Old 10-05-2009, 11:54 PM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Don't create a filename with special chars, especially (sic) '/', it's the dir sigil.
Typically, people go for a format YYYYMMDD_HHMM eg

fname_20091006_0905.log

Note the leading zeros. These filenames will sort nicely automatically/naturally.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 05:11 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 11:53 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 07:00 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 08:01 PM


All times are GMT -5. The time now is 07:10 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration