LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Find command confusion ??? (https://www.linuxquestions.org/questions/linux-newbie-8/find-command-confusion-4175655433/)

ddenial 06-09-2019 10:11 PM

Find command confusion ???
 
Hello

I want to find files that are less than 1 MiB.

Following is the actual list of files:
Code:

$ ls -lS
total 18636
-rw-r--r--. 1 xxxxx xxxxx 3960382 Mar 18 17:59 adwaita-day.jpg
-rw-r--r--. 1 xxxxx xxxxx 3376649 Mar 18 17:59 adwaita-morning.jpg
-rw-r--r--. 1 xxxxx xxxxx 1005300 May 27 19:54 rhel_patterncity_2560x1600.jpg
-rw-r--r--. 1 xxxxx xxxxx  911868 May 27 19:54 rhel_patternstripe_2560x1600.jpg
-rw-r--r--. 1 xxxxx xxxxx  860907 May 27 19:54 rhel_patterncity_2560x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  821545 May 27 19:54 rhel_patterncity_2048x1536.jpg
-rw-r--r--. 1 xxxxx xxxxx  813419 May 27 19:54 rhel_patternstripe_2560x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  706510 May 27 19:54 dottexture_navy_2560x1600.jpg
-rw-r--r--. 1 xxxxx xxxxx  679680 May 27 19:54 rhel_patterncity_1800x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  674743 May 27 19:54 rhel_patternstripe_2048x1536.jpg
-rw-r--r--. 1 xxxxx xxxxx  659666 May 27 19:54 dottexture_navy_2560x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  584644 May 27 19:54 dottexture_navy_2048x1536.jpg
-rw-r--r--. 1 xxxxx xxxxx  549745 May 27 19:54 rhel_patternstripe_1800x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  509531 May 27 19:54 night.jpg
-rw-r--r--. 1 xxxxx xxxxx  482844 May 27 19:54 dottexture_navy_1800x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  456058 May 27 19:54 morning.jpg
-rw-r--r--. 1 xxxxx xxxxx  447635 Mar 18 17:59 adwaita-night.jpg
-rw-r--r--. 1 xxxxx xxxxx  392746 May 27 19:54 day.jpg
-rw-r--r--. 1 xxxxx xxxxx  356827 May 13 15:26 074ea2885318037b764178e7f8c03b11b.jpg
-rw-r--r--. 1 xxxxx xxxxx  223076 May 27 19:54 corporate_engraved_2560x1600.jpg
-rw-r--r--. 1 xxxxx xxxxx  208803 May 27 19:54 corporate_engraved_2560x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  191219 May 27 19:54 corporate_engraved_2048x1536.jpg
-rw-r--r--. 1 xxxxx xxxxx  163977 May 27 19:54 corporate_engraved_1800x1440.jpg

So I issue this command:
Code:

$ find . -size -$((1*1024*1024))c -ls
359751838    4 drwxr-xr-x  2 xxxxx    xxxxx        4096 Jun  9 12:35 .
359955587  164 -rw-r--r--  1 xxxxx    xxxxx      163977 May 27 19:54 ./corporate_engraved_1800x1440.jpg
359955590  188 -rw-r--r--  1 xxxxx    xxxxx      191219 May 27 19:54 ./corporate_engraved_2048x1536.jpg
359955591  204 -rw-r--r--  1 xxxxx    xxxxx      208803 May 27 19:54 ./corporate_engraved_2560x1440.jpg
359960452  220 -rw-r--r--  1 xxxxx    xxxxx      223076 May 27 19:54 ./corporate_engraved_2560x1600.jpg
359960453  384 -rw-r--r--  1 xxxxx    xxxxx      392746 May 27 19:54 ./day.jpg
359960454  472 -rw-r--r--  1 xxxxx    xxxxx      482844 May 27 19:54 ./dottexture_navy_1800x1440.jpg
359960455  572 -rw-r--r--  1 xxxxx    xxxxx      584644 May 27 19:54 ./dottexture_navy_2048x1536.jpg
359960456  648 -rw-r--r--  1 xxxxx    xxxxx      659666 May 27 19:54 ./dottexture_navy_2560x1440.jpg
359960457  692 -rw-r--r--  1 xxxxx    xxxxx      706510 May 27 19:54 ./dottexture_navy_2560x1600.jpg
359960458  448 -rw-r--r--  1 xxxxx    xxxxx      456058 May 27 19:54 ./morning.jpg
359960459  500 -rw-r--r--  1 xxxxx    xxxxx      509531 May 27 19:54 ./night.jpg
359960460  664 -rw-r--r--  1 xxxxx    xxxxx      679680 May 27 19:54 ./rhel_patterncity_1800x1440.jpg
359960461  804 -rw-r--r--  1 xxxxx    xxxxx      821545 May 27 19:54 ./rhel_patterncity_2048x1536.jpg
359960462  844 -rw-r--r--  1 xxxxx    xxxxx      860907 May 27 19:54 ./rhel_patterncity_2560x1440.jpg
359960463  984 -rw-r--r--  1 xxxxx    xxxxx    1005300 May 27 19:54 ./rhel_patterncity_2560x1600.jpg
359960464  540 -rw-r--r--  1 xxxxx    xxxxx      549745 May 27 19:54 ./rhel_patternstripe_1800x1440.jpg
359960465  660 -rw-r--r--  1 xxxxx    xxxxx      674743 May 27 19:54 ./rhel_patternstripe_2048x1536.jpg
359960466  796 -rw-r--r--  1 xxxxx    xxxxx      813419 May 27 19:54 ./rhel_patternstripe_2560x1440.jpg
359960467  892 -rw-r--r--  1 xxxxx    xxxxx      911868 May 27 19:54 ./rhel_patternstripe_2560x1600.jpg
358795372  352 -rw-r--r--  1 xxxxx    xxxxx      356827 May 13 15:26 ./074ea2885318037b764178e7f8c03b11b.jpg
380644160  440 -rw-r--r--  1 xxxxx    xxxxx      447635 Mar 18 17:59 ./adwaita-night.jpg

Perfect.

But when I use -exec option, why I'm getting this output?
Code:

$ find . -size -$((1*1024*1024))c -exec ls -l "{}" +
-rw-r--r--. 1 xxxxx xxxxx  356827 May 13 15:26 ./074ea2885318037b764178e7f8c03b11b.jpg
-rw-r--r--. 1 xxxxx xxxxx  447635 Mar 18 17:59 ./adwaita-night.jpg
-rw-r--r--. 1 xxxxx xxxxx  163977 May 27 19:54 ./corporate_engraved_1800x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  191219 May 27 19:54 ./corporate_engraved_2048x1536.jpg
-rw-r--r--. 1 xxxxx xxxxx  208803 May 27 19:54 ./corporate_engraved_2560x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  223076 May 27 19:54 ./corporate_engraved_2560x1600.jpg
-rw-r--r--. 1 xxxxx xxxxx  392746 May 27 19:54 ./day.jpg
-rw-r--r--. 1 xxxxx xxxxx  482844 May 27 19:54 ./dottexture_navy_1800x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  584644 May 27 19:54 ./dottexture_navy_2048x1536.jpg
-rw-r--r--. 1 xxxxx xxxxx  659666 May 27 19:54 ./dottexture_navy_2560x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  706510 May 27 19:54 ./dottexture_navy_2560x1600.jpg
-rw-r--r--. 1 xxxxx xxxxx  456058 May 27 19:54 ./morning.jpg
-rw-r--r--. 1 xxxxx xxxxx  509531 May 27 19:54 ./night.jpg
-rw-r--r--. 1 xxxxx xxxxx  679680 May 27 19:54 ./rhel_patterncity_1800x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  821545 May 27 19:54 ./rhel_patterncity_2048x1536.jpg
-rw-r--r--. 1 xxxxx xxxxx  860907 May 27 19:54 ./rhel_patterncity_2560x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx 1005300 May 27 19:54 ./rhel_patterncity_2560x1600.jpg
-rw-r--r--. 1 xxxxx xxxxx  549745 May 27 19:54 ./rhel_patternstripe_1800x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  674743 May 27 19:54 ./rhel_patternstripe_2048x1536.jpg
-rw-r--r--. 1 xxxxx xxxxx  813419 May 27 19:54 ./rhel_patternstripe_2560x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  911868 May 27 19:54 ./rhel_patternstripe_2560x1600.jpg

.:
total 18636
-rw-r--r--. 1 xxxxx xxxxx  356827 May 13 15:26 074ea2885318037b764178e7f8c03b11b.jpg
-rw-r--r--. 1 xxxxx xxxxx 3960382 Mar 18 17:59 adwaita-day.jpg
-rw-r--r--. 1 xxxxx xxxxx 3376649 Mar 18 17:59 adwaita-morning.jpg
-rw-r--r--. 1 xxxxx xxxxx  447635 Mar 18 17:59 adwaita-night.jpg
-rw-r--r--. 1 xxxxx xxxxx  163977 May 27 19:54 corporate_engraved_1800x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  191219 May 27 19:54 corporate_engraved_2048x1536.jpg
-rw-r--r--. 1 xxxxx xxxxx  208803 May 27 19:54 corporate_engraved_2560x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  223076 May 27 19:54 corporate_engraved_2560x1600.jpg
-rw-r--r--. 1 xxxxx xxxxx  392746 May 27 19:54 day.jpg
-rw-r--r--. 1 xxxxx xxxxx  482844 May 27 19:54 dottexture_navy_1800x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  584644 May 27 19:54 dottexture_navy_2048x1536.jpg
-rw-r--r--. 1 xxxxx xxxxx  659666 May 27 19:54 dottexture_navy_2560x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  706510 May 27 19:54 dottexture_navy_2560x1600.jpg
-rw-r--r--. 1 xxxxx xxxxx  456058 May 27 19:54 morning.jpg
-rw-r--r--. 1 xxxxx xxxxx  509531 May 27 19:54 night.jpg
-rw-r--r--. 1 xxxxx xxxxx  679680 May 27 19:54 rhel_patterncity_1800x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  821545 May 27 19:54 rhel_patterncity_2048x1536.jpg
-rw-r--r--. 1 xxxxx xxxxx  860907 May 27 19:54 rhel_patterncity_2560x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx 1005300 May 27 19:54 rhel_patterncity_2560x1600.jpg
-rw-r--r--. 1 xxxxx xxxxx  549745 May 27 19:54 rhel_patternstripe_1800x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  674743 May 27 19:54 rhel_patternstripe_2048x1536.jpg
-rw-r--r--. 1 xxxxx xxxxx  813419 May 27 19:54 rhel_patternstripe_2560x1440.jpg
-rw-r--r--. 1 xxxxx xxxxx  911868 May 27 19:54 rhel_patternstripe_2560x1600.jpg

Not only the output gets repeated but also shows files that are greater than 1 MiB.

How do I correct this?

Thanks

ehartman 06-09-2019 11:09 PM

Quote:

Originally Posted by ddenial (Post 6003744)
Not only the output gets repeated but also shows files that are greater than 1 MiB.

How do I correct this?

Thanks

First: the size option can be easier put as "-size -1M" (capital M).
Then, the -exec as YOU did it will also work on subdirectories, as they're (almost always) smaller then 1M. So either put an extra "-type f" in the find or an extra option -d in
the ls -l (to show directories AS directories, not as their contents).
Finally the string {} doesn't need to be quoted.

See the section about "-execdir" too in the man page for find.

ddenial 06-09-2019 11:38 PM

Thanks

Quote:

Originally Posted by ehartman (Post 6003753)
First: the size option can be easier put as "-size -1M" (capital M).

For some reason, find won't find files less than 1MiB. I got this from here, post-No.3

Quote:

Originally Posted by ehartman (Post 6003753)
The, the -exec as YOU did it will also work on subdirectories, as they're (almost always) smaller then 1M. So either put an extra "-type f" in the find or an extra option -d in the ls -l (to show directories AS directories, not as their contents).

But there are no subdirectories. The -type f option works though. Does the . and .. are also considered as directories?

ehartman 06-10-2019 12:44 AM

Quote:

Originally Posted by ddenial (Post 6003761)
Does the . and .. are also considered as directories?

As your output shows the contents of
Quote:

.:
total 18636
too: yes!

I don't think find will go into .., though.

BTW: find's -ls option is equivalent to ls -dils, so with i-nodes and size in blocks added.

Shadow_7 06-10-2019 04:13 AM

The $(()) only takes two parameters, for three you'd have to nest $(()), so $(( 1 * $(( 1024 * 1024 )) ))c. Or I could be wrong.

Shadow_7 06-10-2019 04:48 AM

Odd, the "-type f" as previous suggested helps. Without it the -size seemed to get ignored. For me anyway.

$ find ./ -size $(( -1 * 50 ))k -type f 2>&1 | while read FILE; do ls -l $FILE; done

The while read just to have ls -l output to show file size. Probably a way for find to output that, and not efficient my way. I just wanted it to verify results. I could have sworn that $(( )) needed nesting, maybe a versioning quirk. Or an old programmer mental block having started on assembler where nothing took more than two parameters (or used to at least). In either case, probably better to multiply with a leading negative number for a - prefixed result. The - prefixed arithmetic operation might be interpreted as a bad parameter, than a value to a parameter. Probably just another programmer mental block though, as it seems to work regardless.

GazL 06-11-2019 05:59 PM

edit: NM. I see you corrected yourself. Sorry for the noise.

rnturn 06-12-2019 02:54 PM

Quote:

Originally Posted by ehartman (Post 6003765)
I don't think find will go into .., though.

No. It doesn't. If it did, you'd have a difficult time controlling where it was supposed to look. :^D

rnturn 06-12-2019 02:58 PM

Quote:

Originally Posted by Shadow_7 (Post 6003794)
The $(()) only takes two parameters, for three you'd have to nest $(()), so $(( 1 * $(( 1024 * 1024 )) ))c. Or I could be wrong.

No restriction on the number of arguments or on the spacing (in case that affect the arg count):
Code:

$ echo "$(( 1 * 1024 * 1024 ))"
1048576
$ echo "$(( 2 * 2 * 1024 * 1024 ))"
4194304



All times are GMT -5. The time now is 11:43 PM.