Home » Questions » Computers [ Ask a new question ]

how to make softlink to bunch of files in one shell comand

how to make softlink to bunch of files in one shell comand

I'm under linux and use bash as my login shell . I need to make softlink to a bunch of files. Thoes files are listed in a text file , let's say file_list.txt . Every line in file_list.txt is just a full path of one target file need to be linked . Although I don't know exactly how to, I do believe there is way to combine "cat " "| " and "ln -s " together to make the job done whithin one shell command . So need your help .

Asked by: Guest | Views: 151
Total answers/comments: 2
bert [Entry]

"If you want the names in the current directory (or any one directory), and if the names in the file are all absolute names, or are relative names correct relative to the target directory, then:

ln -s $(<file_list.txt) .

If you prefer to use 'cat', then:

ln -s $(cat file_list.txt) .

Clearly, I'm using the current directory as the target."
bert [Entry]

"Consider something like (untested):

for targ in $(cat file_list);
do
tfile=$(basename $targ)
ln -s $targ $tfile
done

Note that the basename will strip any file extention like naming (i.e. "".exe"" or whatever). This can be fixed but it is a pain. You can have base name strip any single given filename extension if you so desire (use the -s flag).

You can compress this into a single line like:

$ for targ in $(cat file_list); do tfile=$(basename $targ); ln -s $targ $tfile; done"