0

I am extracting names of directory at 5th column of tsv file ExtensionList.tsv and I want to create symlinks to all those folders with names beginning with that located at ${HOME} inside Extension folder

awk -F'\t' '{print $5 }' ExtensionList.tsv \
    | xargs -L 1 -I "{}" ln -s -f "${HOME}/{}"* Extension

Sample Tsv file

2   n   xshrim.txt-syntax   Txt Syntax  xshrim.txt-syntax

I want to do (for all records in tsv file)

ln -s -f xshrim.txt-syntax* Extension

Any suggestions?

Why did my command didn't work?

3
  • You need awk '{print $6}' since the name you want is in column 6.
    – sseLtaH
    Commented Jul 24, 2021 at 21:44
  • @HatLess It is tab separated file! there are only 5 columns
    – Porcupine
    Commented Jul 24, 2021 at 21:46
  • Yes. Now it is 5 columns. When I responded, it was 6 columns. You have since added the - to Txt Syntax which was space seperated. awk uses space or tab delimiters by default. Removing the -F"\t" from your code works.
    – sseLtaH
    Commented Jul 25, 2021 at 9:09

2 Answers 2

1

The glob expansion must be done for each name read from the TSV file separately. This can be done by calling sh -c from xargs once for each directory name.

cut -f 5 ExtensionList.tsv | 
  xargs -I {} sh -c 'ln -s -f "$HOME/$1"* Extension/' sh {}

Note that it assumes file names in that fifth column don't start with blanks and don't contain backslash, single quote nor double quote characters. With the GNU implementation of xargs, adding -d '\n' works around those issues.

To avoid running one sh per file:

cut -f 5 ExtensionList.tsv | 
  xargs sh -c '
    ret=0
    for dirpath do
      ln -s -f "$HOME/$dirpath"* Extension/ || ret=$?
    done
    exit "$ret"' sh

On top of the limitations above, file paths must also not contain blanks anywhere. That extra limitation is also avoided with GNU xargs' -d '\n'.


Refer: is there a way to create symbolic link with wildcard - Stack Overflow

0

With zsh:

typeset -U prefixes=( ${(f)"$(cut -f5 < ExtensionList.tsv)"} )
typeset -U regular_files_with_those_prefixes=( ~/$^prefixes*(N.) )
if (( $#regular_files_with_those_prefixes )); then
  ln -sf -- $regular_files_with_those_prefixes Extensions
fi

Where:

  • typeset -U: makes sure the array elements are unique (are deduplicated)
  • ${(f)"$(cmd)"}: splits the output of cmd on line feeds.
  • x/$^array when array=(a b c) expands to x/a x/b x/c instead of x/a b c for x/$array.
  • (N.): glob qualifiers: N for Nullglob for globs to expand to nothing when there's no match. . to restrict the glob expansion to regular files to the exclusion of any other type of files (fifos, directories, symlinks...).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .