Quick Linux Question - redirecting output from cp to run dos2unix

statik213

Golden Member
Oct 31, 2004
1,654
0
0
I frequently need to copy text files that are updated in one folder to another. I also need to run dos2unix (to convert line terminations from crlf to lf) on each file that gets copied....
My script does something like this,

cp -uv /${DIR}/${file} ./

I'd like to run dos2unix on the files that are copied. I know that you can do something like this:
ls | sort

to call sort on the output from ls...

so, I want to do something like this:
cp -uv /${DIR}/${file} ./ | dos2unix -k
(the -k keeps the same timestamp as the source file)

which kinda work, but I get a prompt from cp, like so:
cp: overwrite `./abstractSyntaxTree.cc'?

why am I getting this prompt?
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
nvm....

figured it out, the -f in the cp wasn't doing what i expected....

but anway, is this the right of doing it? should i be doing things differently?
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
cp doesn't output the file's contents, so it makes no sense to pipe the cp command through dos2unix. You would want to run dos2unix on the files independently.

The prompt is likely because your shell startup scripts are pure evil and alias cp to cp -i. Try using \cp or `which cp` instead of just plain cp if you want to get around that. Or you could pipe 'yes' through it but the other solutions are simpler.

Other nitpicks: curly brackets are only needed to disambuguate. like if you wanted to create a file called <whatever>file.txt, using the variable $foo, doing $foofile.txt would check the variable $foofile, not $foo. So you'd need to do ${foo}file. But in your case the variables are touching slashes so there's no ambiguity, since a variable name can't have a slash in it. Also, quoting is always a good idea. If $DIR or $file has a space in it, your code will break unless you quote.

\cp -uv "/$DIR/$file" .
<run dos2unix on "$file">

I don't know the correct way to run dos2unix on an individual file, but you seem to have a grasp of the man page. Might be -f. Too lazy to install and look.

And WTF is -u?
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
Originally posted by: n0cmonkey
for i in * ; do
cp $i /where/ever
dos2unix /where/ever/$i
done

but I don't want to copy all the files, just ones that are updated...

 

bersl2

Golden Member
Aug 2, 2004
1,617
0
0
Originally posted by: statik213
Originally posted by: n0cmonkey
for i in * ; do
cp $i /where/ever
dos2unix /where/ever/$i
done

but I don't want to copy all the files, just ones that are updated...

s/cp/cp -u/ in the above.
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
Originally posted by: statik213
Originally posted by: n0cmonkey
for i in * ; do
cp $i /where/ever
dos2unix /where/ever/$i
done

but I don't want to copy all the files, just ones that are updated...

If that's what -uv does, you can easily add that in like bersl2 said. :confused: