Find, make and move file based on username


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find, make and move file based on username
# 1  
Old 04-18-2007
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.
# 2  
Old 04-19-2007
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
# 3  
Old 04-19-2007
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.
# 4  
Old 04-19-2007
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.
# 5  
Old 04-19-2007
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
# 6  
Old 04-19-2007
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to move specific files to directory based on match to file

I am trying to mv each of the .vcf files in the variants folder to the folder in /home/cmccabe/f2 that the .vcf id is found in file. $2 in file will always have the id of a .vcf in the variants folder. The line in blue staring with R_2019 in file up to the -v5.6 will always be an exact match to a... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

File Move based on date

how to move file based on date in linux (1 Reply)
Discussion started by: syedzoniac
1 Replies

3. Shell Programming and Scripting

Move input file based on condition

Hello, I have File1 in a directory A, a File2 in a directory B. If the File2 is not empty Then I have to move File1 from directory A to a directory archive Else no action. Is it possible to do this from one command line? Thank you in advance for your answers. Madi (2 Replies)
Discussion started by: AngelMady
2 Replies

4. UNIX and Linux Applications

grep file to find unique instances of username

hello - A SystemOut.log file has recurring entries that follow this format: Principal: auth9.nick.al.gov:389/USERNAME Over the course of a day thousands of lines similar to this are produced, with each username represented hundreds of times. I need to create a new file that shows... (4 Replies)
Discussion started by: 1075FJ40
4 Replies

5. Fedora

Move file based time stamp

Hi all, I've already tired to try to solved this problem. Also search in Internet didn't find anything solution I have a directory like this : # pwd /opt/projects/juventini # ls -al | more total 3627460 drwxr-xr-x 2 app apps 12472320 Sep 24 14:59 . drwxr-xr-x 11 app apps 4096 Jun... (8 Replies)
Discussion started by: sunardo
8 Replies

6. Shell Programming and Scripting

move contents from one file to another based on line number or content

I want a script that will move everything beyond a certain line number or beyond a certain content word into another file. For example, if file A has this: first line second line third line forth line fifth line sixth line I want to run a script that will move everything beyond the third... (4 Replies)
Discussion started by: robp2175
4 Replies

7. Shell Programming and Scripting

Move file based on filename

Hi All I need a script to manipulate files based on a filename: example filename: 66600_042706.pdf the script will create a directory 66000 only if this directory is not existing. If that directory is existing it will just move the file to 66000/666000_042706.pdf in addition, i want to... (4 Replies)
Discussion started by: aemestech
4 Replies

8. Shell Programming and Scripting

Need to move a file based on size

Hi All, I have some files in a directory. If the file size is 0 i want to move the file to new directory using unix script. Can anyone help me in this regard Thanks Vinny (4 Replies)
Discussion started by: vinny81
4 Replies

9. Shell Programming and Scripting

File Move Based on 1st Character of File Name

I need some help with a unix script to mv image files to subdirectories based on the 1st character of the filename. See example below... /images/main 1191.jpg 9999.jpg A101.jpg A102.jpg B201.jpg c333.jpg ... Z999.jpg I would like to move to the following: ... (11 Replies)
Discussion started by: srdconsulting
11 Replies

10. Shell Programming and Scripting

To find the username in /etc/passwd file

Hi, I need to a shell script to list out only the username in the /etc/passwd file. Regards Siva (7 Replies)
Discussion started by: gsiva
7 Replies
Login or Register to Ask a Question