Create New Text File with Echo Only
I Have a Repertory Called 'Data' and Have Multiple Random Files in It. I Want to Create a 'Results. Txt' File with All the Names of the Files in 'Data'. I Want...
I have a repertory called 'data' and have multiple random files in it. I want to create a 'results.txt' file with all the names of the files in 'data'.
I want to use only the command echo in one command. So far I've done:
cd data
and then
echo * > ../results.txt
It does work but not in the same command.
3 Answers
Clearly you could avoid the cd by doing
echo data/* > results.txt
however this will result in the filenames in results.txt being prepended with the pathname data/.
In zsh, you could use the :t (tail) modifier to strip off the pathname:
echo data/*(:t) > results.txt # zsh only
Try this? Not using echo command which you mentioned but does the same thing..
ls -1a >../result.txt
No need to cd. You can do
ls data > results.txt
Don't put a * because that will cause all results to start with data/. However my answer did not use the echo command as you needed.