Newbie question: if[command not null]

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Newbie question: if[command not null]
# 1  
Old 04-07-2011
Newbie question: if[command not null]


i have to put in my script a command that should tell me if the contents of two different paths are the same.

I thought to write an "if" command who makes the diff of two files which contains the `ls` of the folders and go on with the script if is not null, but i'm afraid of the fact that the diff is not null if the order of the files is different.

Please,can someone write me the best if command to compare the folders?
# 2  
Old 04-08-2011
Contents of paths, that means all (dir, file, link, device, FIFO) entry names in the subtree, permissions, flat file contents, link counts, modified dates, access dates -- well, they are probably never the same unless you can access both trees identically in each second. Diff two dirs does some of this. ls goes in alpha order, if I remember the man page right. You can always sort. find might be better than ls, providing relative paths of every entry name. cmp can compare file contents, even if binary, one file at a time. So, the first trick is good requirement writing! Smilie
# 3  
Old 04-09-2011
I'd try:
cd /path/to/dir1 
find . | sort > /path/outside/of/here/log1
cd /path/to/dir2 
find . | sort > /path/outside/of/here/log2
d="`diff /path/outside/of/here/log{1,2}`"
if [ -z "$d" ] ; then   #is $d empty?
  echo "Dirs are the same"
  echo "Dirs difer in these: " 
  echo $d

Sorting because, even if the file subtrees contain same files, find may output them in different order.

Last edited by mirni; 04-09-2011 at 05:38 AM..
# 4  
Old 04-09-2011
You can use diff recursively on two directories and use the exit status to discern whether the paths are identical (special device files and the like excepted).
if diff -r dir1 dir2 >/dev/null; then
    echo identical
    echo different


Last edited by alister; 04-09-2011 at 12:30 AM..
# 5  
Old 04-11-2011
Uses multiple CPUs and finds all flat file differences, even binary files:
comm -3 <(
  cd dir1
  find * -type f | xargs -n999 cksum | sed 's/\(.*\) \(.*\)/\2 \1/' | sort
 ) <(
  cd dir2
  find * -type f | xargs -n999 cksum | sed 's/\(.*\) \(.*\)/\2 \1/' | sort
 ) |sed '
  s/^\t/dir2 /
  s/^/dir 1 /

This User Gave Thanks to DGPickett For This Post:
# 6  
Old 04-11-2011
That's a cool solution. I like the cksum part.
Just a little modification to deal with spaces in files:
find * -type f -print0 | xargs -0 -n999 cksum

# 7  
Old 04-12-2011
I never usek find -print0, since I can control that with start dir arg(s).

Here, it messes up the comm, making identical files on identical relative paths show different (all show different).

The xargs -0 option is nice when the input is lines of badly behaved file names. Some have hidden their borrowed space using 'mkdir ". " ' to make a hidden directory that is a little hard and scary to remove. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Newbie PATH command question...

Still trying to pick up speed on the command line in OSX. I have installed Apache, and some other server software, but am having problems getting my install of Perl to work. I feel like it's because my Apache install is looking for the base (built-in) Perl that came with OSX which is 5.10. I... (4 Replies)
Discussion started by: Bridger
4 Replies

2. Shell Programming and Scripting

Question on NULL and zero value of variable

Hi all, I have a stupid question on NULL and zero(0). In a script I've been working with, one of the lines is: if && then The problem I seem to have is when $Current_csm2 is null, this if block is not triggered, and I don't get why because I was under the impression that NULL!=0 Can... (7 Replies)
Discussion started by: spynappels
7 Replies

3. UNIX for Dummies Questions & Answers

newbie question

Hi all, I am sure this is very simple but I cant quite get it. I am trying to search textfile1.txt for a string then take the results of the search and append the result to textfile3.txt So far I have used $ find file1.txt -exec grep "string i am looking for" '{}' \; -print this... (2 Replies)
Discussion started by: radgator
2 Replies

4. Shell Programming and Scripting

perl newbie . &&..programming newbie (question 2)

Hello everyone, I am having to do a lot of perl scripting these days and I am learning a lot. I have this problem I want to move files from a folder and all its sub folders to one parent folder, they are all .gz files.. there is folder1\folder2\*.gz and there are about 50 folders... (1 Reply)
Discussion started by: xytiz
1 Replies

5. UNIX for Dummies Questions & Answers

UNIX newbie NEWBIE question!

Hello everyone, Just started UNIX today! In our school we use solaris. I just want to know how do I setup Solaris 10 not the GUI one, the one where you have to type the commands like ECHO, ls, pwd, etc... I have windows xp and I also have vmware. I hope I am not missing anything! :p (4 Replies)
Discussion started by: Hanamachi
4 Replies

6. Programming

Question about NULL Character & fgets()

Assume client send the message " Hello ", i get output such as Sent mesg: hello Bytes Sent to Client: 6 bytes_received = recv(clientSockD, data, MAX_DATA, 0); if(bytes_received) { send(clientSockD, data, bytes_received, 0); data = '\0';... (2 Replies)
Discussion started by: f.ben.isaac
2 Replies

7. UNIX for Dummies Questions & Answers

Newbie question?

What is the best way to learn UNIX on the web, with out buying books? any link would be much help. Thank you in advance, L (1 Reply)
Discussion started by: lsoria1
1 Replies

8. Shell Programming and Scripting

newbie question

hey all, I have repeatedly seen scripts containing the following syntax, grep "hello" $myfile >> $log 2>&1 can anyone explain exactly what "2>&1" mean? THANK YOU (4 Replies)
Discussion started by: mpang_
4 Replies

9. Shell Programming and Scripting

Newbie question

Hello, I have text file while looks this test1 test2 test3 test4 test5 test6 and if I want to parse it and make new file which would like this test1 test2 test3 test4 test5 test6 How can I do this in korn shell script Thanks (9 Replies)
Discussion started by: peeyush_23
9 Replies

10. UNIX for Dummies Questions & Answers

/dev/null 2>&1 question

Hi, suppose you have the following line at your crontab : 5 * * * * /usr/mine/script > /dev/null 2>&1 now i understood that the " > /dev/null 2>&1 outputs both Standard outpout and Standard Error messages to the /dev/null device or file... the first part , " > /dev/null " transfers... (1 Reply)
Discussion started by: BAM
1 Replies
Login or Register to Ask a Question

Featured Tech Videos