New answers tagged

6 votes

Filtering find results by length

With the zsh shell, globs can be filtered by file type (here directory) using glob qualifiers, and with the extendedglob option on, you get a (#cX,Y) glob operator similar to ERE {X,Y}, so you could ...
user avatar
7 votes
Accepted

Filtering find results by length

You could use the ? wildcard five times: find ./directory -maxdepth 1 -type d -name '?????*' This requires matches to have at least five characters in their name.
user avatar
  • 63k
0 votes

Find number of lines between a pattern inside specific filetype

Not sorting the output: Trying this for my own file tree with thousands of json files: $ find . -type f -name "*[0-9].json" \ -exec bash -c 'printf "%4d %s\n" $(jq "....
user avatar
  • 2,229
-2 votes

Find number of lines between a pattern inside specific filetype

Unfortunately, I could not do a one-liner for this requirement. The following script solved the problem #!/bin/bash declare -a arr arr=() for i in $(find . -name notes.json) do arr+=`(echo $i | ...
user avatar
  • 7
3 votes

find files matching pattern inside directory matching another pattern

In bash (and other shells with similar functionality) you could use globstar shopt -s globstar nullglob printf '%s\n' **/bar_[0-9]/foo_[0-9][0-9]
user avatar
  • 72.1k
5 votes
Accepted

find files matching pattern inside directory matching another pattern

For this case, you can use the -path <pattern> option, similar to the -name option of find: find . -path "*bar_[0-9]/foo_[0-9][0-9]"
user avatar
  • 5,970
0 votes

Delete only files older than 7 days: -mtime and find

With zsh: rm -f /var/log/mbackups/*(.m+6) same as: rm -f /var/log/mbackups/*(.^m-7) Would remove the regular (with .) files that are not hidden and haven't been modified in the last 7 days. Like for ...
user avatar
6 votes

`find` command doesn't return array like element, how to work around this

In bash, I know the following is roughly equivalent: test=$(find . -iname '*.py') when I echo $test[0] however, I don't get the first item, so I think find returns a string like object. For some ...
user avatar
  • 114k
0 votes

xargs running more than one command?

directories are one of many types of files (regular, directory, symlink, fifo, device...). Maybe you meant regular files as that's the other type of files beside directories that have disk space ...
user avatar
1 vote
Accepted

xargs running more than one command?

You can use find to get only regular files and run du for them. Standard error is supressed for the command below, because if you do it for your root directory, there could be some complaints for some ...
user avatar
  • 5,970
1 vote

How to search filenames by regex with "find" and rename this files

With zsh: autoload zmv zmv -n '(**/)*-(<->-<->.mp4)' '$1$2' (remove the -n for dry-run if happy).
user avatar
1 vote
Accepted

How to search filenames by regex with "find" and rename this files

GNU find supports several regular expression dialects (the -regextype option lets you select one). Parentheses are special characters in some of them and need to be backslash-escaped to gain their ...
user avatar
  • 8,971
2 votes
Accepted

How can I use `find` to find the files and awk to search within the files?

$ find . \( -name .git -prune \) -o \( -type f -exec \ awk -v OFS=':' ' FNR == 1 { fname=FILENAME; sub(/^\.\//,"",fname) } /zorro/ { gsub(/ +/," "); gsub(/\t+/,&...
user avatar
  • 21.9k
0 votes

Find command if filename doesn't exists in directory

Using find + sed find missing-foo 2>&1 > /dev/null | sed 's@find: ‘\([^’]*\).*@\1@' Find prints to stderr if the file doesn't exists. Swap stdout/stderr and filter with sed. Ugly and ...
user avatar
  • 119
1 vote

Deleting files using xargs prints lots of No such file or directory (but seems to work...)

You are deleting directories including its contents, and then delete these contents which don't exist anymore, hence the error messages. It's probably not a good idea to delete directories based on ...
user avatar
  • 20.6k
-1 votes
Accepted

Piping find to xargs works in terminal, but not in shell script

So, in the answer linked by @KamilMaciorowski , I learned that Bash treats pipes differently in strings that run commands. I solved the problem by changing the line $cmd to eval "$cmd" and ...
user avatar
  • 101
-2 votes

Piping find to xargs works in terminal, but not in shell script

You have a good idea about loading a list to a command, but there are better ways to combine variables and commands. @KamilMaciorowski provided some good context here, but you're probably still ...
user avatar
  • 107
10 votes
Accepted

Handle names with spaces when iterating recursively over files

Your error lies in using find in a command substitution. A command substitution always results in a single string. If you leave the substitution unquoted, that string will undergo splitting on spaces, ...
user avatar
  • 267k
1 vote

How to remember, which order grep and find takes the parameters?

grep and find I'm stumbling through, which order I should type the commands: When I should write the path and when I should write the flags, if it needs double quotations or not, etc. etc. The ...
user avatar
  • 28.8k
1 vote

How do I handle strange path characters when piping find output?

Even though -exec echo {} avoids shell processing, many versions of echo mangle argument(s) containing backslash or leading hyphen. (And of course it doesn't pipe to wc as you want.) Instead of ...
user avatar
2 votes
Accepted

How do I handle strange path characters when piping find output?

Pass the filename as an argument to the shell script: find . -exec sh -c 'printf "%s\n" "$1" | wc -c' sh {} \; or for multiple files per shell invocation: find . -exec sh -c 'for ...
user avatar
  • 114k
1 vote

How to display the paths to all files that have a size greater than the specified limit

The first error is simple: # wrong: FILESIZE = "+10M"; # correct: FILESIZE="+10M" You should not surround = sign with spaces in assigning shell variables. Another problem - ...
user avatar
  • 1,138
1 vote

A printf format for "find" or a means to control diff in comparing files across file systems

This answer is admittedly incomplete, not least because I don't have a Windows machine from which to test. But I will touch first briefly on the grave disservice your IT department is doing you by ...
user avatar
  • 5,420
3 votes
Accepted

xargs -I# showing unexpected behavior - why?

A comment (in the bash shell), is introduced by the # character. But this only happens if the # character is unquoted, and the first character in a word (token). If # is quoted or occurs as a ...
user avatar
  • 267k
0 votes

How to find executable filetypes?

find . -type f -executable -exec sh -c "[[ \"\$(head -c 4 -- \"\${1}\")\" == \$'\\x7FELF' ]]" -- \{\} \; -print has the advantage that it doesn't "escape find"....
user avatar

Top 50 recent answers are included