Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move file to another directory once it is done transferring

I have a video encoding script that I would like to run as soon as a file is moved into a specific directory.

If I use something like inotify, how do I ensure that the file isn't encoded until it is done moving?

I've considered doing something like:

  • Copy (rsync) file into a temporary directory.
  • Once finished, move (simple 'mv') into the encode directory.
  • Have my script monitor the encode directory.

However, how do I get step #2 to work properly and only run once #1 is complete?

I am using Ubuntu Server 11.10 and I'd like to use bash, but I could be persuaded to use Python if that'd simplify issues.

I am not "downloading" files into this directory, per se; rather I will be using rsync the vast majority of the time.

Additionally, this Ubuntu Server is running on a VM.

I have my main file storage mounted via NFS from a FreeBSD server.

like image 670
javanix Avatar asked Apr 15 '12 16:04

javanix


People also ask

How do I move a file from one directory to another directly?

Use the mv command to move a file from one location to another. To move a file on a computer with a graphical interface, you open the folder where the file is currently located, and then open another window to the folder you want to move the file into. Finally, you drag and drop the file from one to the other.

How do I move files rather than transfer?

To move a file, hold down the Shift key while dragging. You can also use the middle mouse button to drag files. In this case, gThumb will ask you if you wish to copy the files, move the files, or cancel the operation. Select the files to be transferred, right-click on the selection, and choose Copy to... or Move to....

When you drag and drop a file into a folder within the same drive?

How do I Drag and Drop? By default, if you left-click and HOLD the left mouse or touchpad button while moving your mouse pointer to a different folder location on the same drive, when you release the left mouse button the file will be moved to the new location where you released the mouse button.

What is the difference between moving and copying file and folder?

Difference between copying and moving files / foldersCopying – make a duplicate of the selected file or folder and place it in another location. Moving – move the original files or folder from one place to another (change the destination). The move deletes the original file or folder, while copy creates a duplicate.


1 Answers

The easiest way would be to download using a program like wget or curl that doesn't exit until the file is done downloading. Failing that, I'm not sure if it's the absolute best solution, but you could use a script that loops, checking whether the file is open using lsof and grep:

while lsof | grep /path/to/download >/dev/null; do sleep 5; done
mv /path/to/download /path/to/encode/dir
like image 189
Kevin Avatar answered Sep 25 '22 10:09

Kevin