Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: Linux Homework

I need the command to find all the 2 letter commands like rm, ls, cp, mv in all the diectories that ends with 'bin'.

I have found those directories using the command
ls -alR / 2>/dev/null | grep '^d' | gerp 'bin$'

Please help.
Posted 22-Nov-12 20:13pm
Edited 23-Nov-12 3:19am
Sergey Alexandrovich Kryukov at 23-Nov-12 2:07am
Sorry, I feel that this is not quite a topic adequate to the site devoted to software development. Manipulations with bash commands, even tricky, is not quite programming... this is still user's level...
srmohanr at 23-Nov-12 2:10am
Please tell me whether
find / -type f -name "??" | grep '/*bin/' 2>/dev/null, will work correctly
Richard MacCutchan at 23-Nov-12 6:43am
Why don't you try it for yourself?
srmohanr at 23-Nov-12 7:47am
I am not having terminals with me. Please help
Richard MacCutchan at 23-Nov-12 7:57am
Neither am I. If you don't have access to a Unix/Linux system then why do you need this command?
srmohanr at 23-Nov-12 8:05am
this for my college assignment.
Richard MacCutchan at 23-Nov-12 8:19am
In that case your teacher will be expecting you to do the work for yourself by reading your study guides and notes.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

We are developers, after all (error handling left to the reader):
/* finds recursively files having 2-letters name in directories whoose name ends with 'bin'
    rd [<DIRNAME>]
  on <DIRNAME> missing, current directory is used
<h1>include <string.h></h1>
<h1>include <stdio.h></h1>
<h1>include <sys/types.h></h1>
<h1>include <unistd.h></h1>
<h1>include <dirent.h></h1>
<h1>define BUFSIZE 0x400</h1>
int has_slash(const char * buf, size_t len);
int rd(const char * nam, int check)
  char buf[BUFSIZE];
  DIR * d = opendir(nam);
  if (d)
    struct dirent *entry;
    while   ((entry = readdir(d)) != NULL)
      if ( check && entry->d_type == DT_REG )
        if (strlen(entry->d_name)==2)
          printf("%s/%s\n", nam, entry->d_name);
  if (entry-&gt;d_type == DT_DIR)
    int check;
    int len;
    strcpy( buf, nam );
    if ( ! has_slash(nam, strlen(nam)))
      strcat(buf, &quot;/&quot;);
    strcat(buf, entry-&gt;d_name);
    len = strlen(entry-&gt;d_name);
    check =  ! strncmp( &amp;entry-&gt;d_name[len-3], &quot;bin&quot;, 3);
    if ( strcmp(&quot;.&quot;, entry-&gt;d_name) &amp;&amp; strcmp(&quot;..&quot;, entry-&gt;d_name))
      rd(buf, check);

return 0;

int has_slash(const char * buf, size_t len)
if ( len < 1) return 0;
if ( buf[len-1] == '/' ) return 1;
return 0;

int is_bin(const char * buf, size_t len)
if ( has_slash( buf, len) )
if ( len < 3)
return 0;
else if ( len == 3 )
if ( ! strncmp(buf, "bin",3))
return 1;
if ( !strncmp(&buf[len-4], "/bin", 4))
return 1;
return 0;
int main (int argc, char * argv[])
char buf[BUFSIZE];

if (argc > 1)
strncpy(buf, argv[1], BUFSIZE-1);
buf[BUFSIZE-1] = '\0';
getcwd(buf, sizeof(buf));

rd( buf, is_bin(buf, strlen(buf)));

return 0;
output example:
$ ./rd /

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You already asked this question here[^] and I gave you a suggestion to get started. Have you looked at the find command and read the man pages for it? How about grep and awk?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 475
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 290
3 ProgramFOX 265
4 CHill60 200
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210

Advertise | Privacy | Mobile
Web01 | 2.8.150331.1 | Last Updated 24 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100