Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to create a file or folder and the same time write it to the .sh file?

For example: word count for a file, write that info to a new file and save it in the .sh file at the same time.

I tried:

echo 'wc filename.txt > ./folder/newfilename.txt' >>

it would give me the word count for file name in the folder, but in the sh file it would give me what's on the quotes.

share|improve this question

closed as unclear what you're asking by Radu Rădeanu, guntbert, BuZZ-dEE, Mitch, Anwar Shah Feb 24 '14 at 12:59

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

Can you provide a more complete example? What should "" look like? – Amanda Feb 23 '14 at 14:56
the results of the commands instead of what I typed in quotes – user251427 Feb 23 '14 at 15:03
Right. So please update your question to include a code block that shows us an example of that. – Amanda Feb 23 '14 at 15:09
@user251427 Did you mean to write output of wc filename.txt run from a .sh file to another text file at ./folder/newfilename.txt? – souravc Feb 23 '14 at 15:17
Can you clarify your question? What exactly do you want to write in the file? If you mean the output of wc filename.txt > ./folder/newfilename.txt this is always null if the command ends with success. So, nothing to write in file... – Radu Rădeanu Feb 23 '14 at 15:50

Echo will print whatever you've given it in quotes, literally. So

echo 'wc filename.txt > ./folder/newfilename.txt' >>

Should append this text to the end of

wc filename.txt > ./folder/newfilename.txt

If you want to write the output of wc filaname.txt to "" you'd just do wc filename.txt >> So if I do wc examples.desktop > foo.txt then foo.txt will contain the results of that command:

amanda@mona:~$ cat foo.txt 
 240  569 8980 examples.desktop

if you just want to keep a log of things you've done to or with filename.txt you can do history | grep filename.txt >

If that isn't what you're trying to accomplish, please clarify your question! To do something more complex, you're probably going to need to write a shell script that takes the input and does a few different things with it.

share|improve this answer

You can do this with tee and a process substitution:

echo 'wc filename.txt' | tee >(bash > ./folder/newfilename.txt) >>

The process substitution acts like a file for the tee command. tee will pipe the output of the echo command to both the file and to stdout (where the redirection appends it to the script). bash will read the command on its stdin

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.