Listing files script

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:

-rw-r--r-- 1 joey12 users 0 2010-10-29 16:13 test/file3

I want it to print exactly like this but instead of the test/file3, I just want it to say file3.

Thanks if anyone can help with this.

Have you tried diff?

I like comm for this sort of thing, too.

Cd to the dir in a subshell so the file names are barefoot.

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?

Post your script.

This is how I am outputting the files and it is listing them with the name of the directory and the file name and I just want it to list the file.

Second I would like to no how I can make it run from other directories and not just in my home directory. Is that possible?

ls -ld  $1/$file

To run in other directories requires that the other directories to be other ( or world) r-x
or r-x for the group you are in.

Most system ( e.g. /usr or /opt) directories are readable.

I have the premission it just says that it is not found.

error cnf not found that command.

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 for all your help. I had been listing my files like this

for file in $(ls -a $2)
   do
        if [ ! -e $1/$file ]
            then
                ls -ld $2/$file
        fi
   done

and doing the same thing for $1. Does anyone know How I can fix this so it wont list the path of the file. My output is like this

Files in . 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 . are:
-rw------- 1 user users 88 2010-03-14 23:06 /home/user/120/a1dir2/file2
-rw------- 1 user users 39 2007-01-28 16:14 /home/user/120/a1dir2/.hidden.file
-r-------- 1 user users 135838 2007-01-28 16:21 /home/user/120/a1dir2/phones
-rw------- 1 user users 120 2007-01-28 16:23 /home/user/120/a1dir2/prog2.c
drwx------ 2 user users 4096 2007-01-28 16:11 /home/user/120/a1dir2/test2.dir

I want it to list those files just not the whole path to it. PLZ Help.

( cd $2 ; ls -ld $file )

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

(cd dir1 ) ;( cd dir2)

, you have not moved.

so from this

for file in $(ls -a $2)
   do
        if [ ! -e  $1/$file ]
            then
            cd $2
         ls -ld $file
        fi

I have to get into my $PWD then cd in $2? If so how would the new code look or it would have to be completely different from what I have now.

Put the cd and ls in (). cd changes your working directory, $(pwd) = $PWD, the base of all relative paths.

so like this?

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.