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 .
3 Answers
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.
You don't say what you want to link to.
Use a while
loop without cat
. If there are spaces in the filenames this method will handle that.
while read -r line; do ln -s "$line" target; done < file_list.txt
You will have to supply the way to derive the name of the target since you didn't specify it in your question.
Consider something like (untested):
for targ in $(cat file_list);
do
tfile=$(basename $targ)
ln -s $targ $tfile
done
Note that the You can have base name strip any single given filename extension if you so desire (use the basename
will strip any file extention like naming (i.e. ".exe" or whatever). This can be fixed but it is a pain.-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
-
-
@Dennis: For some reason I had that behavior reverse in my head. Thanks. Feb 8, 2010 at 0:29
-
BSD and OS X (BSD based)
basename
has-s
, others (Linux, HPUX, AIX, etc.) don't. For the ones that do, you can still do the extension stripping without-s
for portability. Feb 8, 2010 at 1:42