Grep Help

Hey there

I have extracted using ls -lt and tail the last file generated in my webcam directory.

It gives me this result

-rw-rw-rw- 1 TonysiMac staff 505429 Jan 10 10:45 IMG_0006.jpg

How can I now just get the file name extracted i.e. so I am left with just IMG_0006.jpg attached to my variable? I need to pipe this into a move command to replace another file in another location that is going to be uploaded to my web site.

I'll keep looking and I know it's pretty simply but it's been a while since I last did any scripting so it's all a bit hazy.

Thanks a lot

You can use ls -t instead to get just the filename

no that gave me the time, parameters, owner too.

I worked it out with this:

ls -lt /Users/TonysiMac/Public/WebCam/images | tail -1 | awk '{print $9}'

Cheers

Hello,

if you want to extract the file name then use awk

ls -lt | tail | awk '{print $NF}'

mv doesnt read from stdin so you cant read from the pipe by redirecting the stdin as it doesnt make sense.

---------- Post updated at 11:38 PM ---------- Previous update was at 11:37 PM ----------

ls -t does give only the filenames.

Yup all sorted now thanks

IMAGE=`ls -lt /Users/TonysiMac/Public/WebCam/images | tail -1 | awk '{print $9}'`
echo $IMAGE

Now I can move the image. My camera is tethered but takes multiple pictures sequentially and I wanted to find a way to move and rename the latest picture as that was the one I was uploading to my webcam page

#
mv /Users/TonysiMac/Public/WebCam/images/$IMAGE /Users/TonysiMac/Public/WebCam/itworked.jpg

It's amazing how easy everything is when you know how to do it eh.

No it doesn't! If you only want the filename why do a long listing with -l?

And what you've done gets you the oldest file. To get the newest file use head, or ls -ltr.

# ls -ltr | tail -1
-rw-r--r--   1 root  wheel      47 10 Jan 19:00 exports
# ls -tr | tail -1
exports

# ls -tr | tail -1 | xargs -I{} mv {} newfile

# ls -tr | tail -1
newfile

I need the whole file name not just the image. I need image.jpg.

The solution I have seems to work perfectly but always hapy to see better solutions.

find /PATH -type f -print