Moving specific files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving specific files
# 1  
Old 03-30-2007
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  
Old 03-30-2007
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  
Old 03-31-2007
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

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

# 4  
Old 03-31-2007
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:
Code:
var=/etc/passwd
printf "%s\n" "${var##*/}"

# 5  
Old 03-31-2007
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

2. Red Hat

Moving files with specific dates

Hi, These are the list of files in one directory in the server : # ls -lrt total 10120 -rw-r--r-- 1 root root 4484 Jul 8 2011 install.log.syslog -rw-r--r-- 1 root root 51890 Jul 8 2011 install.log -rw------- 1 root root 3140 Jul 8 2011 anaconda-ks.cfg drwxr-xr-x 2 root root... (2 Replies)
Discussion started by: anaigini45
2 Replies

3. Homework & Coursework Questions

Shell Scripting , Moving Old file to specific folder

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: There are files stored like 14.Aug.2014.log, 15.Aug.2014.log etc. in a folder $HOME/log you need to find out all... (4 Replies)
Discussion started by: shajoftaj
4 Replies

4. Ubuntu

Shell Scripting , Moving Old file to specific folder

There are files stored like 14.Aug.2014.log, 15.Aug.2014.log etc. in a folder $HOME/logyou need to find out all the log files of last 1 month and move them into $HOME/logs/lastmonth/ this should be implemented with reference of file name. ---------- Post updated at 12:30 PM ----------... (3 Replies)
Discussion started by: shajoftaj
3 Replies

5. UNIX for Dummies Questions & Answers

Moving files with specific names

Hi, I have a list of names (in a text file) like this: SRR1234 SRR5678 SRR4321 SRR8876 I'd like to have a unix code to find all the files which have any of above strings in their name and move them to a specific directory. I have my files distributed in many subdirectories so it has to... (3 Replies)
Discussion started by: a_bahreini
3 Replies

6. AIX

Moving Hidden files to normal files

I have a bunch of hidden files in a directory in AIX. I would like to move these hidden files as regular files to another directory. Say i have the following files in directory /x .test~1234~567 .report~5678~123 .find~9876~576 i would like to move them to directory /y as test~1234~567... (10 Replies)
Discussion started by: umesh.narain
10 Replies

7. Red Hat

Moving of file content to another two files after searching with specific pattern

Hello, Please help me with this!! Thanks in advance!! I have a file named file.gc with the content: 1-- Mon Sep 10 08:53:09 CDT 2012 2revoke connect from FR2261; 3delete from mkt_allow where grantee = 'FR2261'; 4grant connect to FR2261 with '******'; 5alter user FR2261 comment... (0 Replies)
Discussion started by: raosr020
0 Replies

8. Shell Programming and Scripting

Finding files with wc -l results = 1 then moving the files to another folder

Hi guys can you please help me with a script to find files with one row/1 line of content then move the file to another directory my script below runs but nothing happens to the files....Alternatively Ca I get a script to find the *.csv files with "wc -1" results = 1 then create a list of those... (5 Replies)
Discussion started by: Dj Moi
5 Replies

9. Shell Programming and Scripting

Copying specific files from remote m/c to specific folders

Hi All, I am trying to rsync some of the latest files from remote m/c to my local linux box. Folder structure in my remote m/c looks like this /pub/Nightly/Package/ROLL/WIN /pub/Nightly/Package/SOLL/sol /pub/Nightly/Package/SOLL/linux Each of the folder contains gzip files which on daily... (0 Replies)
Discussion started by: jhoomsharabi
0 Replies

10. UNIX for Advanced & Expert Users

Moving specific data between databases

Dear All, I have 2 databases, There is a lot of data in both the databases, i would like to move some data from one database to the other. I would like to accept 2 parameters from the user, i.e. emplyee id & dept, on entering the 2 i will unload all the data from the tables to the flat files.... (2 Replies)
Discussion started by: lloydnwo
2 Replies
Login or Register to Ask a Question