Is There a Simple Way to Batch Rename Symlink Targets

Is there a simple way to batch rename symlink targets?

util-linux-2.23


rename(1):
- supports new command line option --symlink to rename symlink target

Batch rename target of symlinks to name of symlink

Using find :

find . -type l -name '*.mpg' -exec bash -c '
d=$(readlink "$1")
echo unlink "$1"
echo mv "$d" "$1"
' -- {} \;

Remove the 2 echo commands when the output looks good

or using find and rename :

find . -type l -name '*.mpg' -exec bash -c '
echo unlink "$1"
rename -n 's/(.*)/readlink $1/e "$1"
' -- {} \;

Remove the echo command and the -n when the output looks good

How to batch append subdirectory to link target in bash

Please try the following:

find . -type l -maxdepth 1 -print0 | while IFS= read -r -d "" link; do
target="$(readlink "$link")"
ln -nfs "$target/new" "$link"
done

Hope this helps.

Creating symbolic link to all files/folders in a path

Well if you're only using one % for your variables, I'd assume you're doing this from cmd prompt. %~dp0 won't resolve from the cmd prompt. You have to put that in a batch file and double the %'s. Since you're using the /d switch for mklink That implies that you want to create a directory symbolic link. If that's the case, use:

For /d %%f in (d:\folder1\*) do mklink /d "%%~df\%%~nflink" "%~dp0%%~nf"

Notice there is no backslash. %%~dp0 already includes it.

If you need to create simlinks to files as well, use

For /f "tokens=*" %%f in ('dir /s /b /a-d "d:\folder1"') do mklink "%%~dpnflink" "%~dp0%%~nxf"

How to create symbolic links for multiple files in multiple folders using command prompt or power shell?

You can try something link this:

function createSymbolicLinks ($source, $destination, [switch]$recurse) {
Get-ChildItem $source -Recurse:$recurse | ? { !$_.PSISContainer } | % {
$destpath = $_.Fullname -replace [regex]::Escape($source), $destination
if(!(Test-Path (Split-Path $destpath))) {
#Create missing subfolders
New-Item (Split-Path $destpath) -ItemType Directory -Force | Out-Null
}
cmd /c mklink $destpath $($_.FullName) | Out-Null
}
}

#Create symbolic links in c:\folderC for all files in d:\folderD(with recursive search)
createSymbolicLinks -source d:\folderD -destination c:\folderC -recurse

I believe this will fail if the same filename already exists in c:\folderc. So if you need to replace a file in c:\folderc with a symbolic link from d:\folderd, you need to extend it to remove the existing file.

UPDATE: This will only go down one level with the recurseoption. It's not the prettiest solution, but it should work.

function createSymbolicLinks ($source, $destination, [switch]$recurse) {
Get-ChildItem $source | % {
if($_.PSIsContainer -and $recurse) {
Get-ChildItem $_.FullName
} else {
$_
}
} | ? { !$_.PSIsContainer } | % {
$destpath = $_.Fullname -replace [regex]::Escape($source), $destination
if(!(Test-Path (Split-Path $destpath))) {
#Create missing subfolders
New-Item (Split-Path $destpath) -ItemType Directory -Force | Out-Null
}
cmd /c mklink $destpath $($_.FullName) | Out-Null
}
}

#Create symbolic links in c:\folderC for all files in d:\folderD(with recursive search)
createSymbolicLinks -source d:\folderD -destination c:\folderC -recurse

Git symbolic links in Windows

You can find the symlinks by looking for files that have a mode of 120000, possibly with this command:

git ls-files -s | awk '/120000/{print $4}'

Once you replace the links, I would recommend marking them as unchanged with git update-index --assume-unchanged, rather than listing them in .git/info/exclude.



Related Topics



Leave a reply



Submit