Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[TIP]create,list, or extract archives (eg:for filemanager)
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
truc
Advocate
Advocate


Joined: 25 Jul 2005
Posts: 3199

PostPosted: Tue Jun 13, 2006 2:41 pm    Post subject: [TIP]create,list, or extract archives (eg:for filemanager) Reply with quote

EDIT 06/16/2006: corrected typo in the script Gexecute, when testing for different value of $PROG_NAME(in the case 'loop'), we should test for Gexecute and not juste execute

Hello, I just wana share these scripts since I find them usefull. There are nothing complicated in there so that beginners can find their way:)

The idea first came because I use Rox as filemanager, and when I have an archives, I like to easily
list its content, extract it, or even create an archive from multiple files/directories. There are
probably tools more fancy out there, but eh.. that's not what I want, furthermore these scripts are
writen so that they can be use with CLI, then I just added a little wrapper (not sure if it's
the right word..) to launch them on a term.

This is what you need:
Code:
app-arch/bzip2
app-arch/gzip
app-arch/tar
app-arch/unrar
app-arch/unzip
app-arch/zip

There are some chances you already have them on your system

First, I think most of us, have a special directory where
you put your own scripts, and which is in their PATH.

If not let's create it: if you have the root password, as root:
creating your scripts directory:
mkdir /home/scripts

and if you don't, as user
same for those who don't have root acces:
mkdir
${HOME}/scripts

then add this directory to your path:
root acces, as a normal user :
echo PATH=\"\$PATH:/home/scripts\" >> ~/.bashrc

without root acces, as a normal user:
echo PATH=\"\$PATH:$HOME/scripts\" >> ~/.bashrc


now source it, to have the new $PATH in your env:
Code:
source ~/.bashrc



Now we can start:)

Those with root acces can chown, and/or chmod the scripts directory, to have write permission: (eg:
chown $USER:users /home/scripts, and/or chmod 755 /home/scripts )

Now go into that directory, we 're going to create, three scripts, one to extract archive, a
other to list the content, and the last one to create an archive:

just copy-and-paste this on your prompt
(EDIT: was thinking about that: note that you really only need to copy and paste this (with midle click) on the command line, otherwise you files won't be good, eg: for the following file, the first line will be "#!/bin/sh" and not "cat << EOF > ./xtract-archive" )
xtract-archive:
cat << EOF > ./xtract-archive
#!/bin/sh
# xtract-archive -- truc -- samlt

which "cowsay" &> /dev/null
if [ "\$?" -eq 0 ]
then
    COWSAY=\$(which cowsay)
else
    COWSAY=\$(which echo)
fi

tarvars="--no-same-owner  --extract --verbose --file"
gzipvars="--decompress --verbose"
bzip2vars="--decompress --verbose"

for file in "\$@"
do
   
    # If you use this script on the command line you should be able to use absolute path
    # or even no path at all (just the archive name if you are in the same directory
    # That's why we define here DIR_TO_EXTRACT
    DIR_TO_EXTRACT="\${file%/*}"
    if [ "\$DIR_TO_EXTRACT" == "\$file" ]
    then
        DIR_TO_EXTRACT="\$(pwd)"
    fi
   
    case "\$file" in
        *.tar.gz|*.tgz)
                tar --gzip \${tarvars} "\$file" -C "\$DIR_TO_EXTRACT" || echo "There was a problem with the file '\$file'\nHIT 'q' to continue" | less
                continue
                ;;
   
        *.tar.bz2|*.tbz2)   
                tar --bzip2 \${tarvars} "\$file" -C "\$DIR_TO_EXTRACT" || echo "There was a problem with the file '\$file'\nHIT 'q' to continue" | less
                continue
                ;;
   
        *.gz|*.Z|*.z)
                gzip \${gzipvars} "\$file"
                continue
                ;;

        *.bz2)
                bzip2  \${bzip2vars} "\$file"
                continue
                ;;

        *.tar)
                tar \${tarvars} "\$file" -C "\$DIR_TO_EXTRACT" || echo "There was a problem with the file '\$file'\nHIT 'q' to continue" | less
                continue
                ;;

        *.rar)
                unrar x "\$file" "\$DIR_TO_EXTRACT" || echo "There was a problem with the file '\$file'\nHIT 'q' to continue" | less
                continue
                ;;
       
        *.zip|*.pk3)
                unzip "\$file" -d "\$DIR_TO_EXTRACT" || echo "There was a problem with the file '\$file'\nHIT 'q' to continue" | less
                continue
                ;;
       
        *)
                \${COWSAY} "It's not a known format" | less
                ;;
    esac

done
EOF

(then hit ENTER)

Same thing with
list-archive:
cat << EOF > ./list-archive
#!/bin/sh
# list-archive, -- truc -- samlt -- <samuelethiec at hotmail dot com>
# list the content of an archive

