Hi, I am a newbie at Unix scritping, and I have a question.
Looking at the search functionality on Unix. Here I have a structure
root---------dir1 ------- file1, file2, file3
|_____dir2 ______file1@, file4
|_____dir3_______file1@, file5
Under root directory, I have directory dir1, dir2 , and dir3. In dir1, I have file1, file2, and file3. Under dir2, I have a hard link of file1, and file4. Under dir3, I have a symbolic link of file 1, and file5.
My queries are:
" find all the files that under dir1, but not under dir2 and dir3"
" find all the files that under dir2, but not dir3"
"find all the files that in both dir1, dir2, and dir3"
This may sound a bit of naive method, but thats the immediate solution i can think for.
The script below solves your case 1. You can similarly write script for case 2 and 3.
## find all files that are under dir1, but not under dir2 or dir3
for k in `ls dir1`
do
if [ -f dir2/$k -o -f dir3/$k ]
then
echo "$k present in dir2 or dir3, so ignore"
else
echo "list this one $k"
fi
done
hmm, for doesnt work, what shell are you using.
basically in the above script the check
" if [ -f ...."
checks for the existence of a file with that name. To check if the file is of a particular type like a link, directory or character special command, see the man page for "test" and use the appropriate flags. for eg for a symbolic link, you can test it as "-h".
The above script works for k-shell and bash shell too.
if you are very very new to shell scripting I will suggest you to do some reading first.
I use tcsh. I checked and "foreach" instead of "for", "( )" instead of "[ ]" works in tcsh. Anyways, I tried bash, and the script is running now. Thanks for the help.