How do I redirect command output to a file?

I have a command that I run in a folder that outputs MD5 hashes and filenames on the terminal:

ls |sort -nr | xargs md5sum

I need this output in a text file that I can download and compare to another folder on another customer’s machine. How can I modify the command such that its output is stored in a file in say /tmp? I’m using Redhat 5.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It’s a bad idea to parse the output of ls. The primary job of ls is to list the attributes of files (size, date, etc.). The shell itself is perfectly capable of listing the contents of a directory, with wildcards.

It’s quite simple to run md5sum on all the files in the current directory and put the output in a file: redirect its output to the desired output file.

md5sum * >/tmp/md5sums.txt

If you want the output to be sorted by file name, pipe the output of md5sum into sort.

md5sum * | sort -k 2 >/tmp/md5sums.txt

Note that numeric sorting (-n) will only give useful results if the file names are purely numeric. If all you need is for the output to be deterministic, how you sort doesn’t matter.

Method 2

output redirecting is done by below command

commandname > filename

Method 3

Same way as on most non-*nix platforms, actually.

somecommand > somefile

You don’t need to sort it though, as md5sum -c will check the files for you.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x