Linux - NewbieThis 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.
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.
thx that worked just fine, and now i want that to fuction with a find command. I would like to find every log files in a directory and zip them down, but when i use the following command it will go forever...hehe
Wed Oct 20 root@xxx:~/log/# ls
. .. 1 P9250850.log P9250852.log ......
Wed Oct 20 root@xxx:~/log/# ls -la
The problem is that it will continue forever, another option i would also have is not down zip down files that end with *.log.gz in the same directory, just the files that end with *.log.
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,304
Rep:
edit: do not read. the command will not work, and the explanation is wrong too.
find . "*.log" -exec tar -czvf log.tgz {} \;
that should do it, but i'm drinking, so who knows.
to explain a little, the {} puts the results of the find command there, so if you make the find search specific, you will just get the results of the find command tarred. the * isn't needed.
so in the case of this command, the find command places the filenames it "finds" in the tar command. tar -czvf log.tgz {results of find command}. the \; just terminates the -exec.
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,304
Rep:
Quote:
Originally posted by linuxtesting2 ok, but what if i want every files except *.gz files, say that i have *.log *.jpg *.c *.h *.cpp files...how can i just do like this
Find every files and zip them down except *.gz and *.h
-ole
i've seen a command that would do it, but i can't remember.
you may be happy with gzip -r *. it'll skip gzipped files.
i think i messed up on the find command before. it would repetatively make a tar file of one name over and over for each filename. not what you want. I've gotta go.
Originally posted by linuxtesting2 Wed Oct 20 root@xxx:~/log# find . "*.log" -exec tar -cvzf tarfile.tgz {} \;
./
./PA160872.log
./PA160874.log
./PA160872.log
./PA160874.log
./tarfile.tgz
tar: ./tarfile.tgz: File shrank by 893260 bytes; padding with zeros
tar: Error exit delayed from previous errors
find: *.log: No such file or directory
Wed Oct 20 root@xxx:~/log#
Has anyone actually tried this and then extracted the contents to see if it worked? I don't think it will. I think only the last file the find command finds will be zipped. Better to do a find and pipe to tar to zip multiple files found using the find command.
find . -name '*.log' | xargs tar cvzf tarfile_name.tgz
First of all, none of these find suggestions will work because none used the -name option to have a file name. Every one of them would error out.
Also, xargs lets the user pump a completed found list into tar instead of -exec which pumps the found items through one at a time. -Exec is almost never useful. Usually, when you post a question about how to use find, helpful people will run man on find and build a single command line entry with only find using what they see there. But find's -exec is actually kind of lame in most cases, and xargs with a pipe is the better way to go.
But there are many roads to perdition - this is not the only solution.
The difference is in the way the command runs using a pipe and xargs is very important.
Using find with -exec the command runs end to end with each item found.
find --- is something found? If yes --- -exec this command; if no--- go back to find ---if everything done, then exit out.
This results in a problem if you are building a compressed archive of files - every time the command finds a file to compress, it destroys the archive and replaces it with a new one with the same name.
But with xargs, find completes its list before the data is piped to tar, so that when the tar archive is built, all of the files are put into it and it is only built once.
find --- is something found? Add to list. ---- if everything searched --- exit and send list to command by xargs.
Quote:
Find every files and zip them down except *.gz and *.h
This is not that difficult to do.
find . '*'
Will find nothing or everything - it will error out without a name argument.
find . -name '*'
Will find everything.
grep -v .h
Will whittle out of the list anything with .h
grep -v .gz
Will whittle out anything with .gz on the end
Then pipe to tar using xargs (this will be the remaining file name list
find . -name '*' | grep -v .h | grep -v .gz | xargs tar cvfz tarfile.tgz
That should do it. I tested it on my system by creating a directory with four files in it. Three of them were png images, and the fourth was a bz2. I found all files, grep -v'd away the bz2, and tar'd up the rest with xargs just as written above, and it worked just fine.
Great job! You now know how to figure this stuff out for yourself. Learning "find" is one of the keys to Unix happiness. It is incredibly powerful in concert with xargs and grep.
Now - see if you can figure out how to reduce that command line down to fewer pipes and then you will be on your way.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.