I use mke2fs utility to create a file system. If file system already exists, I get a confirmation prompt:

~# mkfs.ext4 /dev/xvdf 
mke2fs 1.46.2 (28-Feb-2021)
/dev/xvdf contains a ext4 file system labelled 'Data'
        created on Sun Oct 10 22:06:41 2021
Proceed anyway? (y,N) n

So far so good. Now I want to put this command into a script, and make it silently select no in such occasions:

~# echo 'n' | mkfs.ext4 /dev/xvdf 
mke2fs 1.46.2 (28-Feb-2021)
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: c3136577-5ef3-435c-a02c-ebae8d1f4e32
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

??? There is no interactive prompt in this case, it just proceeds with the job. Also tried with expect, and yes n as suggested by @kjensenxz later - there is no prompt either. It doesn't get stuck waiting for the input - there is no question at all, as if mke2fs magically detects that it was run with a piped input and then it doesn't even try to read it.

-q flag has no effect on this.

The script runs on alpine linux, if that matters.

2 Answers 2


There is no option to mkfs.ext4 that tells it to abort if it's found a filesystem. Instead, you need to perform that check yourself. (I would imagine that mkfs.ext4 only bothers to ask for confirmation if stdin is connected to a terminal, and if not it assumes it wouldn't get an answer so continues anyway. This would be a reasonable approach that's similar to ls and ls | cat outputting different layouts.)

Here are examples of device filesystem enquiries on a Pi,

file -s /dev/mmcblk0p1
/dev/mmcblk0p1: DOS/MBR boot sector, code offset 0x3c+2, OEM-ID "mkfs.fat", sectors/cluster 16, root entries 512, Media descriptor 0xf8, sectors/FAT 32, sectors/track 63, heads 255, sectors 129024 (volumes > 32 MB), reserved 0x1, serial number 0x22e0c711, label: "boot       ", FAT (16 bit)

file -s /dev/mmcblk0p2
/dev/mmcblk0p2: Linux rev 1.0 ext4 filesystem data, UUID=202638e1-4ce4-45df-9a00-ad725c2537bb (needs journal recovery) (extents) (large files)

file -s /dev/mmcblk0p3
/dev/mmcblk0p3: LVM2 PV (Linux Logical Volume Manager), UUID: 40t385-3Q2C-u3Qz-TN5b-1fI1-0oYC-PsMQ3c, size: 27618967552

There are quite a number of possible options - considerably more than I've shown here - so it's probably easier to look for the "empty" case and only continue if the intended device matches this. For experimentation you could use dd if=/dev/zero or dd if=/dev/urandom, but both have file -s return data as the file type:

dd if=/dev/zero bs=1M count=100 of=/tmp/100M.dat
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.487274 s, 215 MB/s

lodev=$(losetup --find --show /tmp/100M.dat ); echo $lodev

file -s $lodev
/dev/loop0: data

losetup -d $lodev
rm /tmp/100M.dat

The code could be something like this,


magic=$(file -s "$device" 2>&1)
if [ "$magic" = "$device: data" ]
    # It isn't an obvious filesystem
    mkfs.ext4 ... "$device"
    # Uh oh
    printf "%s\n" "Cowardly refusing to create a filesystem on ${magic%%,*}" >&2
    exit 1
  • Thank you, looks I'll end up doing checks as you describe. It's a shame mke2fs makes those checks, but doesn't provide a command line option to control them. I still try to find out what is the difference between the calls with and w/o a pipe, and if there is a way to 'switch' between them.
    – nevermind
    Oct 11, 2021 at 8:49

yes n | mkfs.ext4 /dev/sdXY

See yes(1).

  • Thank you for suggestion, but that didn't work either. I updated the question to make it clear what happens.
    – nevermind
    Oct 11, 2021 at 8:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .