Possibly a little incorrect English ahead:

There are at least 2 folders. For this, A contains files without extension, usually usernames, and B contains similar files, with '.yml' extension or possibly without extension, or even other extension, that's not always the case.

How to find out if the files in the A folder exist in B, possibly under a different extension, without using scripting language such as Python ? It'd not be much of an issue for me to make a Python script, but for portability I'd prefer shell scripting, which I suck at. Not to mention I don't know all the commands.

It's only good if this can be done in non-Linux systems too, particularly BSD.

EDIT 0: The difference in name isn't always just the extension, the name could have characters before or after the same text. For example, if A had a file called foobar, it could be named location_foobar in B. Otherwise the name wouldn't be more different, like foo1bar, that's entirely different thing, so the script wouldn't be looking for such differences.

  • Welcome here! What do you mean when you write "possibly under a different extension or even a name"? Do you mean that the file may have a different name in the two folders? You should clarify that point because it's a huge difference to compare files by names and by content. – lgeorget Mar 7 '14 at 23:23
  • Why can't you use a scripting language? Python or Perl should be available almost everywhere. – vonbrand Mar 7 '14 at 23:37
  • @vonbrand: Portability and doing it with tools most basically available. Perl and Python aren't marked mandatory whilst Bash/Dash or other compatible is. – rautamiekka Mar 8 '14 at 0:08
  • what do you mean by "at least"? Can there be C, D, etc. and do they matter? – Alois Mahdal Mar 8 '14 at 0:17
  • I answered assuming that you wanted to match files by name, apparently that's not what you want. Are you looking for files with identical content? Or for hard links? Or are you looking for a name that contains the name in A as a substring (e.g. A/gil matches B/virgil42)? – Gilles 'SO- stop being evil' Mar 8 '14 at 0:19

It's a simple matter to write a shell script that loops over the files of one directory and find if there are files with a superstring as their name in another directory.

for x in "$dirA/"*; do
  set -- "$dirB"/*"$base_name"*
  if [ -e "$1" ]; then
    for y; do
      echo "$base_name found in B as ${y##*/}"
    echo "$x not found in B"

This is portable to any Bourne/POSIX-style shell (sh, ash, bash, ksh, …). Some shell constructs used:

  • #!/bin/sh: the shebang line at the top of every script
  • $1, $2: the two arguments to pass to the script, A and B
  • ${x##*/} is the value of x minus the longest prefix of the form */, i.e. $x minus everything up to the last /, i.e. ${x##*/} is the part of $x after the last /.
  • set -- "B/${x##*/}."* sets the positional parameters to the list of matching files in B. If there are no matches, the pattern is left unexpanded, so this sets the first positional parameter to a string ending in .*.
  • for y; do loops over the positional parameters.
|improve this answer|||||
  • See my EDIT 0 on the OP. – rautamiekka Mar 8 '14 at 0:14
  • Suggestion: Add variables for the folders so that the script doesn't need editing to match the folder names. – rautamiekka Mar 8 '14 at 0:26
  • @rautamiekka I've changed my answer to match your updated question. – Gilles 'SO- stop being evil' Mar 8 '14 at 0:36
  • The script does do what it's asked to. – rautamiekka Mar 8 '14 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.