How to find duplicate commands in the search path?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find duplicate commands in the search path?
# 1  
Old 03-10-2010
Question How to find duplicate commands in the search path?

I wanted to see if there is any duplicate of a specific command in the command search path. The following code will list all copies of "openssl" in the command search path.

Code:
find `printenv PATH | sed "s/:/ /g"`  -maxdepth 1  -name openssl

However, the above code would fail if the search path contains any directory whose name contains a space such as "my commands". The main reason for the failure is that a space is the delimiter in the list of directories for the "find" command.

The following workaround changes the delimiter from a space to a colon by modifying the global variable IFS.
Code:
IFS=":"; find `printenv PATH`  -maxdepth 1  -name openssl; IFS=$' \t\n';

However, I do not want to modify global variables. Can anyone suggest me any better methods? The method must work even if a directory name contains a space.

Thanks in advance.

Last edited by radoulov; 03-10-2010 at 05:44 AM.. Reason: Please use code tags!
# 2  
Old 03-10-2010
blimey!
as you can see it works!

(the parentheses ensure that you don't clobber your IFS variable)
Code:
cat:$(IFS=:;echo $PATH|fmt -1| sort| uniq -c)
      1 /bin
      3 /home/mbillingham/bin
      2 /home/mbillingham/w/ntools/scripts
      2 /sbin
      1 /usr/bin
      1 /usr/games
      1 /usr/local/bin
      2 /usr/sbin

you can substitute uniq -d just to show duplicates.
# 3  
Old 03-10-2010
If I understand correctly, you can use something like this to remove any duplicate entries from your PATH:

Code:
PATH="$(perl -e'print join ":", grep !$_{$_}++, split ":",$ENV{PATH}')"

With zsh all you need is:

Code:
typeset -U PATH

Edit: I just saw that you want to find the duplicates, so:

Code:
perl -le'print join $/, grep $_{$_}++==1, split ":",$ENV{PATH}'


Last edited by radoulov; 03-10-2010 at 06:05 AM..
# 4  
Old 03-10-2010
or did you mean this?
Code:
( IFS=:;find  $PATH|fmt -1| sort| uniq -c )

again uniq -d may be what you want
# 5  
Old 03-10-2010
Question

Sorry for the confusion, but I was looking for duplicate commands (or duplicate executable files) but NOT duplicate directories.

For example, while the authentic copy of the executable file "openssl" resides in /usr/bin/, another copy of "openssl" may reside in /sbin/ by accident or by malicious intention. I would like to detect whether multiple copies of an executable file with the same name exist in different directories.

To simplify the problem, assume that I am looking for files whose name is "openssl". The search for "openssl" should be done through the directories listed in PATH (the command search path). I do not want the search to waste time in looking into directories that are not listed in PATH.

I would like the search method to meet the following two conditions. First, the search has to work even if a directory name contains a space. Second, I prefer not to modify the global variable IFS.

I showed two codes in Post #1. The first code met the second condition, but failed in the first condition.

find `printenv PATH | sed "s/:/ /g"` -maxdepth 1 -name openssl

The second code met the first condition, but failed in the second condition.

IFS=":"; find `printenv PATH` -maxdepth 1 -name openssl; IFS=$' \t\n';

I am not sure if there is any elegant method to accomplish this task without modifying IFS. However, if anyone can think of such a method, let me know.

Thanks in advance.
# 6  
Old 03-10-2010
Hi,

If you're using bash:
Code:
type -a name

will list all commands named "name" in your PATH along with any identically named built-ins, functions, and aliases.

Alister
# 7  
Old 03-10-2010
I believe that this small program, written by Randal L. Schwartz, could solve your problem.

Last edited by radoulov; 03-10-2010 at 02:18 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find duplicate values in specific column and delete all the duplicate values

Dear folks I have a map file of around 54K lines and some of the values in the second column have the same value and I want to find them and delete all of the same values. I looked over duplicate commands but my case is not to keep one of the duplicate values. I want to remove all of the same... (4 Replies)
Discussion started by: sajmar
4 Replies

2. UNIX for Dummies Questions & Answers

Find commmand returning search path with the result set

OS Platform : Oracle Linux 6.5 We are creating a shell script to purge old log files . It uses find command with rm in it. The syntax is find <Path of Log Directory> -exec rm -fr {} \; Example: find /tmp/test3 -exec rm -fr {} \; For rm command , we use -r option to... (4 Replies)
Discussion started by: kraljic
4 Replies

3. Shell Programming and Scripting

Search pattern on logfile and search for day/dates and skip duplicate lines if any

Hi, I've written a script to search for an Oracle ORA- error on a log file, print that line and the .trc file associated with it as well as the dateline of when I assumed the error occured. In most it is the first dateline previous to the error. Unfortunately, this is not a fool proof script.... (2 Replies)
Discussion started by: newbie_01
2 Replies

4. Shell Programming and Scripting

Search duplicate field and replace one of them with new value

Dear All, I have file with 4 columns: 1 AA 0 21 2 BB 0 31 3 AA 0 21 4 CC 0 41 I would like to find the duplicate record based on column 2 and replace the 4th column of the duplicate by a new value. So, the output will be: 1 AA 0 21 2 BB 0 31 3 AA 0 -21 4 CC 0 41 Any suggestions... (3 Replies)
Discussion started by: ezhil01
3 Replies

5. Shell Programming and Scripting

Find duplicate based on 'n' fields and mark the duplicate as 'D'

Hi, In a file, I have to mark duplicate records as 'D' and the latest record alone as 'C'. In the below file, I have to identify if duplicate records are there or not based on Man_ID, Man_DT, Ship_ID and I have to mark the record with latest Ship_DT as "C" and other as "D" (I have to create... (7 Replies)
Discussion started by: machomaddy
7 Replies

6. Solaris

duplicate PATH entries

I noticed we have duplicate (or even triple) entries in PATH env variable. Is there any reason to have them all or is it safe to leave only one? thanks. (2 Replies)
Discussion started by: orange47
2 Replies

7. Shell Programming and Scripting

Executing Commands From Non-Standard Path (Changing user's PATH secretely???)

Hi: I have a requirement as below: I have some standard Unix commands modified and kept them in a directory say /usr/clsh/bin. For example I have a script named "ls" kept here which is modified version of "ls" (say it always gives long listing i.e. ls -l). When any user logs on and types... (2 Replies)
Discussion started by: ramesh_samane
2 Replies

8. Shell Programming and Scripting

To remove date and duplicate rows from a log file using unix commands

Hi, I have a log file having size of 48mb. For such a large log file. I want to get the message in a particular format which includes only unique error and exception messages. The following things to be done : 1) To remove all the date and time from the log file 2) To remove all the... (1 Reply)
Discussion started by: Pank10
1 Replies

9. UNIX for Dummies Questions & Answers

Question about Restricting Search path of FIND to current directory

Hi, By default FIND command searches for matching files in all the subdirectories within the specified path. Is there a way to restrict FIND command's search path to only the specified directory and NOT TO scan its subdirectories. Any help would be more than appreciated. Thanks and Regards (2 Replies)
Discussion started by: super_duper_guy
2 Replies

10. Shell Programming and Scripting

Removing duplicate files from list with different path

I have a list which contains all the jar files shipped with the product I am involved with. Now, in this list I have some jar files which appear again and again. But these jar files are present in different folders. My input file looks like this /path/1/to a.jar /path/2/to a.jar /path/1/to... (10 Replies)
Discussion started by: vino
10 Replies
Login or Register to Ask a Question