The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Moving Files iAm4Free Shell Programming and Scripting 5 03-28-2007 05:13 AM
moving between vi files aya_r Shell Programming and Scripting 2 08-23-2005 05:36 AM
Moving specific data between databases lloydnwo UNIX for Advanced & Expert Users 2 10-01-2004 09:09 AM
moving files ?? rocker40 UNIX for Dummies Questions & Answers 9 11-09-2003 08:36 PM
moving only files... sskb UNIX for Dummies Questions & Answers 12 11-19-2001 03:28 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-30-2007
Stud33 Stud33 is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 16
Moving specific files

Hello, I am trying to move specific files to a specific folder. I have a virus script that runs and quarantines the files by changing the ownership to -r--------. This has worked fine but I am wanting to actually move the infected files to a folder called quarantine. I have came up with a basic script but it does not seem to be working properly. I am doing a "find" on the ownership specified above, which is also working fine. I am stuck on the part of moving the files once they have been found. Any help or suggestions is greatly appreciated.

#!/bin/sh

archive=/home/quarantine
cd /home
find . -user root -perm 0400 -type f | while read file; do
dir=`dirname "$file"`
mkdir -p "$archive/$dir"
mv $file "$archive/$dir"
done
  #2 (permalink)  
Old 03-30-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,126
You are using dirname to grab the directory from a path. Now use basename to grab the filename from the path.


$
$
$ basename /etc/passwd
passwd
$ dirname /etc/passwd
/etc
$
  #3 (permalink)  
Old 03-31-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by Perderabo
You are using dirname to grab the directory from a path. Now use basename to grab the filename from the path.

$
$ basename /etc/passwd
passwd
$ dirname /etc/passwd
/etc
$

Where is the basename of the file needed?

if it is, there's no point to using a slow external command when the shell has the neccessary parameter expansion:
var=/etc/passwd printf "%s\n" "${var##*/}"
-->
Code:
var=/etc/passwd
printf "%s\n" "${var##*/}"
        
  #4 (permalink)  
Old 03-31-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,240
Quote:
Originally Posted by cfajohnson

Where is the basename of the file needed?

if it is, there's no point to using a slow external command when the shell has the neccessary parameter expansion:

Except that the OP indicated the use of /bin/sh and didn't give an indication of the OS. In this case I would err on the side of caution and assume the use of the bourne shell, not a POSIX shell.
  #5 (permalink)  
Old 03-31-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by Stud33
Hello, I am trying to move specific files to a specific folder. I have a virus script that runs and quarantines the files by changing the ownership to -r--------. This has worked fine but I am wanting to actually move the infected files to a folder called quarantine. I have came up with a basic script but it does not seem to be working properly. I am doing a "find" on the ownership specified above, which is also working fine. I am stuck on the part of moving the files once they have been found. Any help or suggestions is greatly appreciated.

#!/bin/sh

archive=/home/quarantine
cd /home
find . -user root -perm 0400 -type f | while read file; do
dir=`dirname "$file"`
mkdir -p "$archive/$dir"
mv $file "$archive/$dir"
done

archive=/home/quarantine cd /home find . -user root -perm 0400 -type f | while IFS= read -r file do dir=${file%/*} mkdir -p "$archive/$dir" mv "$file" "$archive/$dir" done
-->
Code:
archive=/home/quarantine
cd /home
find . -user root -perm 0400 -type f |
 while IFS= read -r file
 do
    dir=${file%/*}
    mkdir -p "$archive/$dir"
    mv "$file" "$archive/$dir"
 done
        
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0