List all files not starting with a number - regex

I want to examine the all the key files present in my /proc. But /proc has innumerable directories corresponding to the running processes. I don't want these directories to be listed. All these directories' names contain only numbers. As I am poor in regular expressions, can anyone tell me whats the regex that I need to send to ls to make it NOT to search files/directories which have numbers in their name?
UPDATE: Thanks to all the replies! But I would love to have a ls alone solution instead of ls+grep solution. The ls alone solutions offered till now doesn't seem to be working!

You don't need grep, just ls:
ls -ad /proc/[^0-9]*
if you want to search the whole subdirectory structure use find:
find /proc/ -type f -regex "[^0-9]*" -print

All files and directories in /proc which do not contain numbers (in other words, excluding process directories):
ls -d /proc/[^0-9]*
All files recursively under /proc which do not start with a number:
find /proc -regex '.*/[0-9].*' -prune -o -print
But this will also exclude numeric files in subdirectories (for example /proc/foo/bar/123). If you want to exclude only the top-level files with a number:
find /proc -regex '/proc/[0-9].*' -prune -o -print
Hold on again! Doesn't this mean that any regular files created by touch /proc/123 or the like will be excluded? Theoretically yes, but I don't think you can do that. Try creating a file for a PID which does not exist:
$ sudo touch /proc/123
touch: cannot touch `/proc/123': No such file or directory

Use grep with -v which tells it to print all lines not matching the pattern.
ls /proc | grep -v '[0-9+]'

ls /proc | grep -v -E '[0-9]+'

Following regex matches all the characters except numbers
^[\D]+?$
Hope it helps !

For the sake of of completion. You may apply Mithandir's answer with find.
find . -name "[^0-9]*" -type f

Related

How to use grep to find in a directory by a regex?

I tried
grep -R '.*invalidTemplateName.*' -regex './online_admin/.*/UTF-8/.*'
to find all occurences of possible mathces of the '.invalidTemplateName.' regex within a directory regex pattern './online_admin/.*/UTF-8/.*', but it doesn't work. I got the message:
grep: ./online_admin/.*/UTF-8/.*: No such file or directory
If I use
grep -R '.*invalidTemplateName.*' .
it looks up in all subdirectory of the current directory that's overwhelming. How can I specify a directory pattern in grep? Is it possible?
Find might be a better choice here:
find ./online_admin/*/UTF-8/* -type f -exec grep -H "invalidTemplateName" {} \;
Find will locate all files in the locations you want, including subdirs of UTF-8 and then execute grep on each file. the -H argument ensures the filename will be printed along with the match. If you want only the filename, use the -L switch instead.
with find you could do something like that:
find /abs/path/to/directory -maxdepth 1 -name '.*invalidTemplateName.*'
using the name argument you can directly filter by names. you can also use wildcards for the filter-string.
using the maxdepth argument you can specify the level of recursion to look up the files. 1 means to look up in /abs/path/to/directory, 2 means to look up in /abs/path/to/directory and in the first level of directories in /abs/path/to/directory as well.

Find a string in multiple files using grep

I have a folder with sub-folders inside, all have many types of files. I want to search for a word inside the .css-files. I am using Windows 7 and I have grep.
How I can use grep to :
Find pattern and print it
Give file name (and path) if pattern found
Actually you don't need find. Just use:
grep -R --include=*.css -H pattern .
this will recurse and look for all *.css in subdirectories, while -H will show the filename.
find folder/ -name "*.css" |xargs grep "your-pattern"
You will need to install cygwin to do this.
if the files in which we have to look for, has pattern then we can use this.
Consider I'm looking for pattern "cardlayout" in files named chap1.lst chap2.lst and so on.
then the command
grep -e 'cardlayout' ` find . -name "chap??.lst"`
hope this would help

How to list all subdirectories with a string but not subdirectories of a match

In a bash script I would like to parse the names of all subdirectories and find all subdirectories that have a matching string, but I do not want subdirectories of a match. I am interested in automating construction of my $PATH and $PYTHONPATH variables based on directory structure.
Here's an example:
Let's say I want to go through my ~/dev and ~/bin folders and find all subdirectories with bin/ which holds programs that I will want to run at the shell. I can get a list with
$ ls -lR $HOME/bin $HOME/dev |grep "\/" | grep "bin:"
/Users/dat5h/bin:
/Users/dat5h/bin/project/bin:
...
These can all be appended to $PATH and have all available scripts ready to run.
BUT, let's say I was searching for directories with python modules and packages to add to $PYTHONPATH. I could conceivably look for all directories that start with /py-. So, I try:
$ ls -lR $HOME/bin $HOME/dev |grep "\/" | grep "/py-"
/Users/dat5h/bin/py-test:
/Users/dat5h/bin/py-test/test-package:
/Users/dat5h/bin/py-test/test-package/nested-test:
...
My thinking is that I would not want to put package directories and subdirectories into the path. I'm pretty sure that would be strange, but I am actually new to python so suggestions would be helpful. How would I go about constructing a test case to only get directories with py-* but non of the subsequent subdirectories?
I tried:
$ ls -lR $HOME/bin $HOME/dev |grep "\/" | egrep "/py-.*[^/]:"
But this doesn't get the job done either. Maybe a better regex? Any help would be greatly appreciated!
SOLUTION
The solution I ended up satisfied with was the find suggested below with a cutoms regex:
find $HOME/bin $HOME/dev -type d -regex ".*\/py\(\w\|-\w\)*"
This will find all subdirectories of ~/bin and ~/dev that are some variant of "pySOMETHING", "py-SOMETHING", "pySOME_THING_ELSE", or "py-SOME_THING_ELSE" but does not grab any subdirectories of those unless they also match this string. This ensures that I can have some simple naming convention for all of my directories with python modules/packages and import them this way without accidentally being able to import nested packages without the hierarchy.
Does this:
find -type d -regex ".*py.*bin.*"
give you some start?

How to exclude a directory in a recursive search using grep?

How to do a recursive search using grep while excluding a particular directory ?
Background : I have a large directory consisting of log files which I would like to eliminate in the search. The easiest way is to move the log folder. Unfortunately I cannot do that, as the project mandates the location.
Any idea how to do it ?
are you looking for this?
from grep man page:
--exclude-dir=DIR
Exclude directories matching the pattern DIR from recursive searches.
As an alternate, if you can use find in your search, it may also be useful:
find [directory] -name "*.log" -prune -o -type f -print|grep ...
The [directory] can actually be the current directory if you want (just a . will do).
The next part, -name "*.log" -prune is all together. It searches for filenames with the pattern *.log and will strip them OUT of your results.
Next is -o (for "or")
Then, -type f -print which says "print (to stdout) any type that is a file."
Those results should include every file (no directories are returned) found in [directory] except those that end in .log. Then you can grep the results as you need.

What's the most compact version of "match everything but these strings" in the shell or regex?

Linux: I want to list all the files in a directory and within its subdirectories, except some strings. For that, I've been using a combination of find/grep/shell globbing. For instance, I want to list all files except those in the directories
./bin
./lib
./resources
I understand this can be done as shown in this question and this other. But both versions are not solving the case "everything, but this pattern" in general terms.
It seems that it is much easier to use a conditional for filtering the results, but I wonder if there is any compact and elegant way of describing this in regexp or in the shell extended globbing.
Thanks.
yourcommand | egrep -v "pattern1|pattern2|pattern3"
Use prune option of find.
find . -path './bin' -prune -o -path './lib' -prune -o -path './resources' -prune -o «rest of your find params»
With bash's extglob shopt setting enabled, you can exclude files with ! in your wildcard pattern. Some examples:
Everything except bin, lib, and resources
shopt -s extglob
ls -Rl !(bin|lib|resources)
Everything with an i in it, except bin and lib
ls -Rl !(bin|lib|!(*i*))
(Everything that has an i in it is the same as everything except the things that don't have i's in them.)