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 to use only the command echo in one command. So far I've done:
cd dataand then
echo * > ../results.txtIt does work but not in the same command.
13 Answers
Clearly you could avoid the cd by doing
echo data/* > results.txthowever 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.txtDon'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.