You are not logged in.

#1 2014-08-12 13:54:00

MrOats
Member
Registered: 2014-08-02
Posts: 12

[Bash] Archiving via p7zip in a specified Directory, Recursively

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

#2 2014-08-12 14:28:57

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [Bash] Archiving via p7zip in a specified Directory, Recursively

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

#3 2014-08-12 14:40:55

MrOats
Member
Registered: 2014-08-02
Posts: 12

Re: [Bash] Archiving via p7zip in a specified Directory, Recursively

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

#4 2014-08-12 14:55:24

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,346

Re: [Bash] Archiving via p7zip in a specified Directory, Recursively

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

#5 2014-08-12 15:52:36

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [Bash] Archiving via p7zip in a specified Directory, Recursively

MrOats wrote:

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.

-----

ewaller wrote:

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

#6 2014-08-12 17:00:31

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: [Bash] Archiving via p7zip in a specified Directory, Recursively

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.


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#7 2014-08-13 04:20:48

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: [Bash] Archiving via p7zip in a specified Directory, Recursively

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

#8 2014-08-13 04:24:13

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,231
Website

Re: [Bash] Archiving via p7zip in a specified Directory, Recursively

rockin turtle wrote:

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.

Offline

#9 2014-08-13 06:00:03

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: [Bash] Archiving via p7zip in a specified Directory, Recursively

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

Board footer

Powered by FluxBB