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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
01-12-2017, 02:34 PM
|
#31
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,892
|
Quote:
Originally Posted by trickydba
@rtmistler......I couldn't find the issue after doing what you suggested. If you have folders created like '01012017' that go all the way to '12312017' and you wanted to tar them to one file named as such 'JanJun.tar' and 'JulDec.tar' how can this be done?
I researched doing this with files but couldn't find anything for compressing folders.
|
Tar will compress and also recurse down directories. My personal favorite flags for that type of operation are -cvf, where it will create, and use verbose output. Depending on the tar filename, it will compress. For instance if you say "result.tar.bz2" it will compress to a bz2 style file. If you say "result.tar.zip", it will compress to a zip style file. I haven't experimented much and usually use bz2, but also use zip for those needing to look at it easily with Windows systems even though most modern Windows will seem to recognize bz2. Other variations such as 7z I have not tried.
My best suggestion is to always stage that in a command line first to validate that you get what you desire. Don't just jump in and assume the .zip extension works, try it out and also transfer that file to a destination system where you wish to re-read it. Or whichever extension you end up choosing.
Note that there used to be a -z flag for tar, and it probably still accepts it but ignores it silently. It might be useful for you to check the version of tar you have, check the manual page for the tar you have and verify that it will work in the manners I'm describing.
|
|
1 members found this post helpful.
|
01-12-2017, 02:59 PM
|
#32
|
Member
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310
Original Poster
Rep:
|
UPDATE...Ok I got it partially working. Here is the code again:
Code:
#! /bin/bash
echo "**************************************************************
***************** Multi-Folder Compressor ********************
****************** By Me **********************
*************** Created on January 12,2017 *******************
**************************************************************
Please choose an option:
1)Compress Jan to Jun
2)Compress Jul to Dec
3)FUTURE USE
4)Disabled "
read n
clear
echo "Please provide the year ( if you chose option 3 or 4, just hit ENTER )"
read YEAR
case $n in
1) echo "Compressing folders Jan to Jun "; find . -mtime 182 | xargs tar -zcvf /export/home/TESTING/JanJun${YEAR}.tar.gz;;
2) echo "Compressing folders Jul to Dec "; find . +mtime 182 | xargs tar -zcvf /export/home/TESTING/JulDec${YEAR}.tar.gz;;
3) echo "For future use....closing ";;
4) echo "This option is disabled ";;
*) invalid option;;
esac
When I select #1, I get this error:
Code:
Compressing folders Jan to Jun
tar: Cowardly refusing to create an empty archive
But when I select #2 it works, but looking at the output it seems to have compressed these folders twice:
01062017
01042017
01032017
01012017
01022017
01052017
which of course nothing should have been compressed.
Any help is highly appreciated!
|
|
|
01-12-2017, 03:55 PM
|
#33
|
Moderator
Registered: Aug 2002
Posts: 26,127
|
-mtime 182 finds files modified exactly 182 days ago. Since there is not an option +mtime your results are not valid.
-mtime n (n*24 hours)
+n for greater than n,
-n for less than n,
n for exactly n.
If you want to go by mtime you will need to specify a range but you need to calculate the days based upon the current date. If today was 12/31 then something like the following would find files between Jan 1 - Jun 30 (I did not calculate exact days...)
-mtime +182 –mtime -365
You can find files based upon the file name using wildcard expansion. Although it isn't pretty and others might have a better idea.
find . -maxdepth 1 -name "0[0-6]*"
To specify a year
find . -maxdepth 1 -name "0[0-6]??YYYY"
Would find all files/directories in the current working directory from Jan - Jun assuming they were named MMDDYYYY.
For July-August
find . -maxdepth 1 -name "0[7-9]*"
For Sept-Dec
find . -maxdepth 1 -name "1[0-2]*"
July-Dec would need 3 commands i.e two to create append the tar file and a separate command to compress.
I know that some of these commands can be confusing and in most cases you can find decent help and examples.
|
|
1 members found this post helpful.
|
01-13-2017, 06:52 AM
|
#34
|
Member
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310
Original Poster
Rep:
|
Too confusing to me. I came up with a better idea. How can I compress folders( named like 01012017,02012017,etc ) based only on the first 2 digits (the month)?
Like for instance to only compress folders where the range is from 01****** to 06******
Last edited by trickydba; 01-13-2017 at 07:01 AM.
|
|
|
01-13-2017, 07:24 AM
|
#35
|
Member
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310
Original Poster
Rep:
|
Would this work?
Code:
echo "Compressing folders Jan to Jun "; find . "01*" ! "06*" | xargs tar -zcvf /dir1/dir2/dir3/TESTING/JanJun${YEAR}.tar.gz;;
Last edited by trickydba; 01-13-2017 at 07:31 AM.
|
|
|
01-13-2017, 07:32 AM
|
#36
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,892
|
Quote:
Originally Posted by trickydba
Would this work?
Code:
echo "Compressing folders Jan to Jun "; find . "01******" ! "06******" | xargs tar -zcvf /dir1/dir2/dir3/TESTING/JanJun${YEAR}.tar.gz;;
|
I believe one * should be sufficient. If you wish to mandate that you have exactly 6 digits following the 01 or 06 terms, there are regex expressions which you can use; however you'd also have to account for additional non-digits following the remainder of the dates.
|
|
1 members found this post helpful.
|
01-13-2017, 07:42 AM
|
#37
|
Member
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310
Original Poster
Rep:
|
This is the code I used:
Code:
echo "Compressing folders Jan to Jun "; find . "01*" ! "06*" | xargs tar -zcvf /dir/dir/dir/TESTING/JanJun${YEAR}.tar.gz;;
ans this is the error I got:
Code:
Compressing folders Jan to Jun
find: paths must precede expression: 06*
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
tar: Cowardly refusing to create an empty archive
Last edited by trickydba; 01-13-2017 at 07:47 AM.
|
|
|
01-13-2017, 07:54 AM
|
#38
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,892
|
There's a syntax error in your find command. I know there are shortcuts; however I always use: and you cannot use the AND to combine search terms.
This would either be six find commands, one for each month, or using a -since or date qualifier to capture the files since your last backup.
Once again, I know there are highly particular requirements. I'm remembering that this is to back up information you've generated on the behalf of a company you work for in some manner.
Personally I'd reorganize files once they'd been backed up. Make my backup and then move the backed up files to an "old" sub-directory or something similar.
If things are so restricted that you cannot move files, such as you're not allowed to move files, and this company is so very highly specific about everything, then I question whether or not you should be keeping any copies.
I've worked as a contractor for highly restrictive employers, such as government contractors, and you're not supposed to take any IP out of their shop at all. Not allowed. Then again, the security restrictions were sufficient such that if I tried to take out a drive of any type, it would be a problem, let alone bring in anything capable of storage.
If this is a commercial company, or something more serious such as aviation or power grid, things like that where once again they have protected systems. Same thing. I would not be keeping any personal records.
This "sounds" as if you're generating status reports and you wish to keep a copy. So ... keep a copy on another system, this laptop you mention, and back that up however best works for you.
|
|
1 members found this post helpful.
|
01-13-2017, 08:35 AM
|
#39
|
Moderator
Registered: Aug 2002
Posts: 26,127
|
As stated you have to specify each month which can be done using or. Not tested but should work.
Code:
find . -type d \( -name "01*" -o -name "02*" -o -name "03*" -o -name "04*" -o -name "05*" -o -name "06*"\)
|
|
1 members found this post helpful.
|
01-13-2017, 08:59 AM
|
#40
|
Member
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310
Original Poster
Rep:
|
I tried this code and nothing returns but I do not want to use OR, I want to pick the folders from Jan(01*) to Jun(06*):
Code:
echo "Compressing folders Jan to Jun "; find . -type d \( -name "01*" -o -name "02*" -o -name "03*" -o -name "04*" -o -name "05*" -o -name "06*"\) | xargs tar -zcvf /dir/dir/dir/TESTING/JanJun${YEAR}.tar.gz;;
I also tried this code;
Code:
echo "Compressing folders Jan to Jun "; find . -type d "01*" ! "06*" | xargs tar -zcvf /dir/dir/dir/TESTING/JanJun${YEAR}.tar.gz;;
and got this error:
Quote:
Compressing folders Jan to Jun
find: paths must precede expression: 01*
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
tar: Cowardly refusing to create an empty archive
|
Last edited by trickydba; 01-13-2017 at 09:28 AM.
|
|
|
01-13-2017, 09:30 AM
|
#41
|
Moderator
Registered: Aug 2002
Posts: 26,127
|
In my previously post there was not a space between the last " and \ i.e. "06*"\) vs "06*" \) which caused a syntax error.
Code:
$ ls
01012017 02022017 03012017 04012017 05012017 06012017 07012017 11012017
Code:
find . -type d \( -name "01*" -o -name "02*" -o -name "03*" -o -name "04*" -o -name "05*" -o -name "06*" \)
./05012017
./03012017
./02022017
./06012017
./01012017
./04012017
If the current working directory contains the subdirectories as named above the command returns the results as listed.
Code:
find . -type d \( -name "01*" -o -name "02*" -o -name "03*" -o -name "04*" -o -name "05*" -o -name "06*" \) | xargs tar -zcvf testme.tgz
Completed successfully and listing its contents.
Code:
tar -tf testme.tgz
./05012017/
./03012017/
./02022017/
./06012017/
./01012017/
./01012017/f1.txt (empty file)
./04012017/
|
|
1 members found this post helpful.
|
01-13-2017, 09:56 AM
|
#42
|
Member
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310
Original Poster
Rep:
|
After placing .xlsx files in the months of '02', '01' and '06' it appears that the code below works:
Code:
find . -type d \( -name "01*" -o -name "02*" -o -name "03*" -o -name "04*" -o -name "05*" -o -name "06*" \)
Thank you everyone for your help
|
|
|
01-22-2017, 11:34 PM
|
#43
|
LQ Newbie
Registered: Jan 2017
Posts: 1
Rep:
|
I just think everything is possible
|
|
|
All times are GMT -5. The time now is 03:33 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|