I would like to redirect the command executed along with the output to a file.
for example: #ls >ls_out.txt
ls_out.txt should be something like :
ls
file1 fil2
Thanks!
You could use "script" command: Something like this:
localhost:test_sf user1$ ls
file1 file2 file3
localhost:test_sf user1$ script ls_out.txt #starting script command ls_out.txt will contain output
Script started, output file is ls_out.txt
bash-3.2$ ls
file1 file2 file3 ls_out.txt
bash-3.2$ exit
exit
Script done, output file is ls_out.txt
===================================================================================
localhost:test_sf user1$ cat ls_out.txt #verify the contents now.
Script started on Wed Dec 18 12:05:23 2013
bash-3.2$ ls
file1 file2 file3 ls_out.txt
bash-3.2$ exit
exit
You will just have to get rid off "bash-3.2$ exit exit" piece.
I use a function for this kind of thing:
echocmd() {
echo "$@"
"$@"
}
Then
$ echocmd ls -ltr > ls_out.txt
whoami
@hostname
""$" "$@" in place of echo "$@"