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
    25
    ·
    edit-2
    8 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

        • pssk@lemmy.ml
          link
          fedilink
          arrow-up
          2
          ·
          7 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
        ·
        edit-2
        8 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
      5
      ·
      edit-2
      7 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)

  • utopiah@lemmy.ml
    link
    fedilink
    arrow-up
    1
    ·
    8 days ago
    fabien@debian2080ti:~$ history  | sed 's/ ..... //' | sort | uniq -c | sort -n | tail
    # with parameters
         13 cd Prototypes/
         14 adb disconnect; cd ~/Downloads/Shows/ ; adb connect videoprojector ;
         14 cd ..
         21 s # alias s='ssh shell -t "screen -raAD"'
         36 node .
         36 ./todo 
         42 vi index.js 
         42 vi todo # which I use as metadata or starting script in ~/Prototypes
         44 ls
        105 lr # alias lr="ls -lrth"
    fabien@debian2080ti:~$ history  | sed 's/ ..... //' | sed 's/ .*//' | sort | uniq -c | sort -n | tail
    # without parameters
         35 rm
         36 node
         36 ./todo
         39 git
         39 mv
         70 ls
         71 adb
         96 cd
        110 lr
        118 vi
    
  • Lettuce eat lettuce@lemmy.ml
    link
    fedilink
    arrow-up
    10
    ·
    8 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
      ·
      7 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
        ·
        7 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!

  • Ŝan • 𐑖ƨɤ@piefed.zip
    link
    fedilink
    English
    arrow-up
    10
    arrow-down
    3
    ·
    8 days ago

    ripgrep has mostly replaced grep for me, and I am extremely conservative about replacing core POSIX utilities - muscle memory is critical. I also tend to use fd, mainly because of its forking -x, but its advantages over find are less stark þan rg’s improvements over grep.

    nnn is really handy; I use it for everything but the most trivial renames, copies, and moves - anyþing involving more þan one file. It’s especially handy when moving files between servers because of þe built-in remote mounting.

    • marighost@piefed.socialOP
      link
      fedilink
      English
      arrow-up
      2
      ·
      8 days ago

      Would you recommend nnn for transfering ~5Tb of media between two local servers? Seems like a weird question but it’s something I’ll have to do soon.

      • Ŝan • 𐑖ƨɤ@piefed.zip
        link
        fedilink
        English
        arrow-up
        2
        arrow-down
        1
        ·
        8 days ago

        No. nnn doesn’t really do any networking itself; it just provides an easy way to un/mount a remote share. nnn is just a TUI file manager.

        For transfering 5TB of media, I’d acquire a 5TB USB 3.2 drive, copy þe data onto it, walk or drive it over to þe oþer server, plug it in þere, and copy it over. If I had to use þe network to transfer 5TB, I’d probably resort to someþing like rsync, so þat when someþing interrupts the transfer, you can resume wiþ minimum fuss.

        • marighost@piefed.socialOP
          link
          fedilink
          English
          arrow-up
          1
          ·
          8 days ago

          I could very easily, I’ve just only use rsync a handful of times for one-off files or small directories. Thinking of using it for several Tbs scares me 😅

          • ScientifficDoggo@lemmy.zip
            link
            fedilink
            English
            arrow-up
            1
            ·
            8 days ago

            I’m sure it’ll be fine, I’m no expert but i use it it sync/clone my music storage with my music player. There’s thousands of songs, lyrics, and album art getting synced and backed up regularly in my case.

            Worst thing that happened to me happened when I was new to the tool and accidentally overwrote my source directory (luckily I had backups)

          • ranzispa@mander.xyz
            link
            fedilink
            arrow-up
            5
            ·
            8 days ago

            When transfering large amounts of data I’d most definitely advise using rsync. Something fails, connection falls and everything is okay as it’ll pick up where it left off.

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

      rg and fd have been so much easier to use than the classics to me. Great replacements!

      bat is another one that I think can be worth switching to, though not as essential.

  • harsh3466@lemmy.ml
    link
    fedilink
    arrow-up
    9
    ·
    8 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.
  • Auster@thebrainbin.org
    link
    fedilink
    arrow-up
    3
    ·
    8 days ago

    (Fixed the bolding issue)

    From a file I keep since I started using Linux near 5 years ago:

    Display the RAM usage:
    watch -n 5 free -m
    Useful if you open way too much stuff and/or you’re running on budget processing power, and don’t want your computer freezing from 3 hours.
    Also useful if you use KDE’s Konsole integrated into the Dolphin file manager and you must for some reason not close the Dolphin window. You’d just need to open Dolphin’s integrated Konsole (F4), run the command and without closing it, press F4 again to hide the Konsole.

    Terminal-based file browser that sorts by total size:
    ncdu
    why is the cache folder 50 GB big?

    Mass-check MD5 hashes for all files in the path, including subfolders:
    find -type f \( -not -name "md5sum.txt" \) -exec md5sum '{}' \; > md5sum.txt
    Change md5sum (and optionally the output file’s name) for your favorite/needed hash calculator command.

    For mounting ISOs and similar formats:
    sudo mount -o loop path/to/iso/file/YOUR_ISO_FILE.ISO /mnt/iso

    And unmounting the file:
    sudo umount /mnt/iso
    Beware there’s no N in the umount command

    For creating an ISO from a mounted disc:
    dd if=/dev/cdrom of=image_name.iso

    And for a folder and its files and subfolders:
    mkisofs -o /path/to/output/disc.iso /path/from/input/folder

    Compress and split files:
    7z -v100m a output_base_file.7z input_file_or_folder

    Changes the capslock key into shiftlock on Linux Mint (not tested in other distros):
    setxkbmap -option caps:shiftlock
    Was useful when the shift key from a previous computer broke and I didn’t have a spare keyboard.

    If you want to run Japanese programs on Wine, you can use:
    LC_ALL=ja_JP wine /path/to/the/executable.exe
    There are other options but this is one that worked the better for me so I kinda forgor to take note of them.

    List all files in a given path and its subfolders:
    find path_to_check -type f
    Tip: add > output.txt or >> output.txt if you’d rather have the list in a TXT file.

    Running a program in Wine in a virtual desktop:
    wine explorer /desktop=session_name,screen_size /path/to/the/executable.exe

    E.g.:
    wine explorer /desktop=MyDesktop,1920x1080 Game.exe

    Useful if you don’t want to use the whole screen, there are integration issues between Linux, Wine and the program, or the program itself has issues when alt-tabbing or similar (looking at you, 2000’s Windows games)

    Download package installers from with all their dependencies:
    apt download package_name
    Asks for sudo password even when not running as sudo. Downloaded files come with normal user permissions thankfully. Also comes with an installation script but if you want to run it offline, iirc you need to change apt install in the script for dpkg -i.

    If you use a program you’d rather not connect to the internet but without killing the whole system’s connection, try:
    firejail --net=none the_command_you_want_to_run

    Or if you want to run an appimage:
    firejail --net=none --appimage the_command_you_want_to_run

    If you want to make aliases (similar to commands from Windows’ PATH) and your system uses bash, edit the file $HOME/.bashrc (e.g. with Nano) and make the system use the updated file by either logging out and in, or running . ~/.bashrc

    Python/Pip have some nifty tools, like Cutlet (outputs Japanese text as Romaji), gogrepoc (for downloading stuff from your account using GOG’s API), itch-dl (same as gogrepoc but for Itch.io), etc. If you lack the coding skills and doesn’t mind using LLMs, you could even ask one to make some simpler Python scripts (key word though: simpler).

    If you want to run a video whose codec isn’t supported by your system (e.g. Raspberrian which only supports H.264, up to 1080p):
    ffmpeg -i input_video.mkv -map 0 -c:v libx264 -preset medium -crf 23 -vf scale=1920:1080 -c:a copy -c:s copy output_video.mkv

    • bigredgiraffe@lemmy.world
      link
      fedilink
      arrow-up
      8
      ·
      8 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
          ·
          6 days 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.

    • hades@feddit.uk
      link
      fedilink
      arrow-up
      24
      ·
      8 days ago

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

      ls /var/logs/apache logslog

        • ystael@beehaw.org
          link
          fedilink
          arrow-up
          1
          ·
          7 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
            arrow-down
            4
            ·
            edit-2
            6 days 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.

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

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

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

  • demonsword@lemmy.world
    link
    fedilink
    arrow-up
    5
    ·
    8 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

    • Ephera@lemmy.ml
      link
      fedilink
      English
      arrow-up
      3
      ·
      8 days ago

      I always just do ss -ltnp | grep <port-number>, which filters well enough for my purposes and is a bit easier to remember…

    • eli@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      8 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.

  • thagoat@piefed.thagoat.lol
    link
    fedilink
    English
    arrow-up
    4
    ·
    8 days ago

    Bash/ZSH aliases are invaluable for commands that you run often. I use micro as my terminal editor so I have alias m for micro and sm for sudo micro. Just 2 of maybe a dozen aliases I use. Docker has quite a large list of aliases with ZSH. Super super useful.

  • jim3692@discuss.online
    link
    fedilink
    arrow-up
    2
    ·
    7 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)