I am new in bash scripting and I have to write a script that finds the newest .mpg file in directory and then sends it to a ftp server. So far I found command that find my file.
#! /bin/bash
find /home/user/directory -type f -mmin -50 -name *.mpg \( ! -regex ".*/\..*" \)
This works but I can’t figure out how to send the output of that command to ftp server. I’ve read that I have to use variables, but I can’t understand them so far.
Use can pipe the output from one command as the input to another with
|A simpler way to find the latest mpg file:
If you want to send multiple files xargs will be needed i.e for the 5 newest files
lslists the files in the/home/user/directory/*.mpgwhere*is expanded to any filename where with the.mpgextention.-ttellslsto list in time order and-ris reverse sort as we want the newest first not the oldest. Theheadcommand is used to only show the number of results we want like one-1or five-5.headis usually used for viewing only a certain number of lines in a file, to view the first line in a file we would runhead -1 file.txt.Pipe example:
grepis used to search for text in files/stdoutSo if you wanted to know if
firefoxis contained in the first line offile.txtwe wouldpipethe output ofhead -1 file.txttogrep firefoxhead -1 file.txt | grep firefoxYou can pipe multiple commands together to achieve the result you want.