Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

3 Answers 3

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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