hey guys I am writing a bash script that compares 2 directories and lists the contents that are different in each directory. I have figured out how to do this but the only problem is that it lists the file like this:
i am very new to unix and this is actually the first script I have written. I am trying to run the script in multiple directories but it will only let me run it in my home directory. Is there any way to fix it so it runs in other places?
As you are a novice, perhaps you have not grasped UNIX pathing: file locations can be relative to your current directory $PWD, initially $HOME where you log in, but also absolute (starting with /). You can go anywhere with either, but in $PWD you must go up-tree with ../../../ to get near enough to / to start back down the tree.
Start by doing ls -ld on the path components, so if $1/$file1 is /x/yy/zzz.txt, do:
ls -ld / /x /x/yy /x/xx/zzz.txt
to see the directory permissions and file permission. It is columns 2-7. All permissions is rwxrwxrwx or in octal, 777. The first digit/rwx is for you, the second digit is for the file's group (type id to see yours), and the third digit is for others. Here is my home directory, 755, I (dgp) can do all three but group develop and others can only read and execute (in directories, execute means search/traverse but not display=r read, i.e., permission to go downstairs but not to look around)
$ id
uid=3080(dgp) gid=6900(develop)
$ ls -ld .
drwxr-xr-x 25 dgp develop 4096 Oct 29 11:07 .
$
ok thanks for your help. Just still one thing I am not grasping. is in my directoy I can execute the script, which is called "dirfile".
so when I go dirfile test test2
(test and test2 are directory names)
the script will execute and list the files that are different, but when I go to my teacher's directory and type " dirfile test test2", it tells me "If 'dirfile' is not a typo you can use command-not-found to lookup the package that contains it, like this:
cnf dirfile"
So is there anyway to make this script run in any directory even it the script is not located there?
Changing directory means any files, including executable files, in the old dir are no longer available without a prefix. If you were and /a/b/ and run c, which is /a/b/c, then when you change dir to /a/d/e/, you need to call c as /a/b/c or ../b/c (because /a/ is common, and .. /../ is /a/ when you are in /a/d/e/. I put / on the end of all directory references, for clarity, but you do not need to add them. cd /a/d/e and cd /a/d/e/ both take you to pwd = $PWD = /a/d/e
thanks that worked, now my program runs perfectly using absolute pathnames. But when I run the script like this it doesnt work.
cd ~/user/120/dir1(I go into this directory)
dircompare . ~/user/120/dir2
dircompare is the name of the script. So what I am doing is going into a directory and using . to represent my current directory and then the absolute path to the second directory. The script prints out the first directory files fine but the second directory files dont out the right file. Any idea whats wrong?
output when using absolute pathnames:
Files in /home/user/120/a1dir1 but not in /home/user/120/a1dir2 are:
-rw------- 1 user users 270 2007-01-28 16:07 f2.c
-rw------- 1 user users 418 2007-01-28 16:07 make.socket.c
-rw------- 1 user users 11843 2007-01-28 16:07 prog1
lrwxrwxrwx 1 user users 4 2007-04-30 11:56 s.link -> f2.c
srwx------ 1 user users 0 2007-01-28 16:09 unix.socket
Files in /home/user/120/a1dir2 but not in /home/user/120/a1dir1 are:
-rw------- 1 user users 88 2010-03-14 23:06 file2
-rw------- 1 user users 39 2007-01-28 16:14 .hidden.file
-r-------- 1 user users 135838 2007-01-28 16:21 /phones
-rw------- 1 user users 120 2007-01-28 16:23 prog2.c
drwx------ 2 user users 4096 2007-01-28 16:11 test2.dir
output when using . and absolute:
Files in . but not in /user/120/a1dir2 are:
-rw------- 1 user users 270 2007-01-28 16:07 f2.c
-rw------- 1 user users 418 2007-01-28 16:07 make.socket.c
-rw------- 1 user users 11843 2007-01-28 16:07 prog1
lrwxrwxrwx 1 user users 4 2007-04-30 11:56 s.link -> f2.c
srwx------ 1 user users 0 2007-01-28 16:09 unix.socket
Files in /user/120/a1dir2 but not in . are:
-rw------- 1 user users 88 2010-03-14 23:06 file2
lrwxrwxrwx 1 user users 4 2007-04-30 11:56 s.link -> f2.c
for file in $(ls -a $2)
do
if [ ! -e $1/$file ]
then
cd $2
ls -ld $file
fi
done
there has to be something wrong with this code for the dot but i don't know what.
The parentheses mean the cd moves a subshell but not the parent shell. If you
cd dir1; cd dir2
with relative paths, you are not in dir2, you are in dir1/dir2 -- you are digging down an imaginary tree. Even with absolute paths, for repeatability, it is nice not to move from the starting $PWD. If you
for file in $(ls -a $2)
do
if [ ! -e $1/$file ]
then
(cd $2)
(ls -ld $file)
fi
done
if so now it says this:
ls: cannot access file2: No such file or directory
ls: cannot access .hidden.file: No such file or directory
ls: cannot access phones: No such file or directory
ls: cannot access prog2.c: No such file or directory
ls: cannot access test2.dir: No such file or directory
these are the right files it just cant access them.