Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have taken over from someone else a lot of historic data. However the file names are totaly random and badly formatted IMHO.

To start with I want to get a list of all files that contain spaces, or any character other than A-Z, 0-9, ., -, _ is there a way to do this with regex syntax from the command line on Linux ?

share|improve this question
find . -type f | grep -i '[^a-z0-9\.\/_\-]'

This would match all files that contain a character other than a-z, 0-9, a dot, a slash (for directories), an underscore, or a hyphen-minus.

Note: This will not work if a file (or path) contains a newline, because piping to grep would split the file path into two. While not really common, it should be mentioned.

A simple solution with Zsh (it requires setopt EXTENDED_GLOB):

ls -l **/*[^a-zA-Z0-9-_.]##*

The ** recurses into directories, and * matches zero or more occurrences of any character. The negated pattern of whitelisted characters needs to be matched at least once (##).

share|improve this answer

If you have the locate command, you can do something like this:

locate --basename --regex "[^0-9A-Za-z\.\_\-]"

In centos systems, you can install locate by running yum -y install updatedb. You will need to then run the updatedb command and then run locate.

In this command, the basename forces locate to only search the filename and not the full directory path and regex runs the given reg expr on this filename.

share|improve this answer

This will highlight the strange files but will not print only them:

find . -type f -maxdepth 1 | grep --color='auto' [[:alnum:]]

share|improve this answer
Spaces (and others) are part of ASCII. – slhck Sep 11 '13 at 16:07
The question says, “find all files on a folder tree” –– so why did you add -maxdepth 1? – Scott Sep 12 '13 at 22:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .