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.

  • Why? what is special about "one command"? what actual problem are you trying to solve? Oct 24, 2020 at 0:31

3 Answers 3


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

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

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