I’ve been setting up a new Proxmox server and messing around with VMs, and wanted to know what kind of useful commands I’m missing out on. Bonus points for a little explainer.

Journalctl | grep -C 10 'foo' was useful for me when I needed to troubleshoot some fstab mount fuckery on boot. It pipes Journalctl (boot logs) into grep to find ‘foo’, and prints 10 lines before and after each instance of ‘foo’.

  • HakunaHafada@lemmy.dbzer0.com
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    2 days ago

    redshift -O 5000: ‘redshift’ is a utility that adjusts the color temperature of your display, ‘-O’ allows you to set a specific temperature, and ‘5000’ is what I like.

    Edit: I also like xkill. xkill lets you click on a window or program and kills it. I need to do this frequently every time exit Kodi; the program stops, but the window is still there.

  • InFerNo@lemmy.ml
    link
    fedilink
    arrow-up
    27
    ·
    3 days ago

    I use $_ a lot, it allows you to use the last parameter of the previous command in your current command

    mkdir something && cd $_

    nano file
    chmod +x $_

    As a simple example.

    If you want to create nested folders, you can do it in one go by adding -p to mkdir

    mkdir -p bunch/of/nested/folders

    Good explanation here:
    https://koenwoortman.com/bash-mkdir-multiple-subdirectories/q

    Sometimes starting a service takes a while and you’re sitting there waiting for the terminal to be available again. Just add --no-block to systemctl and it will do it on the background without keeping the terminal occupied.

    systemctl start --no-block myservice

    • Will@lemmy.ml
      link
      fedilink
      English
      arrow-up
      5
      ·
      2 days ago

      For interactive editing, the keybind alt+. inserts the last argument from the previous command. Using this instead of $_ has the potential to make your shell history a little more explicit. (vim $_ isn’t as likely to work a few commands later, but vim actual_file.sh might)

        • pssk@lemmy.ml
          link
          fedilink
          arrow-up
          2
          ·
          2 days ago

          You can use M-. instead of $_ to insert last param of last command. You can also access older commands’ param by repeated M-. just like you would do for inserting past commands with up arrow or C-p

      • wheezy@lemmy.ml
        link
        fedilink
        arrow-up
        4
        ·
        3 days ago

        I have my .bashrc print useful commands with a short explanation. This way I see them regularly when I start a new session. Once I use a command enough that I have it as part of my toolkit I remove it from the print.

  • Ftumch@lemmy.dbzer0.com
    link
    fedilink
    arrow-up
    24
    ·
    3 days ago

    Ctrl-z to suspend the running program.

    bg to make it continue running in the background.

    jobs to get an overview of background programs.

    fg to bring a program to the foreground.

  • HiddenLayer555@lemmy.ml
    link
    fedilink
    English
    arrow-up
    9
    ·
    edit-2
    3 days ago

    parallel, easy multithreading right in the command line. This is what I wish was included in every programming language’s standard library, a dead simple parallelization function that takes a collection, an operation to be performed on the members of that collection, and optionally the max number of threads (should be the number of hardware threads available on the system by default), and just does it without needing to manually set up threads and handlers.

    inotifywait, for seeing what files are being accessed/modified.

    tail -F, for a live feed of a log file.

    script, for recording a terminal session complete with control and formatting characters and your inputs. You can then cat the generated file to get the exact output back in your terminal.

    screen, starts a terminal session that keeps running after you close the window/SSH and can be re-accessed with screen -x.

    Finally, a more complex command I often find myself repeatedly hitting the up arrow to get:

    find . -type f -name '*' -print0 | parallel --null 'echo {}'

    Recursively lists every file in the current directory and uses parallel to perform some operation on them. The {} in the parallel string will be replaced with the path to a given file. The '*' part can be replaced with a more specific filter for the file name, like '*.txt'.

    • ranzispa@mander.xyz
      link
      fedilink
      arrow-up
      4
      ·
      3 days ago

      should be the number of hardware threads available on the system by default

      No, not at all. That is a terrible default. I do work a lot on number churning and sometimes I have to test stuff on my own machine. Generally I tend to use a safe number such as 10, or if I need to do something very heavy I’ll go to 1 less than the actual number of cores on the machine. I’ve been burned too many times by starting a calculation and then my machine stalls as that code is eating all CPU and all you can do is switch it off.

  • emb@lemmy.world
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    3 days ago

    I get a lot of mileage out of the line editing commands. I think they are Emacs based and optional… but like Ctrl k, Ctrl u, Ctrl a.

    • __hetz@sh.itjust.works
      link
      fedilink
      arrow-up
      2
      ·
      3 days ago

      I usually set vi mode to have vi(m)-like line editing but I’ve always liked Ctrl+u over Esc d d. Thankfully it still works even with vi mode enabled. Seems to also be implemented as a shortcut for a lot of login managers and in DEs for settings menus, dialog boxes and such.

      • emb@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        3 days ago

        It’s funny, I love vim and have never understood Emacs, but when setting up a shell it seems the Emacs commands felt more natural.

  • PenguinCoder@beehaw.org
    link
    fedilink
    English
    arrow-up
    1
    ·
    2 days ago

    A couple I use (concept of not exact), that I haven’t seen in the thread yet:

    Using grep as diff: grep -Fxnvf orig.file copy.file

    Using xargs -

    xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input.

    EG: $ find ~/Pictures -name "*.png" -type f -print0 | xargs -0 tar -cvzf images.tar.gz

  • Oinks@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    4
    ·
    edit-2
    3 days ago

    I’m not much of a one-liner collector but I like this one:

    vim +copen -q <(grep -r -n <search> .) 
    

    which searches for some string and opens all instances in vim’s quickfix list (and opens the quickfix window too). Navigate the list with :cn and :cn. Complex-ish edits are the obvious use case, but I use this for browsing logs too.

    Neovim improves on this with nvim -q - and [q/]q, and plenty of fuzzy finder plugins can do a better version using ripgrep, but this basic one works on any system that has gnu grep and vim.

    Edit:

    This isn’t exactly a command, but I can’t imagine not knowing about this anymore:

    $ man grep
    /  -n       # double space before the dash!
    

    brings you directly to the documentation of the -n option. Not the useless synopsis or any other paragraphs that mention -n in passing, but the actual doc for this option (OK, very occasionally it fails due to word wrap, but assuming the option is documented then it works 99% of the time).

  • @marighost I dont use Prox, but for various random linux commands… ive got a wealth. :D in the journalctl vein.

    journalctl -xeu \<service name\>

    ex: journalctl -xeu httpd

    Gives you the specific journal output for the given service. In this example. httpd.

    Also, journalctl is more than boot logs, its all of your logs from anything controlled by systemd. Mounts, services, timers, even sockets.

    For example. On my system, i have /var/home as a mount. systemctl and journalctl can give me info on it with:

    systemctl status var-home.mount
    journalctl -xeu var-home.mount

    You can see all of the mounts with.
    systemctl list-units --type=mount

    Or, see all of your services with
    systemctl list-units --type=service

    Or all of your timers with
    systemctl list-timers

    We do a weekly show on getting into linux terminals, commands, tricks, and share our experience… It’s called Into the Terminal. on the Red Hat Enterprise Linux youtube channel. I’ll send you a link if you’re interested.

  • crispycone@lemmy.zip
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    3 days ago

    Deleted comment.

    I double posted as Mlem didn’t show my initial comment after posting for some reason.

    (Edit)

  • flatbield@beehaw.org
    link
    fedilink
    English
    arrow-up
    1
    ·
    3 days ago

    Various uses of “find” in particular. “xargs” sometimes too. The capabilities of “bash” in general including scripting and the whole redirection, piping, and multiprocessing capabilities in particular.

    • hades@feddit.uk
      link
      fedilink
      arrow-up
      23
      ·
      3 days ago

      Also if you make a typo you can quickly fix it with ^, e.g.

      ls /var/logs/apache

      ^logs^log

        • ystael@beehaw.org
          link
          fedilink
          arrow-up
          1
          ·
          2 days ago

          I usually spell this as !!:gs/foo/bar/ (in bash). Is there a functional difference?

          ! command history can also take line and word selectors. I type something like !-2:2 surprisingly often.

          • Ŝan • 𐑖ƨɤ@piefed.zip
            link
            fedilink
            English
            arrow-up
            1
            ·
            1 day ago

            I honestly have no idea! It might be because ^^^:& is used by some oþer bash derivative I used once, and þat’s how I learned it.

            Yeah, I use !-# a bunch too, just not wiþ global replacement. I’m most often just redo-ing some action wiþ a couple of file extensions.

    • bigredgiraffe@lemmy.world
      link
      fedilink
      arrow-up
      7
      ·
      3 days ago

      To add to this one, it also supports more than just the previous command (which is what !! means), you can do like sudo !453 to run command 453 from your history, also supports relative like !-5. You can also use without sudo if you want which is handy to do things like !ls for the last ls command etc. Okay one more, you can add :p to the end to print the command before running it just in case like !systemctl:p which can be handy!

        • bigredgiraffe@lemmy.world
          link
          fedilink
          arrow-up
          2
          ·
          1 day ago

          Hah I am glad it was helpful! Glad to share, I always felt like half the point of learning is to share what you learned. That is one of my favorite “hidden gems” for lack of a better term that can be a real time saver.

          Bonus just for more fun: you can use cd - to switch back to the directory you were last in after changing directories, it toggles the top two paths in the stack. It is similar to how pushd/popd work if you have you used those. I use that one a ton, there are fancier tools now but that one works everywhere.

          Oh also, anyone on a Mac needs to know about pbcopy, Linux has xclip and I don’t remember what the Wayland analog is.

    • mel ♀@jlai.lu
      link
      fedilink
      arrow-up
      3
      ·
      3 days ago

      with zsh, you can use it, and then press space to have the !! replaced by the previous command to be able to edit it :)

      • smeg@feddit.uk
        link
        fedilink
        English
        arrow-up
        2
        ·
        2 days ago

        You can do this on bash too if you add bind Space: magic-space to your bashrc/profile

    • wheezy@lemmy.ml
      link
      fedilink
      arrow-up
      2
      ·
      3 days ago

      I forget where I got it. But mine will do this if I double tap ESC after I sent the command without sudo. Very useful.

      I should probably figure out what it was I added to do this.

      Doesn’t issue the command. Have to hit enter. Useful to verify it’s the right command first.

      With the way bash history can work Id be worried about running sudo rm -rf ./* by mistake.

          • wheezy@lemmy.ml
            link
            fedilink
            arrow-up
            2
            ·
            3 days ago

            Scrolling is tmux is what I hate about it. I would prefer to use tmux since I’m use to it. But if someone can explain to me why I can’t just use my scroll wheel on my mouse.

            • kittenroar@beehaw.org
              link
              fedilink
              English
              arrow-up
              2
              ·
              2 days ago

              As I understand it, the issue is that tmux invents its own terminal emulator functionality that conflicts with the existing terminal it runs within, while screen simply defers scroll functionality to the terminal emulator.

    • Eager Eagle@lemmy.world
      link
      fedilink
      English
      arrow-up
      1
      ·
      3 days ago

      are you using a maintained alternative? Distros started to remove it from their repos years ago because it was not maintained anymore afaik

        • Eager Eagle@lemmy.world
          link
          fedilink
          English
          arrow-up
          2
          ·
          3 days ago

          maybe they resumed development then, it was removed from Ubuntu and RHEL repos about 5 years ago when I had to look for an alternative

  • ☂️-@lemmy.ml
    link
    fedilink
    arrow-up
    13
    ·
    3 days ago

    ctrl+r on bash will let you quickly search and execute previous commands by typing the first few characters usually.

    it’s much more of a game changer than it first meets the eye.

    • eli@lemmy.world
      link
      fedilink
      arrow-up
      4
      ·
      3 days ago

      And I believe shift+r will let you go forward in history if you’re spamming ctrl+r too fast and miss whatever you’re looking for

  • Lettuce eat lettuce@lemmy.ml
    link
    fedilink
    arrow-up
    10
    ·
    3 days ago

    The watch command is very useful, for those who don’t know, it starts an automated loop with a default of two seconds and executes whatever commands you place after it.

    It allows you to actively monitor systems without having to manually re-run your command.

    So for instance, if you wanted to see all storage block devices and monitor what a new storage device shows up as when you plug it in, you could do:

    watch lsblk
    

    And see in real time the drive mount. Technically not “real time” because the default refresh is 2 seconds, but you can specify shorter or longer intervals.

    Obviously my example is kind of silly, but you can combine this with other commands or even whole bash scripts to do some cool stuff.

    • Breadhax0r@lemmy.world
      link
      fedilink
      arrow-up
      3
      ·
      3 days ago

      Ooooh cool, I think this explains how they have our raid monitor set up at work! I keep forgetting to poke through the script

      • Lettuce eat lettuce@lemmy.ml
        link
        fedilink
        arrow-up
        2
        ·
        2 days ago

        Yeah, it’s a neat little tool. I used it recently at my work. We had a big list of endpoints that we needed to make sure were powered down each night for a week during a patching window.

        A sysadmin on my team wrote a script that pinged all of the endpoints in the list and returned only the ones that still were getting a response, that way we could see how many were still powered on after a certain time. But he was just manually running the script every few minutes in his terminal.

        I suggested using the watch command to execute the script, and then piping the output into the sort command so the endpoints were nicely alphabetical. Worked like a charm!