Jay Taylor's notes
back to listing indexDetermine if filesystem or partition is mounted RO or RW via Bash Script?
[web search]statvfs
which among other things returns a flag fields with a flag indicating a read only mount. Unfortunately I don't know a shell command to do this directly. I would have used stat -f
, but that command shows everything except flags.
– kasperd
Apr 13 '16 at 6:59
mount
doesn't always tell you what you want: to paraphrase, it reads from /etc/mtab
, which is a cached version of the data, and may be outdated in some cases. The info in /proc/mounts
is what you really want.
– mwfearnley
May 23 '16 at 11:31
This little one-liner will pop-out something if a ro file system exists.
grep "[[:space:]]ro[[:space:],]" /proc/mounts
Assuming you don't usually have a ro file system like a CD in the drive, it is sufficient for some basic monitoring type stuff and doesn't require changing the file system to find the current state. It also doesn't assume your file system type. Pipe it into grep -v iso9660 if you want to keep your CDs out of the record.
grep -P "\sro[\s,]" /proc/mounts
or grep " ro[ ,]" /proc/mounts
– WhiteKnight
Apr 7 '15 at 10:11
ext4 rw,noatime,nobarrier,errors=remount-ro,data=ordered 0 0
is set to remount as read only in the event of an error, so without checking mount
you don't know if that remount has occurred, hence if it is in fact currently in ro
.
– Walf
Apr 28 '16 at 1:58
$ grep "\sro[\s,]" /proc/mounts
Output: /dev/mapper/root / ext4 ro,relatime,errors=remount-ro,user_xattr,acl,barrier=1,data=ordered 0 0
– flickerfly
Apr 28 '16 at 17:22
Old question, but I've came across it looking for same help and seems like found even easier way without the need to create file.
[ -w /root-rw ] && echo "rw" || echo "ro"
rw
[ -w /root-ro ] && echo "rw" || echo "ro"
ro
Of course, root-ro is ro mounted fs and root-rw is rw fs.
man dash
for the -w
option - 'The file is not writable on a read-only file system even if this test indicates true.' AFAIK this is the same for other shells.
– Graeme
Jun 26 '15 at 9:21
If the file system is mounted, I'd cd to a temporary directory and attempt to create a file. The return code will tell you if the file system is Read-Only or Read-Write provided that the file system is not full (thanks Willem).
touch afile && { rm afile; echo "read-write"; } || echo "read-only"
– glenn jackman
Jun 6 '11 at 14:59
mktemp -p /filesystem/of/interest/
instead of just using 'afile' to generate the file and filename. best
– Rik Schneider
Jun 6 '11 at 21:22
I just had this issue and these are real pastes ...
Take a look at /proc/mounts -
egrep " ro,|,ro " /proc/mounts
/dev/sda3 / ext4 ro,seclabel,relatime,barrier=1,data=ordered 0 0
/dev/sda5 /var ext4 ro,seclabel,relatime,barrier=1,data=ordered 0 0
FYI - These two partitions show as being mounted rw when just using the mount command.
Based on a flickerdfly's answer, influenced by a comment from WhiteKnight
Create a detector function the fly.
eval "function is_readonly () {
$( grep -P "\sro[\s,]" /proc/mounts | awk '{print "if echo $1 | grep -q \""$2"\"; then return 0;fi"}' )
return 1;}";
use it to determine if a path is on a read only fs
is_readonly /path/to/file/on/read/only/fs && echo "sorry. can't delete that"
And dispose of it when done
#dump temp function
unset -f is_readonly;
Here is my solution:
if findmnt ${YOUR_MOUNT_POINT} | awk '{print $4}' | grep "ro,"; then
echo "Read only!"
fi
site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required. rev 2019.4.22.33376