Celeb Glow
general | March 18, 2026

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 data

and then

echo * > ../results.txt

It does work but not in the same command.

1

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy