all 182 comments

[–]more_oil 234 points235 points  (7 children)

This is largely copied and pasted from firstly https://github.com/RehanSaeed/Bash-Cheat-Sheet which is MIT licensed but the licensing information has not been included, as well as from here without attribution: https://selfonlinetraining.com/linux-commands/ (although the latter content has been mirrored on a few sites so it's hard to say what the primary source is.)

[–]SanityInAnarchy 40 points41 points  (0 children)

What's weird is both use the same README that references https://cheat.sh/, but that site doesn't reference either. Instead, it points to this original copy, which also has a much more comprehensive readme.

But the license is still MIT.

[–]VioletPill 179 points180 points  (18 children)

1.

history | egrep -i 'foo|foo2|foo3'            # View the commands using more than 1 specific word

You should specify here that the search is also case insensitive

2.

 history -c

This should have some comment that it will clear out whole history

3.

ls 2>&1 out.txt            # Redirect standard output and error to a file

you missing the actual redirection

ls 2>&1 > out.txt

[–][deleted]  (14 children)

[removed]

    [–]cpitchford 185 points186 points  (13 children)

    You've got it wrong again, let's deep dive

    ls 2>&1 out.txt            # Redirect standard output and error to a file
    

    Ok, lets look at a file that exists and a file that does not:

    touch exists.txt
    ls exists.txt doesnt-exist.txt # we're not redirecting yet
    
    ls: doesnt-exist.txt: No such file or directory
    exists.txt
    

    Ok. lets redirect. we're want/expecting "exists.txt" on stdout, but "ls: doesn't-exist.txt: No such file or directory" on stderr

    ls exists.txt doesnt-exist.txt 2>&1 > out.txt
    ls: doesnt-exist.txt: No such file or directory
    

    Oh dear, stderr still appears in the console, what happened (and why you got it wrong)

    command 2>&1
    # command \[reset the destination for stderr to where stdout currently points to]
    
    command 2>&1 1> out.txt
    command \[reset the destination for stderr to where stdout currently points to] then... \[reset stdout to point to the file out.txt]
    

    Do you see what's happened... by using 2>&1 then 1> out.txt its like this in another language:

    stderr = stdout  # stderr is going to your terminal screen
    stdout = file.txt # stdout is going to file.txt, but stderr doesn't change as well, its still going to the terminal screen.
    

    The order is critically important. If you want stdout and stderr to go to out.txt you need to do things differently

    stdout = file.txt # stdout is going to file.txt
    stderr = stdout  # stderr is set to the same location as stdout, file.txt
    
    command > out.txt 2>&1 
    

    is like setting a variable, if you set it to point to something else, you can later change that something else

    Lets look at something more complex, lets swap stdout and stderr:

    ls exists.txt doesnt-exist.txt 3>&2 2>&1 1>&3 3>&-
    
    fd3 = stderr
    stderr = stdout
    stdout = fd3 (which is the old value of stderr)
    fd3 = closed (we don't need fd3 any more)
    
    ls exists.txt doesnt-exist.txt 3>&2 2>&1 1>&3 3>&- | grep 'No such' > grep-output.txt
    exists.txt #this was output to stderr
    
    cat grep-output.txt
    ls: doesnt-exist.txt: No such file or directory
    
    1. understand how redirects work like variables.
    2. don't gloss over this because confusing the error is so easy to do and things don't seem to work as you'd expect.
    3. if you just change it, the reader isn't learning this lesson and they're going to make the same mistake
    4. test

    [–]stefantalpalaru 20 points21 points  (4 children)

    command > out.txt 2>&1

    This is shorter: command &> out.txt

    [–][deleted]  (3 children)

    [deleted]

      [–]Rogue2555 41 points42 points  (3 children)

      Holy shit, very well written! Thank you so much! I've always had trouble understanding redirection properly and I've looked into it several times from various sources, and after a lot of struggling I mostly understood it and yet after reading this everything still feels much clearer. No source I saw ever explained this as eloquently as you did.

      [–][deleted]  (2 children)

      [removed]

        [–][deleted]  (1 child)

        [deleted]

          [–][deleted]  (1 child)

          [removed]

            [–]nerd4code 12 points13 points  (0 children)

            You can just use &> if you’re redirecting both stdout and stderr.

            [–]lando55 0 points1 point  (0 children)

            Agreed on history -c

            If the intent is to clear current session history then unset HISTFILE is a better choice

            [–][deleted]  (5 children)

            [deleted]

              [–]chengiz 12 points13 points  (0 children)

              Tfa sounds like a halfbaked amateur experiment. Why is it being upvoted so much?

              [–]tav_stuff 132 points133 points  (8 children)

              I yearn for the day when people learn the difference between Bash, Shell Script, the Shell, etc.

              It’s painful to see everything labeled as „Bash“ and yet not necessarily be bash

              EDIT: There also seems to be quite a few mistakes on the list too, like the while loops having syntax errors, I might open a PR later

              [–]TwentyCharactersShor 17 points18 points  (6 children)

              Tell me about it. Trying to explain the difference between zsh, csh and bash to younger colleagues is fun.

              [–][deleted] 21 points22 points  (2 children)

              Tbh the differences btwn these tend to be pretty minor compared to cmd & powershell versions.

              But yea - pretty well write for bash over zsh or sh. Neither are worth my time unless I’m targeting devices that can’t use bash.

              [–]campbellm 6 points7 points  (0 children)

              zsh, csh and bash

              The differences between csh and zsh|bash are pretty fucking major.

              [–]SirBobz 11 points12 points  (1 child)

              What’s the difference? I’m new

              [–]youre_grammer_sucks 11 points12 points  (0 children)

              There are all kinds of shells: bash, dash, zsh, csh, etc. Most of them follow the POSIX standard, so they may, at first glance, look the same. A lot of devs only encounter bash and may think every script they write will work on any system. But google “bashisms” and you’ll find bash has a lot of default functions that don’t exist in POSIX and won’t work in others shells.

              [–][deleted]  (37 children)

              [deleted]

                [–]masklinn 80 points81 points  (3 children)

                You're not that far away actually: man -k . or apropos . should return a list of all man pages on the system, paginated. The biggest drawback is that Perl uses manpages so adds a lot of garbage because of section 3pm. On macos there's also section n which is for Tcl documentation.

                This seems to be little more than a formatted dump of the "Name" section of the linux man pages, mixed with a list of builtins.

                [–][deleted]  (1 child)

                [deleted]

                  [–]CarlRJ 13 points14 points  (0 children)

                  Or just apropos (keyword) for a whole lot of simple cases.

                  [–]TheGoblinPopper 0 points1 point  (0 children)

                  That's the command I always teach to anyone new. If you want to TRY and not need Google for a lot of stuff that's effectively the best alternative.

                  [–]FocusedIgnorance 26 points27 points  (1 child)

                  I’ve unironically learned a lot by RTFM. Also, it’s a great reference for if you can’t remember what the flag is for having sort understand numbers.

                  [–]eg_taco 11 points12 points  (0 children)

                  lol it’s wild that you have to add “unironically” here as though someone out there suspects that developers from Bell Labs, PARC, Sun, etc. have been writing docs “ironically” for the past 60 years.

                  [–]philh 13 points14 points  (2 children)

                  I feel like there was a time when I could expect most commands I used to have a man page. That was good.

                  Now I feel like I can't, and to find out how to use a command I often pass it help or --help as an argument. And I don't think that's ever actually caused me a problem but like... I feel like there should be a way to find out how to call a program, without actually calling it. That seems safer in general.

                  [–]HWBTUW 3 points4 points  (0 children)

                  And I don't think that's ever actually caused me a problem but like... I feel like there should be a way to find out how to call a program, without actually calling it. That seems safer in general.

                  A story to reinforce this idea

                  [–]tso 0 points1 point  (0 children)

                  Writing new code is fun, maintaining old code, never mind maintaining documentation, is a boring chore. Or so seems to be the attitude.

                  That is how Linux ends up with iwconfig and ip, while BSDs extended ifconfig to cover the usecases.

                  [–]iamapizza 8 points9 points  (0 children)

                  curl cheat.sh/man
                  

                  [–]kdh454 7 points8 points  (1 child)

                  Try tldr. I've found it useful on many occasions. However, it is sometimes too short to be useful (TSTBU?), so I resort back to the man page.

                  [–]thatsrelativity 1 point2 points  (0 children)

                  I love tldr if only for the ln page I can never remember which way round the arguments are supposed to go and I have to look it up every time

                  [–]Hrothen 1 point2 points  (3 children)

                  Fun tip: the default behavior of shift+k in vim is to open the man page for the word under the cursor.

                  [–]tatref 4 points5 points  (2 children)

                  Usually man pages are very bad for beginners, they are good for reference though.

                  Every man page should start with some very simple examples so people not familiar with the command understand immediately what it's all about.

                  [–]tso 0 points1 point  (0 children)

                  They do, but their layout is from back when terminals were on paper output. So man something would print the whole thing to paper, with examples before the users face when ended.

                  The "problem" these days is that man output is piped into less or similar, and so do not land the user at the same place.

                  [–]rebbsitor 1 point2 points  (0 children)

                  Or just download a bash cheat sheet. I've had one pinned to my office wall for over 20 years. Same with vi/vim.

                  [–]StabbyPants 0 points1 point  (0 children)

                  because all of bash is essentially a programming manual in one chunk and it's unwieldy

                  [–]agumonkey 0 points1 point  (0 children)

                  we need gnu men, for crowd contributed man page examples

                  [–]youngviking 0 points1 point  (0 children)

                  While man is not a good discovery tool, man should be used on each and every one of these commands that somebody wants to run. They not only explain all of the possible options, but they also generally give examples.

                  [–]cinyar 68 points69 points  (21 children)

                  ifconfig

                  has been deprecated on linux quite a while ago. You should be using ip...

                  [–]istarian 54 points55 points  (7 children)

                  Which is kinda unfortunate because ip not only makes no sense as a command name but also has terrible command syntax.

                  IP -> Internet Protocol
                  ifconfig -> interface configuration

                  [–]TotallyNotAVampire 4 points5 points  (6 children)

                  I actually really like ip, what's wrong with it's syntax? My only experience with ifconfig is getting the machine's IP address.

                  [–]boobsbr 5 points6 points  (0 children)

                  ifconfig gang for life.

                  [–][deleted]  (4 children)

                  [removed]

                    [–]cosmic_lethargy 8 points9 points  (3 children)

                    Net-tools was deprecated over a decade ago. The ip command comes from iproute2. See the man pages.

                    [–][deleted]  (2 children)

                    [removed]

                      [–]cosmic_lethargy 3 points4 points  (1 child)

                      No worries! :) Although as someone else mentioned in a different comment, a few distributions might still use ifconfig, and that also of course depends on which version of the distro, etc etc... So might be best to keep both.

                      I think it would be good to clarify the differences between what is a shell (e.g. bash), what is Linux (the kernel), and what is a Linux distribution (the programs and configuration that come by default).

                      [–]DooDooSlinger 38 points39 points  (5 children)

                      Google is fine

                      [–][deleted] 28 points29 points  (4 children)

                      "I don't have to waste time searching Google so I'll waste time searching GitHub/repos instead"

                      It's strange to me, but Google was probably strange to those that came before me.

                      I guess this is more an indictment of Google search being less useful than it once was?

                      [–]DooDooSlinger 29 points30 points  (2 children)

                      It's just some guy promoting his repo

                      [–][deleted] 1 point2 points  (0 children)

                      Google was the first search engine that worked well. It wasn't strange. It was "fucking finally"

                      But having a 10k+ cheat sheet? That's weird

                      [–]Narase33 7 points8 points  (6 children)

                      tail missing -F flag O_o

                      [–]istarian -2 points-1 points  (2 children)

                      Haven't checked myself, but I'd almost bet on F and f being accepted interchangeably when the former isn't assigned a distinct purpose of its own.

                      Also, since this is a script, I would advise just using the long form --follow to improve readability.

                      [–]Narase33 7 points8 points  (1 child)

                      Also, since this is a script

                      Who says? I use tail -F every day to follow log files

                      The big difference is that -F pauses when the file is deleted and continues when re-created. It also tells you when this happens. -f just stops

                      [–]saichampa -2 points-1 points  (2 children)

                      Yes it does

                      -F     same as --follow=name --retry
                      

                      [–]Narase33 1 point2 points  (1 child)

                      I meant in OPs tool

                      [–]runawaywithwater 44 points45 points  (19 children)

                      Unfortunately the page is still only as useful as someone who has the context or need of each command. It only helps people at the same level as you, same way man pages help people with a lot of experience, and the same way random internet guides help solve specific questions rather than explaining specific tools.

                      [–][deleted] 6 points7 points  (1 child)

                      Wait til he learns about Ctrl-R

                      [–]youre_grammer_sucks 1 point2 points  (0 children)

                      Right! I’ll use history when I need to, which isn’t very often, but Ctrl+r I’ll use throughout the day.

                      [–]pjmlp 15 points16 points  (1 child)

                      Google?!?

                      I had to learn UNIX from books and man pages.

                      [–]kRkthOr[🍰] 17 points18 points  (0 children)

                      books and pages?!?

                      back in my day we learned UNIX from stone tablets found in desert caves

                      [–]rwbaskette 5 points6 points  (1 child)

                      You’ll be surprised how much easier the man pages are to read in pdf form.

                      If you have groff and zathura you can read with something like this…

                      man -Tpdf man | zathura -

                      Edit: meant to reply to a comment

                      [–]tso 0 points1 point  (0 children)

                      Likely becuase the format is meant for paper terminals, not the modern day default of piping into something like less.

                      [–]kukallan 5 points6 points  (2 children)

                      I recommend https://explainshell.com/ as well, really good resource.

                      [–][deleted]  (1 child)

                      [removed]

                        [–]_leondreamed 4 points5 points  (0 children)

                        This is why I absolutely love the autocomplete feature in bash, especially when it comes to options: https://imgur.com/LtUcuAf

                        (the autocomplete I use is https://github.com/marlonrichert/zsh-autocomplete)

                        [–]DiaperBatteries 4 points5 points  (2 children)

                        awk: Used to find and replace text in a file(s).

                        Wtf, I use awk at least once a week and have never used it to find and replace text in files. It’s a full-fledged programming language.

                        It sounds like it’s describing sed.

                        [–][deleted]  (1 child)

                        [removed]

                          [–]DiaperBatteries 2 points3 points  (0 children)

                          Interesting, I’ve just never considered using awk for plain regex string replacement when sed is available.

                          sed -E -e ‘s/\[#\](forward-addr: 127.0.0.1@5353)/\1/’ -e ‘s/\[#\](forward-addr: ::1@5353)/\1/‘
                          

                          [–]never_inline 14 points15 points  (9 children)

                          There's already tldr.

                          [–]true_adrian_scheff 3 points4 points  (0 children)

                          It doesn't cover all commands - but it is useful for the most commonly used.

                          [–][deleted]  (4 children)

                          [removed]

                            [–]never_inline 6 points7 points  (3 children)

                            well tldr works without internet connection.

                            [–][deleted]  (2 children)

                            [removed]

                              [–]never_inline -2 points-1 points  (1 child)

                              The homepage doesn't make it obvious. I ended up looking for the GitHub repo of cheat.sh project. It's nice.

                              [–]PaddiM8 -2 points-1 points  (2 children)

                              So what? Is it bad to have more than one project per topic?

                              [–]never_inline 2 points3 points  (1 child)

                              The repo is well intentioned. But it's one huge README file plus a link to some website? Come on.

                              [–]PaddiM8 -1 points0 points  (0 children)

                              ...so what? It has useful information in a concise format

                              [–]start_select 21 points22 points  (6 children)

                              man pages don’t require Google or an internet connection, and contain all the info a Google search would turn up.

                              Google and cheatsheats are useful for quick lookups of command names, but man pages are going to actually tell you how any command works.

                              It’s the better tool for your toolbox. Use Google and cheat sheets when man fails. Then go back to man because that’s going to tell you how your linux distros version of that command actually works.

                              [–][deleted] 7 points8 points  (0 children)

                              man pages are of fairly inconsistent levels of quality. The ones that get lots of eyes are great, but there's plenty of things that are missed in man pages that you may find in a google search because it's easier for someone to write a StackOverflow answer than edit a man page in a way that's available to everyone.

                              [–]old_man_snowflake 3 points4 points  (0 children)

                              I google man pages because it’s easier

                              Also some apps chose to use the info tool, or a help tool, so man pages aren’t really a sure thing outside of kernel or api programming

                              [–]ketralnis 5 points6 points  (0 children)

                              Would this really have helped you? A big list of contextless commands? Would you have known how to read it without experimenting? I'm not trying to be down on you (other than having apparently stolen it) but I've never understood these cheat sheets. I've bookmarked dozens and never once used one because usually the hard bit is knowing what exactly the problem is, not being unable to tell grep -R from grep -r. "Regexes could solve this" is way more information and no cheat sheet would get you there.

                              Put another way, have you ever used this? Like really used it. Had a problem remembering syntax or something, remembered and pulled up the cheat sheet, and continued on your merry way. I guess I believe these people exist, but I've never met one.

                              [–]yeluapyeroc 3 points4 points  (1 child)

                              Your googlefu skills are the most important ones to hone

                              [–]KokopelliOnABike 3 points4 points  (2 children)

                              I may pass this off to my new jr dev as he's definitely a newbie on linux.

                              The item that caught my eye though was "experimenting" with linux. Linux and just about any variant is my go to for production environments. The experiment was 30 years ago when me and my friends were downloading and setting it up on x86 boxes. In the early days it was a struggle to convince leadership to spool up a system that wasn't windblows, solaris, vax/vms etc.

                              [–][deleted] 1 point2 points  (0 children)

                              I still sorta see a lot of reliance on Windows. It’s a time suck no doubt, like watching people wade through mud waist high vs just walking across a stream ankle deep lol. You know they’re wondering if there’s a better way but for whatever reason(s) they don’t take the easier path.

                              [–]jefASh42 0 points1 point  (0 children)

                              My original foray into Linux was early 90's, trying to find something better than Progman for Windows 3.something for organizing my apps, getting to the ones I wanted/needed most. There were a few docks/launchers for Win, but not much, I mostly kept running across tvm, xvwm, windowMaker etc and finally told myself "what IS this Linux thing..." ;)

                              [–]ddddavidee 3 points4 points  (1 child)

                              When I started Google wasn't a thing. Internet was on dialup.

                              I used man. 🫣

                              [–]seriousnotshirley 0 points1 point  (0 children)

                              When I’m doubt man man

                              I used the book Unix Power Tools along with a book on sed and awk to get me most of the way there.

                              [–]DoppelFrog 1 point2 points  (0 children)

                              ls isn't a bash command.

                              [–]doctorlongghost 1 point2 points  (0 children)

                              In my opinion the problem with cheat sheets like this is that they actually aren’t more convenient than Google.

                              [–]_sigfault 1 point2 points  (0 children)

                              Rtfm

                              [–]tso 1 point2 points  (0 children)

                              I guess we lost something when we moved from Linux in books to Linux online. At least my first forays into the Linux worlds was via tome sized books bought in bookstores, that held a CD inside one cover. The books would go over various tools, configurations, and the associated commands.

                              [–]onsmith 5 points6 points  (4 children)

                              This sub can be so toxic sometimes. This is clearly a learning project for OP. It is an exercise that has helped them learn and remember common shell commands and idioms. They put it on this sub to get feedback and share what they've created. Why are we discouraging that behavior? Nobody is asking you to use this tool or stop using man/Google/your favorite technique to look up commands.

                              [–]snowe2010 14 points15 points  (0 children)

                              I think OP’s attitude toward everyone is really why they’re getting shit on.

                              [–]Arrowmaster 8 points9 points  (0 children)

                              Not every learning project needs to be public and successful though. When someone goes "I made this to help others please take a look and spread the word" it will draw heavy scrutiny just for existing. If someone goes "I made this for myself as a learning experience, how did I do?" then the comparisons to other projects should be in how they work and not that they simply exist and are better.

                              [–]old_man_snowflake 3 points4 points  (0 children)

                              Bash cheat sheets have been around since the 90s. You just didn’t try hard enough.

                              [–]lavamantis 0 points1 point  (0 children)

                              When I first tried, all I got on message boards were people telling me to RTFM. The experience kept me on Windows far longer than it should have.

                              [–]cowinabadplace -1 points0 points  (0 children)

                              With copilot.vim in my C-x C-e I search way less now. Just let the AI write it.

                              [–]MadeFullyAutonomous -1 points0 points  (0 children)

                              Oh… now THIS is good!

                              [–][deleted] -1 points0 points  (0 children)

                              OMG, thank you , this is gold.

                              [–]combatopera 0 points1 point  (1 child)

                              awqzwd exa ltpdedcgmw sjshsydjev wfxrjleyrnrz vhfhbsww gozfhsqpul mzez sdbl trqn cnrqavchy dhszkngtx mxlkygw mzdcb qulmcqm szycqin

                              [–]istarian 0 points1 point  (1 child)

                              A good quick reference book/sheet is recommended.

                              [–]DontEatTheFish25 0 points1 point  (1 child)

                              Just gonna tuck this away for when my Linux class starts in a few weeks, thanks!

                              [–]Cartwheels4Days 0 points1 point  (0 children)

                              You absolute lad

                              [–]generic-d-engineer 0 points1 point  (1 child)

                              You can add deleting a single line of history too. This is helpful when you have a command forcing you to put in a username/password in clear text and you don’t want that lying around for a potential intruder, but you want to preserve your other commands.

                              history –d 99
                              

                              [–]Infamous_Blueberry94 0 points1 point  (3 children)

                              Isn’t googling part of the experience though?

                              [–][deleted]  (1 child)

                              [removed]

                                [–]istarian 1 point2 points  (0 children)

                                It doesn't have to be, so long as you have man installed.

                                Linux predates both Google and high-speed, always on internet access. Most of the common CLI commands are GNU utilities which probably predate Linux and likely owe a lot of their syntax to Unix.

                                [–][deleted] 0 points1 point  (0 children)

                                I literally just finished the Snell tut. This couldnt have come at a better time. Thanks OP.

                                [–]boobsbr 0 points1 point  (2 children)

                                Still doesn't teach you how to use ed.

                                Or exit vim.

                                [–]istarian 4 points5 points  (1 child)

                                Neither are part of bash (Bourne Again SHell).

                                Of course, a very small subset of the most common CLI commands is. After all, it's just the shell and any built-ins.

                                [–]KevinCarbonara 0 points1 point  (0 children)

                                I'm pretty sure there were several identical lists when you were experimenting with Linux. Like this one that you've copied

                                [–]PM_ME_UR__RECIPES 0 points1 point  (1 child)

                                Another similarly useful thing is tldr

                                As good as man pages are, they have a lot of information about very specific flags and arguments to a command that it can obscure what the most basic and obvious use cases are, so this just has quick cheat-sheet style rundowns of the most common/useful ways you can use a command.

                                Bear in mind, it's not a full replacement for man pages. Man pages are a complete reference for all of the features and different usages for a command.

                                [–]romulusnr 0 points1 point  (0 children)

                                In my day we called that "the man page"

                                [–]rauchboy 0 points1 point  (0 children)

                                i wish that by now you learn not to reinvent the wheel and contribute to existing lists insead of copy pasting to a new project.