I made a copy of all the backups made on Wed of every week The time stamp of each file is not sorted but the day is Wednesday of every file

Now, I need to sort each file based on time Stamp e.g if date is 1-May then it should first display the backup files of 1-May then it should display the files of 8-May

I used this command but of course it is slapping me with error

sort $(cat /home/emerg/Wedbackup.txt)

Error is

sort: invalid option -- w

As i don't know how to use the output of one command as input of 2nd command. I need advice how to do it.


There's no need to use cat in this case:

sort /home/emerg/Wedbackup.txt

The problem with your example is that your file is being passed as the command line to sort, which is not what you want. For example, if this was your file:

foo bar
baz qux
wibble wobble

The arguments would look like this:

sort foo bar baz qux wibble wobble

This is not what you want. What you actually want is to pass the file to sort on stdin, which can be done like this:

sort < /home/emerg/Wedbackup.txt

This is more generalisable, as taking a filename as an argument is specific to sort, and is not a universal convention.

In the case of sort, you should prefer to pass the filename as an argument rather than on stdin, as it allows seeking on the file, which can improve sorting efficiency.


You should use pipe, like this:

cat /home/emerg/Wedbackup.txt | sort

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.