Find files using ls and wildcards

fileslswildcards

pwd : ../bin under bin I have a directory called datafiles.

Use meta characters and the ls -lL command (with lower and upper case L) to list all filenames under the datafiles directory that contain a dot . with the letter 'f' or 'u' anywhere after the dot.

I tried: ls -lL datafiles *.*f*u

ls -lL datafiles .[f*u]

Thank you for answering. Unfortunately, none of the solutions worked the way I wanted it.

By clarifying the question, /bin/datafiles/file.backup is the root to the directory. From the /bin directory, the command I used is ls -lL datafiles followed by meta characters.

I'm looking for file.backup that has a dot and followed by f or u, not directly followed by f or u (anywhere after the dot), and not ending by f or u.

Best Answer

This looks for files in the data directory that contain a dot followed by f or u but not ending in f or u:

ls -lL datafiles/*.*[fu]*[^fu]

Answer to version 1 of this question

You wrote "f or u". To write that in a glob, use [fu]. To also insist that the f or u be preceded by a dot, try:

ls -lL .*[fu]* *.*[fu]*

The first glob above, .*[fu]*, expands to files that start with a dot (which means that they are normally 'hidden'). The second, *.*[fu]*, expands to files that have a dot somewhere in the name but do not begin with dot.

The hidden files generally serve as configuration files for various programs. If you don't want to see them, and one usually doesn't, then omit the first glob and use simply:

ls -lL *.*[fu]*

More on globs

  • *.*f*u expands to a file name that contains a dot followed (not necessarily immediately) by an f and ends with a u. In other words, this glob requires that both f and u be present in the filename and that they appear in the order f then u.

    Normally, * means zero or more of any character. As a special case, the above does not match files that start with a dot because such files are, by convention, "hidden."

  • .[f*u] expands to a file name that consists of a dot immediately followed by one of the characters f, *, or u (and nothing else after that). Because the star is inside the square brackets, it loses its usual meaning of zero or more characters and becomes a literal *.