which "cowsay" &> /dev/null
if [ "\$?" -eq 0 ]
then
    COWSAY=\$(which cowsay)
else
    COWSAY=\$(which echo)
fi

tarvars="--list --verbose --file"
gzipvars="--list --verbose"
bzip2vars="--verbose"


for file in "\$@"
do
   
    case "\$file" in
        *.tar.gz|*.tgz)
                tar --gzip \${tarvars} "\$file" | less
                continue
                ;;
   
        *.tar.bz2|*.tbz2)   
                tar --bzip2 \${tarvars} "\$file" | less
                continue
                ;;
   
        *.tar)
                tar \${tarvars} "\$file" | less
                continue
                ;;

        *.rar)
                unrar lb "\$file" | less
                continue
                ;;
       
        *.zip|*.pk3)
                unzip -l "\$file" | less
                continue
                ;;
       
        *)
                \${COWSAY} "It's not a known format" | less
                ;;
    esac

done
EOF

(hit ENTER again..)

These two files are not so commented, since there are nothing special in it, but you might be interested in part: Chopping strings like a pro if you don't know yet:)

now we need the scripts to create archive:
Again copy and paste the following
create-archive:
cat << EOF2 > ./create-archive
#!/bin/bash
# create-archive, -- truc -- samlt -- <samuelethiec at hotmail dot com>

PROG_NAME="\$0"

which "cowsay" &> /dev/null
if [ "\$?" -eq 0 ]
then
    COWSAY=\$(which cowsay)
else
    COWSAY=\$(which echo)
fi


# this script is mainly for the ROX filemanager use. ROX works with absolute path, this is
# annoying when creating archive, since we would have the full path in it :/.
# If first arg is --cd-arg then second arg should be a directory name otherwise it exits

if [ "\${1}" = "--cd-arg" ]
then
    # is \$2 a directory, or a symbolic link towards a directory?
    if  test -d "\${2}"
    then
        # just in case it is NOT an absolute path that is given
        if [ "\${2:0:1}" != "/" ]
            then
            echo "Bad syntaxe \: --cd-arg absolute_path_for_an_existing_directory (Exiting)" | less
            exit 1
        fi
       
        store_dir="\${2}"
        shift 2

    else
        echo "Bad syntaxe \: --cd-arg existing_directory_name (Exiting)" | less
        exit 1
    fi
fi



# We now check if store_dir is already filled up with something, otherwise it will be the first
# arg dir then we change to this directory
cd "\${store_dir:=\$(pwd)}"


# Since this script can call itself, the archive_name can be already known, in this case
# the script just doesn't ask the user again
if [ -z \$archive_name ]
then
    \${COWSAY} "Archivage"
    cat <<EOF
///////////////////////////////////////////////////////////////////////////
//                                                                       //
//    name.tar or simply name since name.tar is the default              //
//    name.tar.gz or name.tgz to compress the archive with gzip          //
//    name.tar.bz2 ou name.tbz2 to compress the archive with bzip2       //
//    name.zip to create a zip archive                                   //
//    In the case you enter etheir:                                      //
//    name.gz or name.bz2 {,G}create-archive will compress each          //
//    path/to/file into path/to/file.{gz,bz2} regardless of the          //
//    'name' you gave.                                                   //
//                                                                       //
///////////////////////////////////////////////////////////////////////////


The created archive will saved in \$store_dir.

Nom de l'archive:
EOF
    read archive_name
fi

tarvars="--no-same-owner  --create --verbose --preserve-permission --file"
bzip2vars="--compress --keep --verbose"
gzipvars="--verbose"


