:::: MENU ::::

Create multiple symlink by changing name with command find

  • Mar 24 / 2017
  • 0
Linux

Create multiple symlink by changing name with command find

Let’s imagine that we have the following directory listing:

$ ls -ls /data/db1/
total 4778688
   1488 -rw-r--r-- 1 dbmaster      760264 Feb 14 00:20 database_2016_file1
    496 -rw-r--r-- 1 dbmaster      251529 Dec 29 12:45 database_2016_file2
 808960 -rw-r--r-- 1 dbmaster   414187520 Jan 30 10:20 database_2016_file3
    344 -rw-r--r-- 1 dbmaster      172544 Mar 18 00:42 database_2016_file4
 958528 -rw-r--r-- 1 dbmaster   490764886 Feb  7 12:18 database_2016_file5
  19480 -rw-r--r-- 1 dbmaster     9970189 Mar  2 17:31 database_2016_file6
2813952 -rw-r--r-- 1 dbmaster  1440743424 Mar 20 10:00 database_2016_file7
 160336 -rw-r--r-- 1 dbmaster    82088969 Mar 22 10:30 database_2016_file8
  14632 -rw-r--r-- 1 dbmaster     7490048 Mar 16 16:13 database_2016_file9
    104 -rw-r--r-- 1 dbmaster       51525 Mar  2 16:18 database_2016_idx1
     32 -rw-r--r-- 1 dbmaster       16114 Dec 14 18:55 database_2016_idx2
    144 -rw-r--r-- 1 dbmaster       71056 Dec  6 13:32 database_2016_idx3
     40 -rw-r--r-- 1 dbmaster       18214 Mar  9 12:38 database_2016_idx4
    104 -rw-r--r-- 1 dbmaster        8686 Jan  5 09:10 database_2016_idx5
     48 -rw-r--r-- 1 dbmaster       21316 Dec 14 20:01 database_2016_idx6

We would like to get a copy of those files named 2016 with a new name for the 2017 year, but we need to keep same content and retro-compatibility. For that, it’s easier to create some symbolic links of all those files instead of doing copies.

You can so use this command for creating those symlinks:

find /data/db1/ -type f -name "database_2016*" -exec bash -c 'ln -s $1 ${1/2016/2017}' -- {} ;

We are now getting this, as expected:

$ ls -ls /data/db1/
total 4778808
   1488 -rw-r--r-- 1 dbmaster      760264 Feb 14 00:20 database_2016_file1
    496 -rw-r--r-- 1 dbmaster      251529 Dec 29 12:45 database_2016_file2
 808960 -rw-r--r-- 1 dbmaster   414187520 Jan 30 10:20 database_2016_file3
    344 -rw-r--r-- 1 dbmaster      172544 Mar 18 00:42 database_2016_file4
 958528 -rw-r--r-- 1 dbmaster   490764886 Feb  7 12:18 database_2016_file5
  19480 -rw-r--r-- 1 dbmaster     9970189 Mar  2 17:31 database_2016_file6
2813952 -rw-r--r-- 1 dbmaster  1440743424 Mar 20 10:00 database_2016_file7
 160336 -rw-r--r-- 1 dbmaster    82088969 Mar 22 10:30 database_2016_file8
  14632 -rw-r--r-- 1 dbmaster     7490048 Mar 16 16:13 database_2016_file9
    104 -rw-r--r-- 1 dbmaster       51525 Mar  2 16:18 database_2016_idx1
     32 -rw-r--r-- 1 dbmaster       16114 Dec 14 18:55 database_2016_idx2
    144 -rw-r--r-- 1 dbmaster       71056 Dec  6 13:32 database_2016_idx3
     40 -rw-r--r-- 1 dbmaster       18214 Mar  9 12:38 database_2016_idx4
    104 -rw-r--r-- 1 dbmaster        8686 Jan  5 09:10 database_2016_idx5
     48 -rw-r--r-- 1 dbmaster       21316 Dec 14 20:01 database_2016_idx6
      8 lrwxr-xr-x 1 dbmaster          30 Mar 24 13:59 database_2017_file1 -> /data/db1/database_2016_file1
      8 lrwxr-xr-x 1 dbmaster          30 Mar 24 13:59 database_2017_file2 -> /data/db1/database_2016_file2
      8 lrwxr-xr-x 1 dbmaster          30 Mar 24 13:59 database_2017_file3 -> /data/db1/database_2016_file3
      8 lrwxr-xr-x 1 dbmaster          30 Mar 24 13:59 database_2017_file4 -> /data/db1/database_2016_file4
      8 lrwxr-xr-x 1 dbmaster          30 Mar 24 13:59 database_2017_file5 -> /data/db1/database_2016_file5
      8 lrwxr-xr-x 1 dbmaster          30 Mar 24 13:59 database_2017_file6 -> /data/db1/database_2016_file6
      8 lrwxr-xr-x 1 dbmaster          30 Mar 24 13:59 database_2017_file7 -> /data/db1/database_2016_file7
      8 lrwxr-xr-x 1 dbmaster          30 Mar 24 13:59 database_2017_file8 -> /data/db1/database_2016_file8
      8 lrwxr-xr-x 1 dbmaster          30 Mar 24 13:59 database_2017_file9 -> /data/db1/database_2016_file9
      8 lrwxr-xr-x 1 dbmaster          29 Mar 24 13:59 database_2017_idx1 -> /data/db1/database_2016_idx1
      8 lrwxr-xr-x 1 dbmaster          29 Mar 24 13:59 database_2017_idx2 -> /data/db1/database_2016_idx2
      8 lrwxr-xr-x 1 dbmaster          29 Mar 24 13:59 database_2017_idx3 -> /data/db1/database_2016_idx3
      8 lrwxr-xr-x 1 dbmaster          29 Mar 24 13:59 database_2017_idx4 -> /data/db1/database_2016_idx4
      8 lrwxr-xr-x 1 dbmaster          29 Mar 24 13:59 database_2017_idx5 -> /data/db1/database_2016_idx5
      8 lrwxr-xr-x 1 dbmaster          29 Mar 24 13:59 database_2017_idx6 -> /data/db1/database_2016_idx6

Comments are closed.

Question ? Contact