How to find file with range parameter? - linux

For the following files:
res1, res2, res3, 1res4, res100
Expected result would be res1, res2 and res3. How to use 'grep' to get this result.
Thanks in Advance.

grep is not needed.
ls res[1-5]
If you want to have number range try:
ls res{1..100}

To do exactly what was requested:
find . -maxdepth 1 -type f | grep '^\./res[1-5]$'
will ignore res100, and only look for files in the current directory.
To get sorted output (as "ls" would do), add that step:
find . -maxdepth 1 -type f | grep '^\./res[1-5]$' |sort

Related

How to count files in specific subdirectories of a parent directory?

I use the find . -type f | wc -l command to count all the files in a regular directory, but in more specific cases if a directory contains many files, is it possible to specify this in the command? In case I only want to count all the files in the image subdirectories for example. To know how many images (all in .jpeg) I have in total in mydirectory.
This command works find /Users/mydirectory -type f -exec file --mime-type {} \; | awk '{if ($NF == "image/jpeg") print $0 }' but just display them. How to count them?
Finally the command find /Users/mydirectory -type f -exec file --no-pad --mime-type {} + | awk '$NF == "image/jpeg" {$NF=""; sub(": $", ""); print}' | wc -l seems to do the trick.
mydirectory/
folder1/
image/
label/
folder2/
image/
label/
...
I have the impression that you are not aware of the maxdepth parameter of the find command, which indicates the depth of your search command: by using find ... -maxdepth 1 you say that you only want to search within the directory itself. I believe this will solve your question.
Simple example: I created a subdirectory "tralala" and added two files, a subsubdirectory with one file, and I launched following command:
find tralala -maxdepth 1 -type f | wc -l
The answer was 2, which was correct, as you can see here from the amount "to be counted":
Prompt$ find tralala -ls
... drwxrwxrwx ... tralala/ => is a directory, don't count.
... drwxrwxrwx ... tralala/dir => is a directory, don't count.
... -rwxrwxrwx ... tralala/dir/test.txt => is inside subdirectory, don't count.
... -rwxrwxrwx ... tralala/test.txt => is file inside directory, to be counted.
... -rwxrwxrwx ... tralala/test2.txt => is file inside directory, to be counted.

Constructing a UNIX pipeline that finds all files in a directory (using find ,xargs and du commands )

I need to construct a UNIX pipeline that finds all files under the directory containing the word "english" (using find command)and calculate the size of each and sort them .
This is my implementation and I am getting an assertion error, any inputs will be appreciated
find /usr/share/dict -type f -name "english*"| xargs -n 1 du | sort -n
The error message is
E AssertionError: assert 44 == 2
E + where 44 = len(['4\t./.git/branches\r', '4\t./.git/objects/info\r', '4\t./.git/objects/pack\r', '4\t./.git/refs/tags\r', '8\t./.cache/v/cache\r', '8\t./.git/info\r', ...])
Two error:
Files can contain "spaces" - user print0
It's must be files. Use -type f
find /usr/share/dict -type f -name "english*" -print0| xargs -0 du | sort -n

How can I output the result of find and grep as filename => found

How can I combine the result of commands find and grep in the format: filename: => string?
For example, find . -maxdepth 2 -type f -name .env -exec grep 'CURRENT_ENV' {} \; The command will display me the line when CURRENT_ENVstring found, e.g. CURRENT_ENV=staging. I want to modify the output in the follow way: ./site1.com: CURRENT_ENV=staging.
I can't understand How can I reach that. Is it possible?
-H, --with-filename
Print the file name for each match. This is the default when
there is more than one file to search.
http://man7.org/linux/man-pages/man1/grep.1.html

finding largest file for each directory

I am currently stuck with listing the largest file of each subdirectory in a specific directory.
I succeeded in listing the largest file in a directory by entering the following command (in Debian):
find . -type f -printf "%p\n" | ls -rS |tail -1
I expected entering the command in a shell-file (searchHelper.sh) and running the following command would return the expected filenames for each subdirectory:
find -type d -execdir ./searchHelper.sh {} +
Unfortunately it does not return the largest file for each subdirectory, but something else.
May I get a hint for getting the filename (with absolute path) of the largest file of each subdirectory?
Many thanks in advance
Give a try to this safe and tested version:
find "$(pwd)" -depth -type f -printf "d%h\0%s %p\0" | awk -v RS="\0" '
/^d/ {
directoryname=substr($0,2);
}
/^[0-9]/ {
if (!biggestfilesizeindir[directoryname] || biggestfilesizeindir[directoryname] < $1) {
biggestfilesizeindir[directoryname]=$1;
biggestfilesizefilenameindir[directoryname]=substr($0,index($0," ")+1);
}
}
END {
for (directoryname in biggestfilesizefilenameindir) {
print biggestfilesizefilenameindir[directoryname];
}
}'
This is safe even if the names contain special chars: ' " \n etc.

How to find a name containing "space" in a file?

I have a BD.txt file with the following content:
Person1:Boston:20
Person2:New York:18
Person3:New York:25
Person4:New Orleans:30
I have to show how many people live in New York. I used:
cat BD.txt | grep New York | wc -l
The output was "3" instead of 2. "New York" has a space char on its name, and it finds only "new" (counting some results I don't want, like "New Orleans").
What should I use to find words containing sapce?
Use quotation marks around New York:
cat BD.txt | grep "New York" | wc -l
if I read the question correctly then it states to find the files having space in their name so below command will work
to find files in current directory only
find . -maxdepth 1 -type f -name \*' '\* -prune
to find files in all subdirectories also
find . -type f -name \*' '\*

Resources