The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need find a file based length J_ang UNIX for Dummies Questions & Answers 8 03-26-2007 05:21 AM
how to move files into different folders based on filename italia5 UNIX for Dummies Questions & Answers 7 08-23-2006 07:04 AM
How would I make a find command NOT show the path of a file? LordJezo Shell Programming and Scripting 1 10-01-2004 03:01 AM
find filename based on file content kollerj UNIX for Dummies Questions & Answers 4 06-02-2001 10:31 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-18-2007
Registered User
 

Join Date: Apr 2007
Posts: 15
Stumble this Post!
Find, make and move file based on username

hi there,
i'm new to UNIX( just 3month used),
i found my new box contained alot of files and directories in /home/box/
i've tried to search script in tis forum and found many of them but, i don't know
how to combine them to make a script, although using pipes.
my tasks are:

1) to scan user dir in /home/user/ - i'm used ls-ld /home/$user/ , $user must be key in.

2) Then, grouping all files in scanned dir(in task 1), into group of extensions such as ".txt",".sh",".dat"
i used the ls -l | awk 'BEGIN {FS="."} {print $2}'>index.txt

3) Based on index.txt, i'm going to make directories referring to those extensions eg.
/home/user/txtExtension
/home/user/datExtension/

then move all of files to their extension dir. i'm sorry, i can't find the way to create this script.

i tried all my best to combine all above simple script but, no.3 is quite hard for me.
is there any UNIX-Shell expert have ideas, solutions, or even example scripts to guide me
Regards,
Helmi.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-19-2007
Registered User
 

Join Date: Mar 2007
Location: Chennai
Posts: 222
Stumble this Post!
Moving Files by Index

I tested the below script by creating user directories under /tmp with different types of file extensions

Code:
#!/bin/ksh
 
 /bin/rm index.dat
 
 set -A USER_HOME  /tmp/UserA /tmp/UserB /tmp/UserC /tmp/Root /tmp/GroupA /tmp/GroupB
 set -A USERS UserA UserB UserC Root GroupA GroupB
 
 for v in   ${USERS[*]} ; do
   find /tmp/$v -type f | awk -F. ' (NF == 2 ){ print $2 }' | sort -ud | tee -a index.dat
 done
 
 for type in $(<index.dat); do
   NewDir=/tmp/${type}-IndexedFileDir
   mkdir -p $NewDir
   find ${USER_HOME[*]} -type f -name "*.$type" -print | xargs -I{} -t mv {} ${NewDir}
 done
Please check the above script by running it on some fake directories and let us know if this doesnot help.

Thanks
Nagarajan Ganesan
Reply With Quote
  #3 (permalink)  
Old 04-19-2007
Registered User
 

Join Date: Apr 2007
Posts: 15
Stumble this Post!
Oh, man...so cool ennstate
Thanks a lot pal!
it all work well ! but there is something i missed at first,
if certain file being removed without "noticing/asking permission to move" the user or root, is it really troublesome for other users to track back and maybe they expected the files had been deleted.

for second time , is there anyone can coupe this problems, maybe, by make a certain tracker log or something?

Regards,
Helmi.
Reply With Quote
  #4 (permalink)  
Old 04-19-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,536
Stumble this Post!
Code:
#!/bin/sh
awk -v user="user1" ' BEGIN {  path = " /home/" user "/";command = "ls" path ;
              while ( (command | getline line)>0 ) {                          
                            if (line ~ /\....$/) {
                                          ext = substr(line,length(line)-2)
                                          Extension[ext]    
                                          files[line] = ext
                            }                            
              }
            }
        END {   makedircmd = "mkdir "
                for ( e in Extension) {
                     print "making " path e                     
                     dir = path e
                     makedircmd = makedircmd dir                                      
                     print makedircmd
                 }
                 #system(makedircmd) #uncomment to use
                 for (f in files) {
                            mvcmd = "mv" path f " " path files[f]
                            traceback = "mv" path files[f] "/" f path 
                            print traceback
                            print traceback >> "backup.sh" #backup
                            #system(mvcmd) #uncomment to use
                 }
        }

' "file"
an attempt in awk. you can use backup.sh to move back those files to original directory.
Reply With Quote
  #5 (permalink)  
Old 04-19-2007
Registered User
 

Join Date: Apr 2007
Posts: 15
Stumble this Post!
I've edited the script at
Code:
user="TEST" ' BEGIN {  path = " /home/rosman/" user "/";command = "ls" path
Where TEST is a testing dir which contained the 1st made by ennstate

Fyi, i don't login as a root, therefore i can't adduser. so, i just made a dummy dir for testing. The error state was
Code:
[rosman]: /home/rosman/TEST => backup.sh
awk: 0602-533 Cannot find or open file file.
 The source line number is 1.
And i assume that it was in the first line of the awk. Any suggestion how to fix this?

Regards,
Helmi
Reply With Quote
  #6 (permalink)  
Old 04-19-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,536
Stumble this Post!
not sure,but you could try
Code:
..
print traceback > "backup.sh" ( single > )
...
FYI, i am using GNU awk. also make sure write access is granted to TEST directory.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:35 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0