LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-759544/)

johnh10000 10-04-2009 02:56 AM

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

JulianTosh 10-04-2009 03:07 AM

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

vinaytp 10-04-2009 03:12 AM

Quote:

Originally Posted by johnh10000 (Post 3706892)
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

johnh10000 10-04-2009 03:15 AM

Quote:

Originally Posted by Admiral Beotch (Post 3706901)
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?

jschiwal 10-04-2009 03:17 AM

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

JulianTosh 10-04-2009 03:19 AM

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)

johnh10000 10-04-2009 03:44 AM

Quote:

Originally Posted by jschiwal (Post 3706911)
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

johnh10000 10-04-2009 03:56 AM

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

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

thanks

johnh10000 10-04-2009 04:09 AM

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

chrism01 10-05-2009 01:36 AM

hr=`date +%H`

man date

johnh10000 10-05-2009 02:16 AM

Quote:

Originally Posted by chrism01 (Post 3707987)
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')

johnh10000 10-05-2009 01:13 PM

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

itz2000 10-05-2009 01:21 PM

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.

rholme 10-05-2009 03:01 PM

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

chrism01 10-05-2009 10:54 PM

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.


All times are GMT -5. The time now is 01:51 PM.