0

I have a fairly large number of symlinks (more than a hundred in a single folder, and anywhere up to a couple dozen in each of a couple dozen other folders) that don't work after moving from Mate 16.04 to Kubuntu 20.04 (and apparently not accounting for a platter drive that's currently not connected when I recreated /etc/fstab) -- what used to be /media/user/sdc9 is now just /sdb9. These are the files I linked based on the accepted answer for this question.

I've verified that making this exact change in these symlinks will correct them so they open the folder they were made to point to. As opposed to either recreating them or manually editing the properties on all of them (which I've done in four, enough to confirm that this fixes the problem) (did I mention there are at least a couple hundred of these, one set spread through a couple dozen folders?), is there an efficient way, ideally with a single command per folder, to change the target of each symlink and replace /media/user/sdc9 with /sdb9 -- and no other changes?

I know there are ways to rename files using wildcards, or feeding the output of ls into awk or other arcane-seeming paths. I presume something similar can be done with the contents of these files that behave as pointers to other files or folders. However, despite using Debian-based Linux as my daily driver for almost ten years, I'm not knowledgeable enough with Linux commands to have even a clue what will do what I need here. Stuff like this simply doesn't come up often enough for someone who just wants to get his stuff done to spend hundreds of hours learning it.

Zeiss Ikon
  • 5,128

2 Answers2

1

The simplest way to repair your broken symlinks is to mount the partition in question at /media/user/sdc9.

Create an according fstab-entry for this partition. Do not use device-name like /dev/sdc9 or /dev/sdb9, use UUID or LABEL to idententify the partition. I prefer LABEL over UUID, since it's better readable and I don't need to search for the partitions UUID. Just make sure that LABEL is unique in your system.

You can set a Label for ext2,ext3 or ext4 partitions with tune2fs:

sudo tune2fs -L /dev/sdxy

where xy needs to be replaced with the correct letter and number.

From man tune2fs:

-L volume-label
          Set the volume label of the filesystem. Ext2 filesystem labels can be at 
          most 16 characters long; if volume-label is longer than 16 characters,
          tune2fs will truncate it and print a warning. The volume label can be
          used by mount(8), fsck(8), and /etc/fstab(5) (and possibly others) by
          specifying LABEL=volume-label instead of a block special device name 
          like /dev/hda5.

The command lsblk -f will display sufficient information about your partitions, including LABEL, UUID and device-name.

mook765
  • 15,925
  • I can't use LABEL here because these are on ntfs -- but this will quickly and efficiently solve my problem, and avoid further confusion when I get the AWOL platter drive reconnected. Thanks for spotting the easy way (I'll still have to manually fix the four I "fixed" already and three or four new ones, but that's not a couple hundred). – Zeiss Ikon Feb 05 '21 at 13:05
  • I wonder, though, if this will deal with these partitions no longer residing in /media/user/ (apparently a change from 16.04 to 20.04)? – Zeiss Ikon Feb 05 '21 at 13:08
  • LABEL will also work with NTFS-partitions, see here how to assign a label to NTFS-partitions. It's not a change from 16.04 to 20.04, partitions reside on a physical drive and you can mount them to any folder you want, it just depends on your fstab where you define where to mount a partition. If there is no fstab-entry for a partition, it will still be mounted under /media/user/UUID or, if the partition is labeled, under /media/user/LABEL. Just correct your current fstab-file and you'll be good to go. – mook765 Feb 05 '21 at 15:04
  • Well, that's a revoltin' development. This is still giving trouble, because the links that are broken have a target of the form "/media/[USER]/sdc9" with my old username (I change it each reinstall so settings don't get crossed up in the same home folder) so just remounting the partition (which I've now done) doesn't fix them. One folder I can recreate with an answer to my previous question (recursive symlink creation). The others, I'll probably have to do manually. – Zeiss Ikon Feb 17 '21 at 10:08
  • So why don't you create the folder /media/[oldusername]/sdc9 and mount your partition there? As long as you use the same /path/to/mointpoint you used when creating the symlinks, they will work again. – mook765 Feb 17 '21 at 12:45
  • Sigh. I'm clearly just not lazy enough... I'll give that a try. Ideally, I'd like to be able to navigate to those from the root of that partition, but I guess I can mount all the partitions on the old NTFS platter drive that way. – Zeiss Ikon Feb 17 '21 at 14:04
0

Find all files in the current directory that are a symlink to /media/user/sdc9:

$ find . -ilname '/media/user/sdc9'

Find all files in the current directory that are a symlink to /media/user/sdc9, and replace them with a symlink to /sdc9:

$ find . -ilname '/media/user/sdc9' -exec ln -vfs /sdc9 {} \;

The {} is replaced by the file name and the \; signals the end of the command to be executed. This will operated on each single file that is found, in turn.

If your user doesn't have write permission you will need to do this as root.

Be careful if you're going to do this in some file tree that may have other filesystems mounted; if you did this from the root directory then it will recurse down into /proc/ and /sys/ and probably find itself in an infinite loop. You may want to use the -xdev option to prevent find from crossing a mount point.

If you absolutely only want this to operate on the directories you list and not recurse downwards, use the -maxdepth 1 option.

Don't try to do this by parsing ls; it may work most of the time but eventually you will come unstuck.

grifferz
  • 1,006