What is wrong with shell expansion expansion? (Assuming there is only one file name.)
% tar -cvf *mysearchstring* % tar -xvf *mysearchstring*
Of course, a file name that matches * mysearchstring * will have spaces. But the shell [tcsh, bash] will assign this file name , including its spaces , to one tar argument.
You can verify this with a simple C or C ++ program. For instance:.
#include <iostream> using namespace std; int main( int argc, char **argv ) { cout << "argc = " << argc << endl; for ( int i = 0; i < argc; i ++ ) cout << "argv[" << i << "] = \"" << argv[i] << "\"" << endl; return 0; }
Try: ./a.out * foo *
This is why CSH has a parameter: q [quoted wordlist] ...
eg. [Tcsh]
foreach FILE ( *mysearchstring* ) tar -xvf $FILE:q end
Or tar -xvf "$ FILE" instead of tar -xvf $ FILE: q if you prefer.
If you really have to use ls ...
Using ls passed through anything yields one file name per line. Using tr, we can translate newlines to any other character, such as null. xargs can accept null-terminated strings. This circumvents the problem of spaces. Using -n1 will overcome the problem with multiple files.
For example: \ ls | grep mysearchstring | tr '\ 012' '\ 0' | xargs --null -n1 tar -xvf
But I don’t believe tar contains several tarfiles from stdin, as the OP does ...
source share