I was reviewing one shell code and I found this command written between other shell code


I don't know what this command does, so I tried it on my desktop. I made one shell script and I wrote this command inside my shell script and when I ran that, I found that it doesn't do anything. What does this >myfile.txt do??

  • 1
    Actually it was just >filename.txt there was nothing before '>' sign – ProgBeginner Mar 22 '17 at 18:28
  • It's to clear the file. The keyword is "nothing". See my answer below. – Alxs Mar 22 '17 at 18:38

As you have written with nothing preceding the redirect symbol >:


is to literally redirect nothing into filename.txt.  This is commonly done to clear/erase the contents of a text file.  If filename.txt does not already exist, it will be created.


The command > myfile.txt will just create a file or clear the file content, if any.

This command is also to attribute something to a .txt file, if there is a command before. For example, you want to create a file with the content of a folder so you can do ls -1 > myfile.txt. This will create a file with the names of your files and directories by line.

You also can use it as a logfile of your script, so instead of show the output in the screen this command save the output in a file.

  • There was no command written before '>' symbol so it was not output redirection – ProgBeginner Mar 22 '17 at 18:33
  • 1
    Then the command > myfile.txt will just create a file or clear the file content, if any. – MaiaraSC Mar 22 '17 at 18:53
  • You should add this to your answer. OP does not ask what ls -1 > filename.txt is supposed to do, but what > filename.txt (alone) is supposed to do. – xhienne Mar 22 '17 at 19:01

script.sh > file.txt overwrites the file with the output of the script. Whereas, script.sh >> file.txt appends text to the end of the file.

You can redirect any output from any program on the command line to a file like this. However, this only redirects STDOUT, if you want errors in the file, too, use something like script.sh > file.txt 2>&1. This sends STDERR to STDOUT, which is being written to a file.

  • No there was no command written before '>' symbol it was just >filename.txt so it can not be considered as output redirection of any command to filename.txt – ProgBeginner Mar 22 '17 at 18:37

There is no command like filename.txt. It's only an text file named as filename.

As you asked in the title

What does >filename.txt does in shell script

">" is used for redirecting the output of the command into the text file. for example if you run the below command in the terminal

ifconfig > filename.txt

then the output of command ifconfig will not display in the terminal. but a new file will create on your working directory or overwrite if the same file is present there. You can read the output of the command from the file filename.txt.

  • The appearance of the filename was a formatting issue. – Stephen Kitt Mar 22 '17 at 18:22

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.