Skip to main content
added <> to placeholders
Source Link

Quick One-liner

find /home/user/public_html/qa -type l -print0 | xargs -0 -n1 -P0 sh -c 'NEWLINK=$(readlink "$3" | sed -e "s|${1}|${2}|"); ln -snf "$NEWLINK" "$3";' _ "/dev/" "/qa/"

Shouldn't grab anything with dev or qa in their name.

Explain

find PATH <PATH> -type l -print0

Find all links under PATH

xargs -0 -n1 -P0 CMD <CMD>

For every input (null delimited), run CMD with 1 parameter, as parallel as possible

CMD = sh -c 'SCMD' '<SCMD>' _ "ARG1" "ARG2" ARG3

Run a shell SCMD with arguments (3 in this case)

SCMD

NEWLINK=$(readlink "$3" | sed -e "s|${1}|${2}|"); 
ln -snf "$NEWLINK" "$3";

Get original link then modify with sed using ARG1 and ARG2.
Update link with new value.

Can be compressed to: ln -snf $(readlink "$3" | sed -e "s|${1}|${2}|") "$3" but then becomes less readable and could break with /some/path with spaces/here in certain contexts

Quick One-liner

find /home/user/public_html/qa -type l -print0 | xargs -0 -n1 -P0 sh -c 'NEWLINK=$(readlink "$3" | sed -e "s|${1}|${2}|"); ln -snf "$NEWLINK" "$3";' _ "/dev/" "/qa/"

Shouldn't grab anything with dev or qa in their name.

Explain

find PATH -type l -print0

Find all links under PATH

xargs -0 -n1 -P0 CMD

For every input (null delimited), run CMD with 1 parameter, as parallel as possible

CMD = sh -c 'SCMD' _ "ARG1" "ARG2" ARG3

Run a shell SCMD with arguments (3 in this case)

SCMD

NEWLINK=$(readlink "$3" | sed -e "s|${1}|${2}|"); 
ln -snf "$NEWLINK" "$3";

Get original link then modify with sed using ARG1 and ARG2.
Update link with new value.

Can be compressed to: ln -snf $(readlink "$3" | sed -e "s|${1}|${2}|") "$3" but then becomes less readable and could break with /some/path with spaces/here in certain contexts

Quick One-liner

find /home/user/public_html/qa -type l -print0 | xargs -0 -n1 -P0 sh -c 'NEWLINK=$(readlink "$3" | sed -e "s|${1}|${2}|"); ln -snf "$NEWLINK" "$3";' _ "/dev/" "/qa/"

Shouldn't grab anything with dev or qa in their name.

Explain

find <PATH> -type l -print0

Find all links under PATH

xargs -0 -n1 -P0 <CMD>

For every input (null delimited), run CMD with 1 parameter, as parallel as possible

CMD = sh -c '<SCMD>' _ "ARG1" "ARG2" ARG3

Run a shell SCMD with arguments (3 in this case)

SCMD

NEWLINK=$(readlink "$3" | sed -e "s|${1}|${2}|"); 
ln -snf "$NEWLINK" "$3";

Get original link then modify with sed using ARG1 and ARG2.
Update link with new value.

Can be compressed to: ln -snf $(readlink "$3" | sed -e "s|${1}|${2}|") "$3" but then becomes less readable and could break with /some/path with spaces/here in certain contexts

make relevant to question
Source Link

Quick One-liner

find /pathhome/user/public_html/qa -type l -print0 | xargs -0 -n1 -P0 sh -c 'NEWLINK=$(readlink "$3" | sed -e "s|${1}|${2}|"); ln -snf "$NEWLINK" "$3";' _ "/path/todev/replace"" "/path/replaceqa/with""

Shouldn't grab anything with dev or qa in their name.

Explain

find /pathPATH -type l -print0

Find all links under /pathPATH

xargs -0 -n1 -P0 CMD

For every input (null delimited), run CMD with 1 parameter, as parallel as possible

CMD = sh -c 'SCMD' _ "ARG1" "ARG2" ARG3

Run a shell SCMD with arguments (3 in this case)

SCMD

NEWLINK=$(readlink "$3" | sed -e "s|${1}|${2}|"); 
ln -snf "$NEWLINK" "$3";

Get original link then modify with sed using ARG1 and ARG2.
Update link with new value.

Can be compressed to: ln -snf $(readlink "$3" | sed -e "s|${1}|${2}|") "$3" but then becomes less readable and could break with /some/path with spaces/here in certain contexts

Quick One-liner

find /path -type l -print0 | xargs -0 -n1 -P0 sh -c 'NEWLINK=$(readlink "$3" | sed -e "s|${1}|${2}|"); ln -snf "$NEWLINK" "$3";' _ "/path/to/replace" "/path/replace/with"

Explain

find /path -type l -print0

Find all links under /path

xargs -0 -n1 -P0 CMD

For every input (null delimited), run CMD with 1 parameter, as parallel as possible

CMD = sh -c 'SCMD' _ "ARG1" "ARG2" ARG3

Run a shell SCMD with arguments (3 in this case)

SCMD

NEWLINK=$(readlink "$3" | sed -e "s|${1}|${2}|"); 
ln -snf "$NEWLINK" "$3";

Get original link then modify with sed using ARG1 and ARG2.
Update link with new value.

Can be compressed to: ln -snf $(readlink "$3" | sed -e "s|${1}|${2}|") "$3" but then becomes less readable and could break with /some/path with spaces/here in certain contexts

Quick One-liner

find /home/user/public_html/qa -type l -print0 | xargs -0 -n1 -P0 sh -c 'NEWLINK=$(readlink "$3" | sed -e "s|${1}|${2}|"); ln -snf "$NEWLINK" "$3";' _ "/dev/" "/qa/"

Shouldn't grab anything with dev or qa in their name.

Explain

find PATH -type l -print0

Find all links under PATH

xargs -0 -n1 -P0 CMD

For every input (null delimited), run CMD with 1 parameter, as parallel as possible

CMD = sh -c 'SCMD' _ "ARG1" "ARG2" ARG3

Run a shell SCMD with arguments (3 in this case)

SCMD

NEWLINK=$(readlink "$3" | sed -e "s|${1}|${2}|"); 
ln -snf "$NEWLINK" "$3";

Get original link then modify with sed using ARG1 and ARG2.
Update link with new value.

Can be compressed to: ln -snf $(readlink "$3" | sed -e "s|${1}|${2}|") "$3" but then becomes less readable and could break with /some/path with spaces/here in certain contexts

Source Link

Quick One-liner

find /path -type l -print0 | xargs -0 -n1 -P0 sh -c 'NEWLINK=$(readlink "$3" | sed -e "s|${1}|${2}|"); ln -snf "$NEWLINK" "$3";' _ "/path/to/replace" "/path/replace/with"

Explain

find /path -type l -print0

Find all links under /path

xargs -0 -n1 -P0 CMD

For every input (null delimited), run CMD with 1 parameter, as parallel as possible

CMD = sh -c 'SCMD' _ "ARG1" "ARG2" ARG3

Run a shell SCMD with arguments (3 in this case)

SCMD

NEWLINK=$(readlink "$3" | sed -e "s|${1}|${2}|"); 
ln -snf "$NEWLINK" "$3";

Get original link then modify with sed using ARG1 and ARG2.
Update link with new value.

Can be compressed to: ln -snf $(readlink "$3" | sed -e "s|${1}|${2}|") "$3" but then becomes less readable and could break with /some/path with spaces/here in certain contexts