DEV Community

loading...

Fish shell: iteration with condition

Heddi Nabbisen
An ICT designer/developer and a security monk with "a cool brain and a warm heart". OpenBSD/Rust etc. Interested in social issues and relationships. Founder of Scqr Inc.
・1 min read

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

Discussion (0)