Results 1 to 7 of 7
Hi everyone. I have a question to experienced people (I am beginer in Linux). I had to write a script that could "Compare two directories (if user at that moment ...
- 10-29-2009 #1Just Joined!
- Join Date
- Oct 2009
- Posts
- 4
Change the way to display the files after comparisson of directories
Hi everyone. I have a question to experienced people (I am beginer in Linux). I had to write a script that could "Compare two directories (if user at that moment is in one of them), display the resulte like "Files that are in . but not in /home/.././" and "Files that are in /home/.././but not in . " But the output should be without pathway, just names of files. I can't do that, in my script for display output I have a string ls -l $1/$file . Please, any suggestion? I would appreciate!
- 10-29-2009 #2
Check out the basename command.
Debian GNU/Linux -- You know you want it.
- 10-29-2009 #3
You can skip the use of basename if you go to one of the two directories... You're doing a set comparison of two directories' filenames, (a-b) - so change directory to the one containing the files "a", get all those filenames (without decoration) using "*" and then search for those names in "b"...
Code:#!/bin/bash # When you invoke this script with the name of another directory # as an argument, it will print out all the filenames that are in the # current directory but not in the one specified on the command line. for f in *; do if [ ! -e "$1/$f" ]; then echo $f; fi; done
- 10-29-2009 #4Just Joined!
- Join Date
- Oct 2009
- Posts
- 4
Thank you!!!! Very useful. It works!
- 10-29-2009 #5Just Joined!
- Join Date
- Oct 2009
- Posts
- 4
Sorry, people, I , actually get stuck....
the simple script:
for file in $(ls-a $1)
do
if [ ! -e $2/$file ]
ls -a $1/$file
fi
done
for file in $(ls -a $2)
if [ ! -e $1/$file ]
ls -a $2/$file
fi
done
If I have 2 directories, I compare them and need a record (all files including hidden) : "Files that in dir1 but not in dir2" and "Files that in dir2 but not in dir1". I start my script form directory of another user and compare the current directory with one of other user's directory. For exemple: (scriptname) . ~user/A1/dir2
I did script, similar in above one, it works, I have my record...But the files are with absolute path, I tried everything what I learnd, no way. I need just names of files without path... Please, help.
- 10-29-2009 #6
There are various problems here. First off, if you say something like "ls -a /bin" you'll get back filenames beginning with "/bin". Therefore $file already begins with $1. Second, your script is not whitespace-safe: if you have filenames with whitespace in them then the for loop will break - or if your directory name has whitespace, then your "if" tests or "ls" commands may break. This was one advantage of using a straight glob in my version as opposed to running a subcommand. If a directory contains the files ./a, ./"b 1", and ./c, then this command:
will result in $f taking the values "a", "b 1", and "c". But if you do it this way:Code:for f in *
Then $f will take the values "a", "b", "1", and "c". This is because the whole output of ls is turned into a single string by the shell, and then split on whitespace (not even on newlines... on whitespace...) When you just stick a file glob in your for loop, however, each matching filename is passed to the loop as if it were a single quoted argument.Code:for f in $(ls .)
But to get off this sidetrack: the basic problem here is that your $file variable already has the $1 path prefixed on it (which you don't want) - and then you add that prefix again when you do the "ls" command ("ls $1/$file"...) What was so wrong with the code that I posted that you had to go and break it? XD
Well, I guess since you're using "ls -a" instead of just "ls", you needed to include dotfiles in the results - which mine didn't. You could fix that by saying "for f in * .*" instead of "for f in *"... Changing to the directory from which you're taking values for $file is an easy way to avoid having leading paths stuck on - but if that doesn't work for you, look into "basename" like GNU-fan said...
- 10-29-2009 #7Just Joined!
- Join Date
- Oct 2009
- Posts
- 4
Thanks for help. I changed the code ( for f in * .*), part 1 works, I have the output what I neded, but the second part (with second directory, where I have to print the report about files that are in second directory but not in first) it does't work, actually I don't have any output for this part...

Ok, the output should be (including error messeges in case of wrong name of directory) like:
The files that in . but not in ~user/A1/dir2
-rw-r--r-- 1 user 0 2009-10-29 15:06 file1
-rw-r--r-- 1 user 0 2009-10-29 15:06 file2
The files that in ~user/A1/dir2 but not in .
-rw-r--r-- 1 user 0 2009-10-29 15:06 file5
-rw-r--r-- 1 user 0 2009-10-29 15:06 file6
Plus error messages (d2 is not a valid existing directory or d1 is not valid existing directory, or if there 3 directory for comparisson, then error messege too)
The fille3 and file4 (for example) are in both directories and we don't have to mention them.


Reply With Quote
