DEV Community

nabbisen
nabbisen

Posted on

Fish shell: iteration with condition

This post shows how to use iteration and condition in fish shell.

For example, as to each file in current directory, let's check if 400KB < the file size and run some commands.

for x in *
    if test -f $x && test 800 -lt (du "$x" | cut -f1)
        some commands "$x"
    end
end
Enter fullscreen mode Exit fullscreen mode

First, forx in * gives each file as $x. Then iftest is used to check (1) if the input is file and (2) the file size.
Besides, "800" means 400 * 1024 / 512, where the block size is 512 byte.

Well, one-liner is also available.

for x in *; if test -f $x && test 800 -lt (du "$x" | cut -f1); somecommand "$x"; end; end;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay