You are not logged in.
So I've been making the move from Windows to Linux, and there was a useful batch command I made for archiving any given file with "x" extenstion using 7zip. But now I'm in a Linux, it's going to be different in the beginning.
Let me explain what this command did. It was a for loop, that FOR every given file matching the extension ".bsp" and ".nav", it would run 7zip on that file, and then do the next file in queue after it was done. Is there any way to replicate this?
Windows Batch command:
for /R "C:\Users\Drake\Downloads\Maps" %A in (*.bsp *.nav) do "C:\Program Files\7-Zip\7z.exe" a -tbzip2 -mx9 "%A.bz2" "%A"
What I've tried doing so far:
for -R "/drake/Downloads/Maps/MapsB" $A in (*.bsp) do 7z a -tbzip2 -mx9 "$A.bz2" "$A"
I'm really lost. I'm sure I got the 7-Zip part right, it's just a matter of modifying the For loop. Which I have no clue how to specify.
Offline
My first inclination would be something like:
while read file; do
echo "$file" "${file}.bz2"
done < <( find . -name "*bsp" )
Though it may be more clear to express it using a pipe:
find . -name "*bsp" | while read file; do
echo "$file" "${file}.bz2"
done
But technically (particularly if you were putting it in a script) you should do something like:
while IFS= read -r -d '' file; do
echo "$file" "${file}.bz2"
done < <(find . -name "*bsp" -print0)
This method will work on files with all legal characters in the filename, including newlines.
Honestly, it's really a good idea to get in the habit of using the most 'robust' method from the get go. Speaking of which, if you're interested in learning Bash, start here!
Last edited by alphaniner (2014-08-12 14:35:29)
But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner
Offline
Oh, that blew me a way. Is it possible to define multiple extensions to look for?
Also inside the quotes of: "-d ' ' file;"
Is where I point the directory to work in?
Offline
You might also want to investigate the find command. Especially the -exec sub-command
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
Also inside the quotes of: "-d ' ' file;"
Is where I point the directory to work in?
The '.' in the find commands I posted represents the directory to search. I thought maybe I shouldn't have used it in the examples, sorry. So to put it together with search for multiple extensions:
find <directory_to_search> \( -name "*bsp" -o -name "*nav" \) ...
Edit: Also, if you're always creating .bz2 files you should probably just use the bzip2 command.
-----
You might also want to investigate the find command. Especially the -exec sub-command
Wow, I didn't know that -exec accepts multiple {} . I guess that's what you were thinking, anyway. It also handles at least one edge case - files with newline in the name - but I don't know if that means it handles all of them. If so it would definitely make for a cleaner solution in a case like this.
Last edited by alphaniner (2014-08-12 16:10:51)
But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner
Offline
Another tip for those just starting out with bash is to run your script through ShellCheck, it will flag up many common errors and bad practices.
Offline
Try this:
#!/usr/bin/bash
dir="/drake/Downloads/Maps/MapsB"
for A in "$dir"/*.{bsp,nav}; do
7z a -tbzip2 -mx9 "$A.bz2" "$A"
done
Offline
Try this:
#!/usr/bin/bash dir="/drake/Downloads/Maps/MapsB" for A in "$dir"/*.{bsp,nav}; do 7z a -tbzip2 -mx9 "$A.bz2" "$A" done
This is not recursive.
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline
Sorry, I didn't realize the windows script was recursive.
Try this:
#!/usr/bin/bash
shopt -s globstar
dir="/drake/Downloads/Maps/MapsB"
for A in "$dir"/**/*.{bsp,nav}; do
7z a -tbzip2 -mx9 "$A.bz2" "$A"
done
Offline