LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-24-2010, 12:46 PM   #1
josecolella
Member
 
Registered: Nov 2010
Location: Milan, Italy
Distribution: Linux Mint 13, Debian 6.0
Posts: 62

Rep: Reputation: 3
Question zipping files into a specific folder


Hello, I am trying to use the command gzip to compress a directory or file list
as argument and compress the file in a file named copia101225, within a directory named ZIPFILES. I want to make sure that if the arguments doesn't exist, the destination directory doesn't exist that it creates it.

I keep failing at compressing the file to copia101225, that is within the directory named ZIPFILES

This is what I have so far:

#! / bin / bash
# Title: Compress a file
# Author: Jose Miguel Colella
# Description: Compress a file

read-p "Enter the file name" ARCHIVO_INPUT

cat test | while read line;
do
echo "Line $ count: $ line"
count = $ [$ count + 1]
done
qzip $ARCHIVO_INPUT > copia101225
 
Old 12-24-2010, 07:17 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Here as an example a oneliner
Code:
for i in *.txt; do gzip $i; mv $i.gz copia; done
just tested on my system. This code zips all *.txt files in the current directory and moves them into the copia directory.

Markus
 
Old 12-25-2010, 12:44 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,253

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Would you explain what the while loop is meant to be doing?
 
Old 12-26-2010, 03:22 AM   #4
josecolella
Member
 
Registered: Nov 2010
Location: Milan, Italy
Distribution: Linux Mint 13, Debian 6.0
Posts: 62

Original Poster
Rep: Reputation: 3
I think I made a mistake
 
Old 12-28-2010, 09:44 AM   #5
josecolella
Member
 
Registered: Nov 2010
Location: Milan, Italy
Distribution: Linux Mint 13, Debian 6.0
Posts: 62

Original Poster
Rep: Reputation: 3
markush,
I have a question, when I execute the scriptt (using bash) I get an error: gzip: *.txt: No such file or directory. mv: cannot stat `*.txt.gz': No such file or directory


Does the script that I started with wrong...should I just erase that because what I want to do is given a directory or given a list of files that it zips them in a file named copia, this copia is within a directory named CopiasSeguridad. If any of the arguments inserted don't exist than it created them
 
Old 12-28-2010, 09:51 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Why not use tar or cpio to archive your files?
 
Old 12-28-2010, 10:12 AM   #7
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
mh, I think I don't understand. You don't want really create new files in order to zip them?

I'd recommend to read a bit about the basics of bashprogramming for example here: http://www.linuxconfig.org/Bash_scripting_Tutorial

In order to check if a directory exists, you may use the following code
Code:
dir = "./copia"
if [ ! -d $dir ]; then
     mkdir $dir
fi
Markus
 
Old 12-28-2010, 10:20 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,253

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Quote:
I get an error: gzip: *.txt: No such file or directory. mv: cannot stat `*.txt.gz': No such file or directory
This error implies that you have zero files with the extension txt in the directory where you executed the script.
The mv error is simply a knock on from the first error
 
Old 01-01-2011, 08:09 AM   #9
josecolella
Member
 
Registered: Nov 2010
Location: Milan, Italy
Distribution: Linux Mint 13, Debian 6.0
Posts: 62

Original Poster
Rep: Reputation: 3
jschiwal,
your idea of using tar is an interesting one.
How would you use it to archive files?
 
Old 01-01-2011, 08:52 AM   #10
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Hi,

try this:
tar -cf archive_name.tar /path/to/directory/
gzip archive_name.tar

Read more about tar:
man tar
 
Old 01-03-2011, 09:40 AM   #11
josecolella
Member
 
Registered: Nov 2010
Location: Milan, Italy
Distribution: Linux Mint 13, Debian 6.0
Posts: 62

Original Poster
Rep: Reputation: 3
crts,
your code was helpful yet I got an error: tar: cowardly refusing to create an empty archive

I want it to accept files as arguments for example name arg1 arg2 arg3 arg4

# @ variable contains the string "arg1 arg2 arg3 arg4
an then compresses these files into a file copiaYYMMDD
where YY stands for the year, MM stands for the month, and DD stands for the days.
the file copiaYYMMDD is in a directory named CopiasSeguridad


this is my revised code:

source_files= #@
tar -cvf archive_name.tar $source_files
gzip archive_name.tar
 
Old 01-03-2011, 09:50 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,253

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
But do the files actually exist??
 
Old 01-03-2011, 09:52 AM   #13
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Hi,

in order to create a tar archive with all files and directories of the current directory, you may use
Code:
tar cf thisdirectory.tar *
Note that you don't need the "-" in front of the options when using tar. Note also, that the gzip command is redundant since you can give it as an option to tar, z for gzip, j vor bzip2
Code:
tar czf thisdirectory.tgz *
will create the archive and compress it with gzip.

Markus
 
Old 01-03-2011, 09:59 AM   #14
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Quote:
Originally Posted by josecolella View Post
this is my revised code:

source_files= #@
tar -cvf archive_name.tar $source_files
gzip archive_name.tar
Don't you mean
source_files=$@

There is no space. And if you want to pass the input arguments of the shell-script then you could do it just as well as
tar -cvf archive_name.tar $@

Or are you using #@ as a placeholder for arg1 arg2 etc.? In any case, make sure that there is no whitespace after '='.
 
Old 01-03-2011, 05:04 PM   #15
josecolella
Member
 
Registered: Nov 2010
Location: Milan, Italy
Distribution: Linux Mint 13, Debian 6.0
Posts: 62

Original Poster
Rep: Reputation: 3
I am using #@ as a placeholder for the arguments(files) that I will insert to be compresed
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Check folder for specific files than move script BlackCrowe Programming 3 11-23-2010 06:59 AM
Write a script to move specific files in various folders to one folder linuxisthedevil Linux - General 13 11-18-2010 07:29 AM
[SOLVED] Bash command to remove all files within a specific folder shayno90 Linux - Newbie 21 10-21-2010 12:55 PM
[SOLVED] Moving files from folders and subfolders to a specific folder mrj2 Linux - Newbie 12 08-22-2010 01:40 PM
Zipping a folder Gins Linux - General 9 06-19-2007 06:17 PM


All times are GMT -5. The time now is 10:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration