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 |
This will show you the last one
Code:
ls -l --time-style="+%Y%m%d%H%M%S" | sort -k 6 | tail -1 Code:
ls -l --time-style="+%Y%m%d%H%M%S" | sort -k 6 | head -n -1 |
Quote:
Hope u have to use: (back quotes instead of double quotes) cp `ls -t | head -n 1` bkup |
Quote:
: Code:
cd /home/johnh10000/intranet/htdocs/webcam Code:
|
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 |
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) |
Quote:
cron starts the script archive this hours jpgs preff execluding the link delete all jpgs except the most recent and the link |
cp $(ls -t | head -n 1) last.jsh
thanks, now i can do a command in something, i recon i can do it thanks |
Code:
#!/bin/bash Anyone got any ideas as to put a hourly date stamp on the tgz filename? the script will run every hour. thanks |
hr=`date +%H`
man date |
Quote:
Code:
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') |
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 |
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. |
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 |
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. |