Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 07-25-2013, 04:21 PM   #1
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.


if ! fuser $FILESRC/folder/*.*
chown user:group $FILESRC/folder/*.*
mv $FILESRC/folder/*.* $FILEDST/folder

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
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.

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:
Old 07-25-2013, 11:54 PM   #3
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
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
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:47 PM.

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