find, make and move file based on username
Category: UNIX-Unix Beginner
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,
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 $(
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
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.
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.
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
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.
