Script to move only files not being used using fuser.
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Script to move only files not being used using fuser.
Hi all,
New to these forums but figured this would be a good jump off point for my question.
I'm trying to create a script that will check to see if all files in a folder are in use. If they are in use, ignore it; if they're not in use, change their ownership and move them to another directory.
if ! fuser $FILESRC/folder/*.*
then
chown user:group $FILESRC/folder/*.*
mv $FILESRC/folder/*.* $FILEDST/folder
fi
When executing the script it will tell me that there's an open file but won't proceed with the rest of the commands. Can anyone help with what's wrong in my script?
- shell scripts usually start with a hash bang line (you know: '#!/bin/whatevershell'),
- debug your shell script by using debug switches (have 'set -vx' as the second line in BASH),
- selecting files this way is not good: easier to use 'find', ensure output is white space safe, use a "while" loop, run 'fuser' on each file inside the loop and use the exit value ('$?') of the fuser command ('>/dev/null 2>&1' output) to select actions,
- shell scripts usually end with an exit line (you know: 'exit 0').
One more way to do it is using lsof but yes you have to use a loop, while would be good here as stated above and input as filenames in the directory then do a
Code:
lsof | grep [filename]
and compare the exit status if the file is being read or opened $? will be 256(1) otherwise zero.
move the dir
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.