y=\${archive_name%.*}
y=\${y##*.}


# If the user select only one file, which is actually a symbolic link to
# a directory, then we assume he wants to archive the content of this link, not
# just the link
if [ "\$#" -eq 1 -a -h \$1 ]
then
    if test -d "\$1"
    then
        archive_name="\$archive_name" "\$PROG_NAME" --cd-arg "\$store_dir" "\${1}"/*
        exit 0
    fi
fi


case "\$archive_name" in
        *.tar.gz|*.tgz)
                tar --gzip \${tarvars} "\${store_dir}"/"\${archive_name}" "\$@" || echo "There was an error." | less
                ;;
   
        *.tar.bz2|*.tbz2)   
                tar --bzip2 \${tarvars} "\${store_dir}"/"\${archive_name}" "\$@" || echo "There was an error." | less
                ;;
   
        *.gz|*.Z|*.z)
                gzip \${gzipvars} "\$@" || echo "There was an error." | less
                ;;

        *.bz2)
                bzip2 \${bzip2vars} "\${archive_name}" "\$@" || echo "There was an error." | less
                ;;

        *.tar)
                tar \${tarvars} "\${store_dir}"/"\${archive_name}" "\$@" || echo "There was an error." | less
                ;;

        *.zip)
                zip "\${archive_name}" "\$@" || echo "There was an error." | less
                ;;
       
        *)
               tar \${tarvars} "\${store_dir}"/"\${archive_name}".tar  "\$@" || echo "There was an error." | less
               ;;

esac


# Keep in mind that if you were using this script from the command line to compress several files,each compressed
# files will be in the corresponding files directory. Fortunately, this is not possible if using this script
# with rox or something similar:)

\${COWSAY} "Archive saved in \${store_dir}" | less
EOF2

(...)
This one is commented where I though it could be needed, otherwise feel free to ask:)

make these scripts executable:
chmod 755 xtract-archive list-archive create-archive


What then?
Now you can use these scripts onthe command line. The good thing is that you can use them with multiple files, eg:

demo:
xtract-archive archive1.tar /home/blah/blou/archive2.tbz ../archive3.tar.gz # will work and extract
# each archive in its own directory

create-archive file1 ../file2 /path/to/file3 # will create ONE archive in the directory where file1 is

list-archive archive1.tar /home/blah/blou/archive2.tbz ../archive3.tar.gz # will list each archive, (one at a time;) )


So yeah, you sometimes need to use the archive tool itself (tar, unrar, unzip..), especially if you want to uncompress
an archive which is in a read-only directory, or when you want to extract only one file from the archive.. Fortunately
I don't need to do this that often:)


Now it's time to show how these scripts can be used with your file manager, eg: ROX

In this one you can put whatever you want, personnally I use urxvt: x11-terms/rxvt-unicode (actually urxvtd(daemon) and urxvtc(the client) ).

You should still be in you scripts directory, otherwise, back to it! ;)
creating your customterm:
cat <<EOF > customterm
#! /bin/bash
# remember this is default args, if same argument are given in cmdline
# (ie giving -geometry again) only the urxvtc will use the last ones

urxvt_args="-geometry 110x55+300+140"
urxvt_args="\${urxvt_args} -scrollstyle plain"
urxvt_args="\${urxvt_args} -sl 140"
urxvt_args="\${urxvt_args} -scrollColor black"
urxvt_args="\${urxvt_args} -fg Limegreen"
urxvt_args="\${urxvt_args} -bg darkblue"
urxvt_args="\${urxvt_args} -bd black"
urxvt_args="\${urxvt_args} -colorRV LimeGreen"

pixmap=0

for arg in "\$@"
    do
        if [ "\$arg" == "-pixmap" ]
            then pixmap=1
            break
        fi
done

if [ \$pixmap -eq 0 ]
then   
    urxvt_args="\${urxvt_args} -tr"
    urxvt_args="\${urxvt_args} -tint grey"
    urxvt_args="\${urxvt_args} -sh 60"
fi

urxvtc \${urxvt_args} "\$@"
EOF


Warning: You might want to modify to previous script!
don't forget to make it executable:
Code:
chmod 755 customterm


Now we gonna create one scripts that you will can use in the futur for your own scripts:)

Gexecute:
cat <<EOF > Gexecute
#!/bin/sh
# -- truc -- samlt --

PROG_NAME="\${0##*/G}"
TERM_TO_USE="customterm"

case "\${PROG_NAME}" in

    create-archive) # This script is supposed to be run by ROX, so each argument
            # should (and have to) be in the same directory, if they are
            # not, you can still use create-archive directly from the
            # command line. Since Rox doesn't use relative paths, and
            # when creating an archive you usually don't wanna have full
            # path (ie /home/truc/dir/dir_or_file_to_compress
           
            cd_dir="\${1%/*}"
           
            \${TERM_TO_USE} -e \${PROG_NAME} --cd-arg "\$cd_dir" "\${@/\$cd_dir\/}"
            exit 0
            ;;

    Gexecute)    echo "The program can't call itself. Exiting!"
            exit 1
            ;;

    *)         \${TERM_TO_USE} -e "\${PROG_NAME}" "\$@"
            exit 0
            ;;
esac
EOF


Again, make it executable
Code:
chmod 755 Gexecute


So with this scripts, we can launch our *-archive scripts in a term, let's do some links
making usefull links:
ln -s Gexecute Gcreate-archive
ln -s Gexecute Gxtract-archive
ln -s Gexecute Glist-archive


Now you can use Gcreate-archive, Glist-archive and Gxtract-archive as you use create-archive, and so on
The difference is that the script will be launched in a new terminal, this will you to call these scripts
from your filemanager, and see what's going on (and see the content of an archive..).

Configuring ROX (Non-ROX user can stop here)

Warning : Use the right path for the scripts dir!
SendTo:
cd ~/.config/rox.sourceforge.net/SendTo
ln -s ~/scripts/Glist-archive Glist-archive
ln -s ~/scripts/Gcreate-archive Gcreate-archive
ln -s ~/scripts/Gxtract-archive Gxtract-archive


For a more complete configuration you can also do that :
Code:
cd  ~/.config/rox.sourceforge.net/SendTo && for dir in .application_{x-{bzip-compressed-tar,compressed-tar,pk3,rar,tar},zip}
do
mkdir -p $dir
ln -s ~/scripts/Glist-archive "$dir"/Glist-archive
ln -s ~/scripts/Gxtract-archive "$dir"/Gxtract-archive
done

cd  ~/.config/rox.sourceforge.net/SendTo && mkdir -p .inode_directory && ln -s  ~/scripts/Gcreate-archive .inode_directory/Gcreate-archive


(that add Glist-archive and Gxtract-archive to the menu associated with rar, tar, tar.gz and so on, suffixes.

We can also make Glist-archive the default action for these MIME-type
settings Glist-archive as the default action for archive:
cd ~/.config/rox.sourceforge.net/MIME-types && for mmtype in application_{x-{bzip{,-compressed-tar},compressed-tar,gzip,rar,pk3},zip}
do
cat <<EOF > $mmtype
#! /bin/sh
exec Glist-archive "\$@"
EOF
chmod 755 $mmtype
done


(rememenber Glist-archive is in your PATH :) )

Eventually we've done it :)

I would be happy if you have any comment (except "that's useless, programme foo does that for you;)"




[OT]
The good news is that you can now create links like these two:
Code:
ln -s /home/scripts/Gexecute ~/.config/rox.sourceforge.net/SendTo/.text_plain/Gvim
ln -s /home/scripts/Gexecute ~/.config/rox.sourceforge.net/SendTo/.text_plain/Gnano


or even a link Gless --> Gexecute :)

Now when you right click on a plain text file you can open it with vim or nano in a terminal, and this without any configuration, just a link:) [/OT]

EDIT: feel free to correct my english, I did my best.. but..:)


Last edited by truc on Tue Sep 29, 2009 12:50 pm; edited 7 times in total
Back to top
View user's profile Send private message
ppurka
Advocate
Advocate


Joined: 26 Dec 2004
Posts: 3181

PostPosted: Tue Jun 13, 2006 11:19 pm    Post subject: Reply with quote

Nice set of scripts.
A TIP I wanted to give:
Instead of using
Code:
 case "\${file##*.}" in
        tgz)
        tbz2)
...
, etc, you could use
Code:
 case "$file" in
*.tar.gz | *.tgz)
*.tar.bz2 | *.tbz2)
...
, etc, which would allow you to handle similar files with different suffixes. You could then dispel with the conditionals under each bz2 and gz cases.
Back to top
View user's profile Send private message
truc
Advocate
Advocate


Joined: 25 Jul 2005
Posts: 3199

PostPosted: Wed Jun 14, 2006 7:25 am    Post subject: Reply with quote

EDIT: I had misunderstood your post, actually that seems like a good idea! I'll make the change as soon as I can :)

EDIT2: thanks to ppurka, the code is now cleaner:)
Back to top
View user's profile Send private message
ppurka
Advocate
Advocate


Joined: 26 Dec 2004
Posts: 3181

PostPosted: Wed Jun 14, 2006 5:13 pm    Post subject: Reply with quote

I am glad that helped :D
Back to top
View user's profile Send private message
Zentoo
Apprentice
Apprentice


Joined: 18 Nov 2002
Posts: 170
Location: /dev/console

PostPosted: Sun Jul 02, 2006 2:36 am    Post subject: Great features for rox Reply with quote

Thanx for this TIP tutorial !

working myself with a personnal scripted rox, i found your way to execute things in terminal really smart !
I haven't the time right now to test it, but yet i could do it i should post about it. I'm yet thinking to make other scripts to add to this concept to convert documents, mail them (through evolution that i use) or do similar things...
_________________
Kernel 3.13.7| Gcc 4.8.2 | Glibc 2.19
Core i7 990x @ 4.0GHz | X58 | 12Gb
ACCEPT_KEYWORDS="~amd64"
CFLAGS="-march=native -O2 -pipe"
Back to top
View user's profile Send private message
truc
Advocate
Advocate


Joined: 25 Jul 2005
Posts: 3199

PostPosted: Sun Jul 02, 2006 9:35 am    Post subject: Reply with quote

Thanks:) I also have other scripts which use Gexecute with a link, I already talk about Gvim or Gnano and so on, one of them is to send one or several file(s) via scp on my lan and I particularly like this one;)


Tuttle wrote:
i found your way to execute things in terminal really smart !


Then, you might be interested in an other script xlaunch that I posted here. It uses the same trick, and I'm using it everyday:)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum