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.