Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagebash
mkdir ~/test; cd ~/test
ln -s -f /stor/work/CCBB_Workshops_1/bash_scripting/data/sampleinfo.txt
ls -l

Multiple files can be linked by providing multiple file name arguments along and using the -t (target) option to specify the directory where links to all the files can be created.

Code Block
languagebash
cd; rm -f test/*.*rf ~/test; mkdir ~/test; cd ~/test
ln -s -f -t test. /stor/work/CCBB_Workshops_1/bash_scripting/data/*.txt
ls -l

What about the case where the files you want are scattered in sub-directories? Here's a solution using find and xargs:

  • find returns a list of matching file paths on its standard output
  • the paths are piped to the standard input of xargs
  • xargs takes the data on itsĀ standard input and calls the specified function (here ln) with that data as the function's argument list.

Code Block
languagebash
cd; rm -f test/*.*rf ~/test; mkdir ~/test; cd ~/test
find ~/workshop//stor/work/CCBB_Workshops_1/bash_scripting -name "*.txt" | xargs ln -s -f -t ~/test.

Removing file suffixes

Sometimes you want to take a file path like ~/my_file.something.txt and extract some or all of the parts before the suffix, for example, to end up with the text my_file here. To do this, first strip off any directories using the basenamefuncitonfunction. Then use the odd-looking syntax ${<variable-name>%%.<suffix-to-remove>}

Code Block
languagebash
path=~/my_file.something.txt; echo $path
filename=`basename $path`; echo $filename
prefix=${filename%%.something.txt}
echo $prefix

...