If you want to save the output of a command to a file, then you can make use of the redirection operators.
Redirection Operators:
Operator | Description |
---|---|
> |
It redirects standard output to a file. Note that it will overwrite the file's content if it exists. |
>> |
It redirects standard output to a file. Note that it will append the output to the end of the file. |
< |
It redirects standard input from a file by providing input to a command from the contents of the file. |
2> |
It redirects standard error (file descriptor 2) to a file, overwriting the file's content if it exists. |
2>> |
It redirects standard error (file descriptor 2) to a file, appending the output to the end of the file. |
&> |
It redirects both standard output and standard error to a file, overwriting the file's content if it exists. |
&>> |
It redirects both standard output and standard error to a file, appending the output to the end of the file.. |
>& |
Redirects one file descriptor to another file descriptor. For example, 2>&1 redirects standard error to standard output. |
Now let us see a few simple examples of how to redirect a bash command output to a file.
bash-3.2$ ls -ltrh > /Users/c2ctechtv/Desktop/ls_command_output.txt
bash-3.2$ cat /Users/c2ctechtv/Desktop/ls_command_output.txt
total 0
drwxr-xr-x 5 root wheel 160B Aug 5 11:51 standalone
drwxr-xr-x 42 root wheel 1.3K Aug 5 11:51 share
drwxr-xr-x 229 root wheel 7.2K Aug 5 11:51 sbin
drwxr-xr-x 357 root wheel 11K Aug 5 11:51 libexec
drwxr-xr-x 32 root wheel 1.0K Aug 5 11:51 lib
drwxr-xr-x 981 root wheel 31K Aug 5 11:51 bin
lrwxr-xr-x 1 root wheel 25B Aug 5 11:51 X11R6 -> ../private/var/select/X11
lrwxr-xr-x 1 root wheel 25B Aug 5 11:51 X11 -> ../private/var/select/X11
drwxr-xr-x 5 root wheel 160B Aug 11 07:36 local

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Bash,
- Bash Hello World! Script Tutorial
- How to run bash command in background
- How to create new user account in Windows bash
- How to Compare Strings in Bash
- Bash: Allow Command to Fail without exiting Script
- Fix bash: script.sh: Permission denied Error
- Bash getopts Command Example
- How to fix bash ping command not found error
- How to add NewLine Character in Bash Script String
- How to Echo Bash Command to a File
- Bash Command to Find String in a File
- How to open new Terminal using Bash Command
- Bash How to Save Output of a Command to a Variable
- Download a SSL Certificate from a URL in Terminal
- Bash Command to Check IP Address
- Know Bash shell version command
- Bash Command To Check If File Exists
- How to run a Command in Bash Script
- How to Compare two Files in Bash Shell
- How to check if a variable is set in Bash Script or Not
- Convert String from uppercase to lowercase in Bash
- How to see Created Accessed Modified and Changed dates of a file using bash terminal command
- Bash Command To Get Current Time
- Bash command to List Files
- Fix: bash: syntax error near unexpected token
More Posts:
- 24: Append One String to Another in Python Program - Python-Programs
- Vertically Center Text in a DIV Element - CSS
- Fix: Error: error:0308010C:digital envelope routines::unsupported NodeJs/Vue/React - JavaScript
- SQLite Error: unknown command or invalid arguments: open. Enter .help for help - Android
- Get HTTP Request Response Headers Safari Browser - MacOS
- iOS 14 Airpods Connected message everytime when the iPhone is unlocked - Apple
- Ubuntu zsh: command not found: nano - zsh
- Program 12: Calculate Area and Circumference of Circle - 1000+ Python Programs - Python-Programs