2

I have the below lines in the my /etc/fstab file:

#
# /etc/fstab
# Created by anaconda on Sun Aug 23 14:32:22 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rootvg-rootlv /                       xfs     defaults        0 0
UUID=af74c8xz-e6ec-2387-b2xe-cdc32519a69f /boot                   xfs     defaults        0 0
UUID=GH4E-B04C          /boot/efi               vfat    defaults,uid=0,gid=0,umask=0077,shortname=winnt 0 0
#/dev/mapper/rootvg-homelv /home                   xfs     defaults        0 0
/dev/mapper/rootvg-optlv /opt                    xfs     defaults        0 0
#/dev/mapper/rootvg-tmplv /tmp                    xfs     defaults        0 0
/dev/mapper/rootvg-usrlv /usr                    xfs     defaults        0 0
/dev/mapper/rootvg-varlv /var                    xfs     defaults        0 0
/dev/mapper/rootvg-homelv /home                   xfs    nodev        1 2
/dev/mapper/rootvg-tmplv /tmp                    xfs    defaults,nodev,nosuid,noexec        1 2
tmpfs /dev/shm tmpfs nodev,nosuid,noexec 0 0
UUID=34g5et-fg55-40er-bx8u-9fhe47f986ab   /datadisk   ext4   defaults,nofail   1   2
/dev/disk/cloud/azure_resource-part1    /mnt    auto    defaults,nofail,x-systemd.requires=cloud-init.service,comment=cloudconfig       0       2

There are two entries for /home and /tmp. I need to uncomment the commented /home and /tmp and comment out the the other /home and /tmp lines.

So, my final file should look like this:

#
# /etc/fstab
# Created by anaconda on Sun Aug 23 14:32:22 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rootvg-rootlv /                       xfs     defaults        0 0
UUID=af74c8xz-e6ec-2387-b2xe-cdc32519a69f /boot                   xfs     defaults        0 0
UUID=GH4E-B04C          /boot/efi               vfat    defaults,uid=0,gid=0,umask=0077,shortname=winnt 0 0
/dev/mapper/rootvg-homelv /home                   xfs     defaults        0 0
/dev/mapper/rootvg-optlv /opt                    xfs     defaults        0 0
/dev/mapper/rootvg-tmplv /tmp                    xfs     defaults        0 0
/dev/mapper/rootvg-usrlv /usr                    xfs     defaults        0 0
/dev/mapper/rootvg-varlv /var                    xfs     defaults        0 0
#/dev/mapper/rootvg-homelv /home                   xfs    nodev        1 2
#/dev/mapper/rootvg-tmplv /tmp                    xfs    defaults,nodev,nosuid,noexec        1 2
tmpfs /dev/shm tmpfs nodev,nosuid,noexec 0 0
UUID=34g5et-fg55-40er-bx8u-9fhe47f986ab   /datadisk   ext4   defaults,nofail   1   2
/dev/disk/cloud/azure_resource-part1    /mnt    auto    defaults,nofail,x-systemd.requires=cloud-init.service,comment=cloudconfig       0       2

I tried the below two commands but it is commenting more lines than I need because it's finding more matches especially for the second sed command.

sed -i '/defaults,nodev,nosuid,noexec/ s/^/#/' /etc/fstab
sed -i '/nodev/ s/^/#/' /etc/fstab

How to achieve this?

2

A GNU Sed approach,

sed -Ei '/home|tmp/{s/^#//;t;s/^/#/}' file
  • /home|tmp/ If line matches home or tmp,
    • {s/^#//;t;s/^/#/} Remove # in the line start. If it was not there, substitute the line start by #.

An Awk alternative

awk '$2 ~ /home|tmp/{ if (!( sub(/^#/, "") )) $1="#"$1} 1' file
  • $2 ~ /(home|tmp)/ If 2nd field contains home or tmp,
    • if (!( sub(/^#/, "") )) Try to match a # in the line start and delete it. If the operation was not successful (because there was no # in the line start),
      • $1="#"$1 Append a # to the line start.
  • 1 Print the resulting line.

If you have Gawk, you can add the -i inplace flag to edit the file in-place, but I find it better to redirect the output to a file and then overwrite the original file after checking that all went right.

-1

With ansible module lineinfile especially if you have to do it for multiple hosts : https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.