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’.

  • InFerNo@lemmy.ml
    link
    fedilink
    arrow-up
    11
    ·
    edit-2
    13 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

      • wheezy@lemmy.ml
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        13 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.

    • Will@lemmy.ml
      link
      fedilink
      English
      arrow-up
      2
      ·
      edit-2
      13 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
          ·
          13 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

    • bigredgiraffe@lemmy.world
      link
      fedilink
      arrow-up
      3
      ·
      13 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!

    • hades@feddit.uk
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      14 days ago

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

      ls /var/logs/apache

      ^logs^log

    • wheezy@lemmy.ml
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      13 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.

    • mel ♀@jlai.lu
      link
      fedilink
      arrow-up
      0
      ·
      14 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 :)

  • harsh3466@lemmy.ml
    link
    fedilink
    arrow-up
    6
    ·
    13 days ago
    find /path/to/starting/dir -type f -regextype egrep -regex 'some[[:space:]]*regex[[:space:]]*(goes|here)' -exec mv {} /path/to/new/directory/ \;
    

    I routinely have to find a bunch of files that match a particular pattern and then do something with those files, and as a result, find with -exec is one of my top commands.

    If you’re someone who doesn’t know wtf that above command does, here’s a breakdown piece by piece:

    • find - cli tool to find files based on lots of different parameters
    • /path/to/starting/dir - the directory at which find will start looking for files recursively moving down the file tree
    • -type f - specifies I only want find to find files.
    • -regextype egrep - In this example I’m using regex to pattern match filenames, and this tells find what flavor of regex to use
    • -regex 'regex.here' - The regex to be used to pattern match against the filenames
    • -exec - exec is a way to redirect output in bash and use that output as a parameter in the subsequent command.
    • mv {} /path/to/new/directory/ - mv is just an example, you can use almost any command here. The important bit is {}, which is the placeholder for the parameter coming from find, in this case, a full file path. So this would read when expanded, mv /full/path/of/file/that/matches/the/regex.file /path/to/new/directory/
    • \; - This terminates the command. The semi-colon is the actual termination, but it must be escaped so that the current shell doesn’t see it and try to use it as a command separator.
  • Lettuce eat lettuce@lemmy.ml
    link
    fedilink
    arrow-up
    6
    ·
    13 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
      1
      ·
      13 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

  • some_guy@lemmy.sdf.org
    link
    fedilink
    English
    arrow-up
    4
    ·
    13 days ago

    Search for github repos of dotfiles and read through people’s shell profiles, aliases, and functions. You’ll learn a lot.

  • ☂️-@lemmy.ml
    link
    fedilink
    arrow-up
    4
    ·
    edit-2
    13 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
      3
      ·
      13 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

  • Ftumch@lemmy.dbzer0.com
    link
    fedilink
    arrow-up
    4
    ·
    edit-2
    14 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.

  • Jhp9232nasd801@lemmy.ml
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    13 days ago

    List open files sudo lsof -i -P

    Network traffic by hardware sudo tcpdump -i en1 -nn -s0

    Current processes top -l 1

  • HiddenLayer555@lemmy.ml
    link
    fedilink
    English
    arrow-up
    3
    ·
    edit-2
    13 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'.

  • demonsword@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    14 days ago

    Something that really improved my life was learn to properly use find, grep, xargs and sed. Besides that, there are these two little ‘hacks’ that are really handy at times…

    1- find out which process is using some local port (i.e. the modern netstat replacement):

    $ ss -ltnp 'sport = :<port-number>'

    2- find out which process is consuming your bandwidth:

    $ sudo nethogs

    • eli@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      13 days ago

      You can do “ss -aepni” and that will dump literally everything ss can get its hands on.

      Also, ss can’t find everything, it does have some limitations. I believe ss can only see what the kernel can see(host connections), but tcpdump can see the actual network flow on the network layer side. So incoming, outgoing, hex(?) data in transit, etc.

      I usually try to use ss first for everything since I don’t think it requires sudo access for the majority of its functionality, and if it can’t find something then I bring out sudo tcpdump.

  • jim3692@discuss.online
    link
    fedilink
    arrow-up
    2
    ·
    13 days ago

    docker run --rm -it --privileged --pid=host debian:12 nsenter -a -t1 "$(which bash)"

    If your user is in the docker group, and you are not running rootless Docker, this command opens a bash shell as root.

    How it works:

    • docker run --rm -it creates a temporary container and attaches it to the running terminal
    • --privileged disables some of the container’s protections
    • --pid=host attaches the container to the host’s PID namespace, allowing it to access all running processes
    • debian:12 uses the Debian 12 image
    • nsenter -a -t1 enters all the namespaces of the process with PID 1, which is the host’s init since we use --pid=host
    • "$(which bash)" finds the path of the host’s bash and runs it inside the namespaces (plain bash may not work on NixOS hosts)
  • kittenroar@beehaw.org
    link
    fedilink
    English
    arrow-up
    2
    ·
    14 days ago

    systemd-run lets you run a command under some limitations, ie

    systemd-run --scope -p MemoryLimit=1000M -p CPUQuota=20% ./heavyduty.sh
    
    • kittenroar@beehaw.org
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      14 days ago

      ulimit can also be used to define limits, but for a user rather than a process. This could protect you against, ie, a fork bomb

  • Jo Miran@lemmy.ml
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    14 days ago

    It isn’t a command but an application. I cannot do my work without it.

    screen
    
          • wheezy@lemmy.ml
            link
            fedilink
            arrow-up
            1
            ·
            13 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.

        • MCHEVA@lemmy.world
          link
          fedilink
          arrow-up
          4
          ·
          13 days ago

          Tmux is good because I can have a little window with a bonsai in it and another little window for the matrix. Sometimes I even leave a window for typing in commands.

  • Ludrol@szmer.info
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    13 days ago

    pkill journalctl -b nvtop tail are great but I like: `LANGUAGE=en_GB LC_ALL=en_GB.UTF-8 LANG=en_GB.UTF-8 <your GUI program> to run a GUI program in English for more universal compatibility for helping newbies and creating/reading non-terminal based documentation