• 0 Posts
  • 68 Comments
Joined 2 years ago
cake
Cake day: February 17th, 2024

help-circle


  • It would not, as @Quibblekrust@thelemmy.club explained in their comment (which I neglected to include in my explanation), Bash uses a special variable called IFS when executing for loops like this. IFS stands for Input Field Separators, and is a list of one of each type of whitespace (tab, space, and newline), and uses these as separators automatically.

    So instead of taking that whole ls output as one string of text, the for loop automatically separates it into an iterable list of strings using the newline separator.



  • You’ve got a few things going on to be broken down here.

    And forgive me if anything I say here seems condescending, it’s not meant to be, I just like to be very explanatory with things like this and to assume the reader may not know anything about anything. (Not as an insult, but simply as a gap in knowledge).

    Also, while I’m proficient at Bash, I’m no expert.

    LIST=$(ls): Here you’ve stored the output of the ls command to the variable LIST, which gives you a list of items in the given directory, in this case, whichever directory the command is run from. It’s also a good idea to quote the variable assignment like this: "$(ls)".

    for i in $LIST;: This is the first part of the for loop statement, which is an iterator, meaning, it will loop or iterate over every item in the given variable/parameter/group of iterable items.

    The i here, as you said could be anything. You could say for file in $LIST; or for item in $LIST;. It doesn’t matter, because it’s just a variable name that you are using in the first part of the for statement.

    So what bash will do with this is loop over the list, and take each item in the list, and assign it to the variable i, which will allow you do act upon that single item by calling the variable i in some other commands.

    do echo "I found one!";: This is the next part of the for loop, which is the instruction set to be executed inside the for loop. Here is where you can act upon the items in your list that have been assigned to the variable i.

    In your case, you’re just printing a statement to stdout (standard out), that says, “I found one!”

    It’s like saying, for each item in this list, print “I found one!”

    So if there are 20 items in the list, it will print that statement 20 times.

    However, maybe you want to print the item itself as part of a statement. So instead of “I found one!”, you could do something like:

    do echo "I found $i!"

    Which then would print “I found some-filename-or-directory-here!” for each item in your list.

    done: Finally, the done statement tells bash that this is the end of the for loop. So any commands after the done statement will only run once the for loop has iterated over all items in the list and executed the commands inside the for loop for each item on the list.

    A couple of notes:

    The ; is used as a command separator or terminator. So bash knows to first run LIST=$(ls) before it attempts to run whatever the next command might be.

    In bash, it’s good practice to always quote your variables like so: for i in "$LIST";. This is to avoid errors for characters that might need escaping like whitespace, backslashes, and other special characters.

    With that in mind, if you’re running a command like echo "I found $i!", you don’t need to quote the variable again, because it’s already inside a quote set.

    Further, it’s not absolutely necessary, but it can also be a good idea to also enclose all of your variables in {}, so whenever you use a variable, you’d do something like: "${LIST}"

    This not only more clearly identifies variables in your bash scripts/commands, but is necessary when using bash’s parameter expansion, which is pretty great.


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













  • I don’t know the specifics but forgejo is a gitea fork. There was/is some controversy around gitea governance and movent towards prioritizing a closed source paid/private versions of gitea.

    Again, I don’t know details, just very broad strokes. I chose forgejo because it’s under active Foss development and I didnt want to deal with potentially going with gitea and then having to abandon it later for whatever reason might develop.