Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 🔁 Moving files from multiple numbered folders into one - using Bash loop and xargs
- Note: The `i` variable in a `for` loop and the `{}` placeholder used with `xargs -I{}` serve a similar purpose -
- both are used to iterate over values and substitute them within the command being executed.
- ✅ Cleaner and simpler version using Bash for-loop:
- for i in {10..29}; do
- mv "Folder-1-0$i"/* \
- "Folder-1-001/"
- done
- ####################################################################################
- 🔹 For loop breakdown:
- - `for i in {10..29}; do` : Loops through numbers from 10 to 29 (inclusive) using brace expansion.
- - `$i` : Holds the current number in each iteration.
- - `do ... done` : Body of the loop that runs your desired commands.
- This method is clean, fast, and highly readable for ranges with known numeric patterns.
- ####################################################################################
- ✅ Alternative using seq and xargs:
- seq 10 29 | xargs -I{} mv "Folder-1-0{}"/* \
- "Folder-1-001/"
- ####################################################################################
- 🔹 xargs breakdown:
- - `seq 10 29` : Generates a list of numbers from 10 to 29.
- - `xargs -I{}` : For each number, replaces `{}` in the command with the current number.
- - Each `mv` command moves the contents of one numbered folder into the target folder.
- Useful when working with piped input or dynamic file lists from other commands.
- ####################################################################################
- Bonus:
- Extract multiple zip files in PWD (using for loop):
- ####################################################################################
- for file in *.zip; do
- dir="${file%.zip}"
- unzip "$file" -d "$dir"
- done
- ####################################################################################
Advertisement
Add Comment
Please, Sign In to add comment