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 01-04-2011, 07:37 AM   #16
josecolella
Member
 
Registered: Nov 2010
Location: Milan, Italy
Distribution: Linux Mint 13, Debian 6.0
Posts: 62

Original Poster
Rep: Reputation: 3

I have changed my code to

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

source_files=$@
tar zcvf copia%y%m%d.tar

...I have a problem though because I need to use "%y%m%d" with the command 'date' so that the files that I choose to compress,
zips into a specific file that represents the year,month, and day that it is compressed.
 
Old 01-04-2011, 08:18 AM   #17
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
easy on the whitespace ...

Hi,

this is not directly related to your problem, but your sha-bang is wrong. It has too many whitespaces. It should be
Code:
#!/bin/bash
You really need to be careful with whitespace. If you were to execute a command like 'rm' you might irreparably damage your system by misplacing a whitespace.


Now to your problem. Try
Code:
tar zcvf copia$(date +%Y%m%d).tar # UNTESTED!
Have have a look at
Code:
man date
for more info and options on date.

Last edited by crts; 01-04-2011 at 08:19 AM.
 
Old 01-04-2011, 09:23 AM   #18
josecolella
Member
 
Registered: Nov 2010
Location: Milan, Italy
Distribution: Linux Mint 13, Debian 6.0
Posts: 62

Original Poster
Rep: Reputation: 3
Thanks,

Now the only problem I have is that I need the script to ask me the arguments(files) to compress...
what I'm thinking is using the command read to ask me which files to compress instead of compressing the
entire directory with *
 
Old 01-04-2011, 09:33 AM   #19
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,423

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
So as that is fairly trivial, maybe you can show us what you have thought of?
 
Old 01-05-2011, 09:43 AM   #20
josecolella
Member
 
Registered: Nov 2010
Location: Milan, Italy
Distribution: Linux Mint 13, Debian 6.0
Posts: 62

Original Poster
Rep: Reputation: 3
This is my new code, I still have a question though, I get an error though because even though the script asks me what files to compress, it doesn't compress those files, it compresses the entire directory with *

The error I get is "read: 'source_files=': not a valid identifier

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

printf "Enter the name of the file you want to compress?:\n"
read source_file=$@
tar -zcvf copia$(date +%y%m%d).tar $source_files
 
Old 01-05-2011, 09:54 AM   #21
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
The $@ refers to the positional parameters that are passed to the script when it is called. If you want to manually read every single file during execution then you annot use $@. Change your code to:
#!/bin/bash
# Title: Compress a file
# Author: Jose Miguel Colella
# Description: Compress a file

printf "Enter the name of the file you want to compress?:\n"
read -r source_file
tar -zcvf copia$(date +%y%m%d).tar "${source_files}"


BTW, you still have a whitespace in your shabang.
 
Old 01-05-2011, 10:35 AM   #22
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 has these errors
tar: {name of folder}: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors

how do I fix this error?
 
Old 01-05-2011, 10:45 AM   #23
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,423

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Well the error seems fairly self explanatory ... the directory you entered does not exist.
Maybe you should put a test in first to see that it does, like:
Code:
if [[ -e $source_file ]]
then
    tar -zcvf copia$(date +%y%m%d).tar "${source_files}"
else
    echo "The following does not exist: ${source_files}"
fi
 
Old 01-05-2011, 10:48 AM   #24
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
By correcting the typo. I mainly copy+pasted your script.
The variable has to be
"${source_file}"
 
Old 01-05-2011, 11:10 AM   #25
josecolella
Member
 
Registered: Nov 2010
Location: Milan, Italy
Distribution: Linux Mint 13, Debian 6.0
Posts: 62

Original Poster
Rep: Reputation: 3
thanks grail,
just one more thing though, even if the directory/file doesn't exist, I want it to create that file/directory and compress it into the designated folder...Also I need the script to accept as many arguments as I need such as if I want to compress X files I can write in X arguments and they will be compressed into their designated folder


my code:

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

printf "Enter the name of the file you want to compress?:\n"
read source_file
if [[ -e $source_file ]]
then
tar -zcvf copia$(date +%y%m%d).tar "${source_files}"
else
echo "The following does not exist but the directory is being created: ${source_files}"
fi
 
Old 01-06-2011, 11:47 AM   #26
josecolella
Member
 
Registered: Nov 2010
Location: Milan, Italy
Distribution: Linux Mint 13, Debian 6.0
Posts: 62

Original Poster
Rep: Reputation: 3
This is my new code but I still need to compressed with gzip and copy the file
prior to a folder which I think that I did...


#! /bin/bash
# Titulo: Comprimir un archivo
# Autor: Jose Miguel Colella
# Descripcion: Comprimir un archivo

printf "Introduzca el nombre del archivo que quieres comprimir?:\n"
read source_files
fichero=copia$(date +%y%m%d).tar
echo "El fichero se llamara: $fichero"

tar -zcvf $fichero $source_files
 
Old 01-06-2011, 09:16 PM   #27
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,423

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Quote:
even if the directory/file doesn't exist, I want it to create that file/directory
Then in the else of your test you can use touch and mkdir and then compress.

I would have to say I see absolutely no point to this though as placing empty files and / or directories in an archive would appear pointless.
Quote:
I still need to compressed with gzip
The 'z' in the tar command already does this for you, simply add .gz to the following:
Code:
fichero=copia$(date +%y%m%d).tar.gz
 
Old 01-07-2011, 03:12 AM   #28
josecolella
Member
 
Registered: Nov 2010
Location: Milan, Italy
Distribution: Linux Mint 13, Debian 6.0
Posts: 62

Original Poster
Rep: Reputation: 3
Thanks Grail,
you helped a lot throughout this process, thanks for the patience that you have shown with my inexperience of the linux script...I will learn
 
Old 01-07-2011, 03:33 AM   #29
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,423

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
We all start somewhere and we all have more to learn
 
  


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 11:08 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