LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 07-25-2013, 04:21 PM   #1
mdemers
LQ Newbie
 
Registered: Jul 2013
Posts: 1

Rep: Reputation: Disabled
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.

So far this is what I have.

FILESRC=/path/to/source
FILEDST=/path/to/destination

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?
 
Old 07-25-2013, 05:02 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Welcome to LQ, hope you like it here.

Quote:
Originally Posted by mdemers View Post
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').


# Here's some Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://www.gnu.org/software/bash/man...ode/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/
http://wooledge.org/mywiki/BashFAQ?a...direct=BashFaq
http://wooledge.org/mywiki/BashPitfalls
 
Old 07-25-2013, 11:54 PM   #3
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
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
 
Old 07-26-2013, 08:15 AM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Code:
lsof +D /path/to/directory
will also show what's in use there.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script for Identifying the file and zip all files, move the files to Target Dir gvenkat Linux - Newbie 3 05-07-2011 10:53 AM
Count files in dir then move that many files in a script? chuckycharms Linux - Newbie 3 12-18-2009 11:31 AM
Creating a script to move or copy files into multiple directories below the files matthes138 Linux - Newbie 5 08-25-2009 04:57 PM
Script to Move and rename files scribbl35 Linux - Newbie 1 07-18-2007 09:14 AM
Script to Move files jain_rajesh Linux - Newbie 1 10-15-2004 08:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration