bash - find command with regex user option

bash find regex
grep regex
find command with regular expressions
linux find regex name
find command line regex
find regex support
linux regex search file
how to use regex in command line

I need get all files from directory with -user option (owner), but user must by in regexp like user_*. This option (regexp) is for filenames but i search solution for user. Sorry for my English.

For example - users (file owners): userA, userB, user_1, user_2... etc

A solution like this:

find ./ -type f -user "user_*"

except the above doesn't work.

This answer is based on Aaron's answer. However users do not have to be listed manually anymore.

compgen -u lists all users. compgen -u user_ lists all users starting with user_. If you want more flexibility use compgen -u | grep regex.

find . -type f \( printf -- '-user %s -o ' $(compgen -u user_) false \) 

bash, bash - find command with regex user option. grep regex find -regextype bash find regex linux find regex name grep regex tester find regex multiple extensions Find command and regex Hi All, We have to copy some files from a source directory to a destination directory. We only have to copy the file if the filename is in a list of values. We can use find command: find. -type f -name '*_111.txt' -o -name '*_115.txt' -exec cp {} /tmp \; But the list contains

There is no direct support for this. Either generate the command from a list of user names, or postprocess the output from find.

Supposing you have GNU find, try

find . -type f -printf '%u\t%p\n' |
grep '^user_' |
cut -f1

This will fail if you have file names with newlines, or if you don't have GNU find. You could probably get around these limitations easily; for replacing the GNU extenstion -printf, try -exec stat though the exact options to pass in are platform-dependent.

regex - Find command with regular expressions, fubar3', you can use the regular expression `. are by default Emacs Regular Expressions, but this can be changed with the -regextype option. The regular expressions understood by find are by default Emacs Regular Expressions (except that `.' matches newline), but this can be changed with the -regextype option. -samefile name File refers to the same inode as name. When -L is in effect, this can include symbolic links

You can use -or to compose predicates :

find ./ -type f \( -user user1 -o -user user2 -o -user user3 \)

It won't be possible to use wildcards that would select over users, but you could possibly generate the command from more specific wildcards :

find . -type f \( $(printf -- '-user user_%s -o ' {1,2,A,B}) false \)

Use of the "OR" Regex operator with the find command, Reading into the man page of find gives a bit of useful information: The regular expressions understood by find are by default Emacs Regular  The find command in UNIX is a command line utility for walking a file hierarchy. It can be used to find files and directories and perform subsequent operations on them. It supports searching by file, folder, name, creation date, modification date, owner and permissions.

find . -type f -exec bash -c 'stat -c "%U %n" "{}"|egrep -e ^user_|cut -d " " -f2-' \;

Explanation: for each file found by find you list username and filename (stat -c "%U %n" "{}"), then you apply your regexp rule, in this case filtering lines with begin with user_ (egrep -e ^user_). Finally, you filter again the result displaying the second field onwards (cut -d " " -f 2-) which, in the output above corresponds to the file name.

As commented below, argument to stat can be platform dependent, here I am using GNU stat on linux.

How to correctly use find with regular expression?, Note that -regex / -iregex (GNU extensions), like the standard -path match on the full path, not just the file name. If you want to find files whose  Despite the popularity of window managers that offer graphical user interfaces, the best way to search for files in Linux requires a shell. The find command, with its myriad options and switches, offers the most powerful and precise features to surface what you're looking for.

How to use find command to search for multiple extensions, Use the -o flag between different parameters. find ./ -type f \( -iname \*.jpg -o -​iname \*.png \) works like a charm. NOTE There must be a space between the  For example, to match a file named './fubar3', you can use the regular expression '.*bar.' or '.*b.*3', but not 'f.*r3' (because the complete path does not begin with an f). The regular expressions understood by find are by default Emacs regular expressions, but this can be changed with the -regextype option (see above). - samefile name

Using Grep + Regex (Regular Expressions) to Search Text in Linux , Execute the following command to use grep to search for every line that contains the word GNU : grep "GNU" GPL-3. The first argument, GNU , is the pattern you're​  File name matches regular expression pattern. This is a match on the whole path, not a search. For example, to match a file named './fubar3', you can use the regular expression '.*bar.' or '.*b.*3', but not 'f.*r3'. The regular expressions understood by find are by default Emacs Regular Expressions, but this can be changed with the -regextype

Linux Find Command Tutorial – Linux Hint, In this quick tutorial, we will see how we can use find command in an Ubuntu machine to quickly find files based on various patterns, regular expressions and inside directories in a The name option we mentioned above is case-sensitive. The general form of the command is: find (starting directory) (matching criteria and actions) The find command will begin looking in the starting directory you specify and proceed to search through all accessible subdirectories. You may specify more than one starting directory for searching.

Comments
  • Your exampes are not consistent. Shoold there be an underscore after user, or not? Also, your pattern looks like a glob, not a regex.
  • Yes, You have right, but I thought about similar solutions... regexp or glob.
  • Thanks, this is good solution for utf-8 encoding, but when files are in windows-1250 and linux see brusches ;) that cut out from result this files (with brusches :). Thanks for help
  • I don't understand "brusches". You can convert the result to UTF-8 by piping to iconv -f cp1250. This is unrelated to anything you mentioned in your question, and would affect any one of the answers here. Maybe think about converting the file system to this millennium.
  • Users put files like doc, xls (ftp), and this files come from Windows system and are encode in windows-1250 with polish language ą,ę,ś,ć etc Linux see this file name like 'someth?ing? .xls'. Your command work for me but it cut off files with '?' characters in result. Grep command cut off this records.
  • Do the iconv before the grep then.
  • The final -o true will select all files, won't it?
  • Thanks for reply. This is good solution for small number users but helpfull ;)
  • @PiotrSiewiera under which form are you getting your users? E.g. is it a fixed list you'll hardcode in your script, or do you read it from somewhere at runtime? I'm sure I could use a solution akin to my second command to generate the appropriate find command
  • @Aaron Good idea... but i get 'paths must precede expression: false'. I will write this in script, but now I test in runtime :)
  • Weird, the printf is supposed to expand into -user user_1 -o [...] -o with an extra trailing -o which is the reason why I added the false. You could use set -x (or simply prepend the command with echo) to see what's going on.
  • Don't use ls in scripts and the regex is (probably) wrong. You could fix this with stat as I outlined in my answer already.