I’ve been homelabbing for a few years now, and recently I’ve really been focusing in on learning how to use gnu/linux. I thought it might be fun to periodically share the things I’ve been learning. The stand outs for me this past week were:

  1. Use the full path when referencing files and directories in bash scripts (Edit: when it makes sense, which is something I’m also still learning. This mkaes sense when the files will always be located in the same place.)
  2. In a bash script, the variable ${file##*/} will get you the name of the file your script is handling (example, when looping over files in a directory. I believe that’s a shell/bash standard variable, but I need to learn where it came from and how it works)
  3. Ubuntu gets a ton of justifiable criticism, but I find Canonical’s Multipass to be a great tool for spinning up Linux virtual machines. Especially on Apple silicon macs.
  4. Piping the output of ls to grep as a variable in a path is a great way to change to a directory you know exists but can’t remember the exact name of. (Example: cd ~/movies/“$(ls ~/movies | grep movie-name)”)
  5. The reason Mac cli utilities have syntax variations compared to the standard gnu/linux utilities is because macOS and its cli utilities are BSD based. This was information I knew at a high level, but had never really understood the implications of until this week.
  6. Related to point 5, if you’re on macOS trying to learn and you’re annoyed by the syntax differences between bsd and gnu utilities, you can run this script from darksonic37 on github to remove the bsd utilities from macOS and replace them with their gnu counterparts. (I have not run or reviewed the script. I found mulitpass first, and so far I’m happy using the Ubuntu virtual machine)
  • Laser@feddit.de
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    10 months ago

    Regarding some of your points:

    1. Depends on the case. A script operating on stuff in its subdirectories will probably be better with a relative path, especially if they get moved somewhere. Same logic goes for symbolic links.
    2. This is called string manipulation. ## deletes the longest match from the beginning, so it deletes everything to the last slash, as the asterisk expands as far as possible. If you wanted the directory the file is in using this method, you’d use ${file%/*}. This deletes the shortest match from the end. You also have the dedicated commands basename and dirname for this.
    3. Can’t comment
    4. I guess this works as long as you get exactly one match.
    • harsh3466@lemmy.worldOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      10 months ago
      1. Agree. Always was too strong a statement. I’ll edit my post to reflect that.
      2. Thank you! I’m going to read up on string manipulation
      3. True. This has been working for me because I know enough of the directories I’m looking for to insure a single match, but not the necessarily the exact, specific directory name.
      • vort3@lemmy.ml
        link
        fedilink
        arrow-up
        2
        ·
        10 months ago

        Instead of cd into output of ls | grep I’d suggest you to install fzf and fuzzy find directories to cd into.

      • unlawfulbooger
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        10 months ago

        To expand a little on @Laser ‘s point 2:

        In bash (and other programming languages) # is used at the start of the line to notate comments.

        When writing percentages, you write the symbol after the number, e.g. 50%

        That’s how I keep them apart, lol

        Theres a section in the bash manual with these and a whole bunch of more expansion tricks.

        One I find useful is

        echo "${myvar@A}"