It is very important that Americans vote in the upcoming election. If you’ve voted, tag your post #IVoted!
MLTSHP
  • Popular
  • Join us! Sign up to post images and create your own shake.
    Sign Up!
  • sign in

redirects cheat sheet

cmd > file.txt
cmd >> file.txt
cmd < file.txt
cmd 2> file.txt
cmd > file.txt 2>&1
cmd1 | cmd2
cmd1 2>&1 | cmd2

three gotchas:

1. cmd file.txt > file.txt  will delete the contents of file.txt

some people use set -o noclobber (in bash/zsh) to avoid this

But I just have "never read from and redirect to the same file" seared into my memory.

2. sudo echo blah > /root/file.txt doesn't write to /root/file.txt as root. Instead, do:

echo blah | sudo tee /root/file.txt

or

sudo sh -c 'echo blah > /root/file.txt'

3. cmd 2>&1 > file.txt  doesn't write both stdout and stderr to file.txt. Instead, do:

cmd > file.txt 2>&1

panel 3: cat vs <

I almost always prefer to do:

cat file.txt | cmd

instead of

cmd < file.txt

it usually works fine & it feels better to me

using cat can be slower if it's a GIANT file though

panel 4: &> and &|

some shells support &> and &| to redirect/pipe both stdout and stderr

(also some shells use |& instead of &|)
alt text
cmd > file.txt
cmd >> file.txt
cmd < file.txt
cmd 2> file.txt
cmd > file.txt 2>&1
cmd1 | cmd2
cmd1 2>&1 | cmd2

three gotchas:

1. cmd file.txt > file.txt will delete the contents of file.txt

some people use set -o noclobber (in bash/zsh) to avoid this

But I just have "never read from and redirect to the same file" seared into my memory.

2. sudo echo blah > /root/file.txt doesn't write to /root/file.txt as root. Instead, do:

echo blah | sudo tee /root/file.txt

or

sudo sh -c 'echo blah > /root/file.txt'

3. cmd 2>&1 > file.txt doesn't write both stdout and stderr to file.txt. Instead, do:

cmd > file.txt 2>&1

panel 3: cat vs <

I almost always prefer to do:

cat file.txt | cmd

instead of

cmd < file.txt

it usually works fine & it feels better to me

using cat can be slower if it's a GIANT file though

panel 4: &> and &|

some shells support &> and &| to redirect/pipe both stdout and stderr

(also some shells use |& instead of &|)
source: https://social.jvns.ca/@b0rk...
1 week ago

Dan Phiffer pro

  • 730 Views
  • 4 Saves
  • 23 Likes

Post URL

https://mltshp.com/p/1R3YL

In These Shakes

  • Dan Phiffer
  • Post to Facebook
  • Post to Tumblr
cmayes 1 week ago
Julia's cheat sheets are excellent.
bezt pro 1 week ago
yay new b0rk!
travis pro 1 week ago
I can't even count the number of things that I have learned from Julia's cheat sheets over the years.

Follow @best_of_mltshp on Mastodon

Are you a developer? Check out our API.

© MLTSHP, a Massachusetts Mutual Aid Society venture All Rights Reserved

  Terms of Use   Code of Conduct   Contact Us

Follow The MLTSHP User!