views:

125

answers:

4

HI,

in unix there are number of files in my directory. i want to change all the files to zero byte files. is that possible on a single command line?

for example:

-rw-r--r--   1 sumanma  dev          434 Jan  8 14:36 pprbc_NL.cpp
-rw-r--r--   1 sumanma  dev          488 Jan  8 14:37 pprbc_TreeBuild.cpp
-rw-r--r--   1 sumanma  dev          783 Jan  8 14:37 pprbc_UPDwm.cpp

i want to change these to

-rw-r--r--   1 sumanma  dev          0 Jan  8 14:36 pprbc_NL.cpp
-rw-r--r--   1 sumanma  dev          0 Jan  8 14:37 pprbc_TreeBuild.cpp
-rw-r--r--   1 sumanma  dev          0 Jan  8 14:37 pprbc_UPDwm.cpp

I am using tcsh

+3  A: 

Yes you can.

Try this

for i in *
do
 echo -n > $i
done

(Assuming bash here but the basic idea is the echo).

Edit : One liner (bash). You'll need the semicolons etc. which you won't need in the multiline version.

 for i in * ; do echo -n > $i ; done

Edit: If you're stuck using tcsh, you can always invoke bash to run a one liner like so

 bash -c 'for i in * ; do echo -n > $i ; done'
Noufal Ibrahim
as i said i need on command line this is not so useful.i can write the script but i want to know what is the command line trick for this.
Vijay Sarathi
Type that as a one liner into the command line and it will work as you want. You don't have to make it a script.
Noufal Ibrahim
` for i in test*fdef do echo -n >$i donei: Undefined variable.`
Vijay Sarathi
for i in *; do echo -n > $i; donedefinitely works in bash, just tested it. Do you use bash? did you miss the ; after * (or test*fdef in your case)?
Carsten
I've updated the answer with a one liner you can type in.
Noufal Ibrahim
`[28]> for i in test*fdef; do echo -n > $i; done;for: Command not found.i: Undefined variable.done: Command not found.`i am having tcsh
Vijay Sarathi
Surely, you can adapt it to `tcsh`. I'm not familiar with that shell but the basic idea is to loop over your files and `echo -n` into them.This is starting to sound like a *please solve my problem for me* question.
Noufal Ibrahim
Not really your fault at all, but this won't work in tcsh. (Aha, you guys seem to have noticed.)
DigitalRoss
Just FYI, you *can* enter multi-line scripts at a bash prompt. If you type something that's not a one-liner (e.g. the first line of a 'for' statement), the prompt will change to a '>' and allow you to enter the rest of the lines. The multi-line script will be executed after you type the terminating token (done, fi, etc).
Andrew Medico
@Andrew, that's why I first suggested it but the OP I think wanted a "command line *trick*"
Noufal Ibrahim
+1  A: 

I can't immediately think of an easy one-liner for tcsh.

Here is a three-liner:

foreach i (*)
  cat /dev/null > $i
end

You can type this in on the command line, i.e., it doesn't have to be a script. Unfortunately for one-liner purposes, and unlike the sh-series, the csh-derived shells parse the control structures by line, not by pipeline. This means that foreach i (*); echo $i; end won't work.

Of course, you could do a one-liner by switching shells for a line:

sh -c 'for i in *; do cat /dev/null > $i; done'
DigitalRoss
A: 
for file in *
do
   [ -f "$file" ] && >"$file"
done
ghostdog74
This is a nice sh script but he is using a csh-derived shell, and this won't work at all.
DigitalRoss
ah, i missed the comments
ghostdog74
A: 

For one file you can use

>file.cpp

For more then one you need a for loop as already described from others. If there is a directory in the current directory, then you recive an error message.

for f in *;do >$f;done

to avoid this use

for f in *;do [ -f $f ] && >$f;done