Unix Quiz

If you ever want to see how well someone knows unix (e.g. in an interview), you could ask them this question:

You have a directory containing a lot of files and subdirectories, and you want to copy all of them except for the directory called big_dir. How do you do it?

Here's my solution (select the text to make it visible):

cp -r `ls | grep -v big_dir` dest_dir

Here's another one:

Remove read and execute priviledges from the files you don't want to copy, then proceed as normal

Posted on February 16, 2004 05:33 PM
More programming articles

Comments

The first solution fails in the pathological case of another file matching /big_dir/, as well as in the less uncommon case where the directory is huge. The second modifies the [meta]data... which may or may not be okay.

Of course, part of the unix spirit is knowing when pathologies can be ignored: in most cases they'll sure both be good enough!

I'd do

ls | xargs perl -MFile::Copy -ne 'next if $_ eq "big_dir"; copy $_, "other_dir" or warn "cp:$_:$!"'

Except that File:Copy doesn't handle directories :)

Posted by: Gaal Yahas at February 16, 2004 05:52 PM

find . ! -name "big_dir" -print0 | xargs -0 -i{} cp -pr {} other_dir

takes care of it.

Posted by: Mark A. Hershberger at February 16, 2004 06:15 PM

Hmmm … I would do:

tar --exclude big_dir/ -cf - | (cd dest_dir ; tar xvf - )

Preserves ownership, permissions, etc and excludes that nasty ol' big directory.

Posted by: Sean Conner at February 16, 2004 09:43 PM

I use zsh. Here's one of the reasons why:

mkdir dest_dir
cp -R source-dir/^big_dir dest_dir

(In zsh's glob syntax, the pattern "^foo" matches "all files except foo".) Make it cp -p -R if you care about preserving modes/owners/etc.

Posted by: Doug L. at February 17, 2004 01:05 PM

I like for loops, so:

for f in * ; do if [ "$f" != big_dir ] ; then cp -r $f ../to/ ; fi ; done

Somehow I don't think windows people have quizes like these :-)

Posted by: B. Johannessen at February 17, 2004 09:02 PM

My solution: mv the big_dir away, cp -r, mv the directory back.
Advantage: relatively fast, only uses cp and mv, size of big_dir doesn't matter (as long as the mv is within the same filesystem, of course)
Disadvantage: you need some place to mv the big_dir to and the current directory's modification time is changed, even though it didn't really change.

So many different solutions... isn't unix great?

PS. In my opinion, the solution above using find is the "correct" one.

Posted by: Jorrit Wiersma at February 18, 2004 08:48 AM

B. Johannessen: In windows you open then window, hit Ctrl-A, and shift click on all the items you don't want. Then drag & drop to the destination with the correct modifiers to make a + appear on the cursor. I mean, sure there's a CLUI but windows no longer considers it the primary way of doing stuff.

In NT the for loop answer should work too.

Posted by: Jesse at February 18, 2004 01:38 PM

Jesse: You seem to have missed my point. The point I was trying to make was that windows has one (or at most a few) easy/practical ways of solving a problem. Unix, on the other hand, often allows you to solve the same problem in what sometimes seems like an infinite number of ways. That's one of the things I (and I suspect many others) love Unix systems, and feel confined when having to work in windows.

Posted by: B. Johannessen at February 18, 2004 10:10 PM

Johannessen,
when you say "unix" you mean "find", "grep", and "ls" right?

I tend to be a OS atheist, so to me tools are not the OS and vice versa.

Posted by: drlloyd11 at February 18, 2004 11:41 PM

find . -print | grep -w big_dir/ | cpio -pdumvB other_dir

Posted by: ArizonaEd at April 4, 2004 10:29 PM

daniel is purple and there is not enough poon

Posted by: jesse at June 15, 2004 06:46 PM

This is when copying is done from level where files and Directories to be copied exist.

For Dest_dir Directory at same level where the all the files and dirs to be copied exist:

cp -pr `ls -F|grep -v 'big_dir/'|grep -v 'dest_dir/'` dest_dir

For Dest_dir is at one or more levels above level of files and directories to be copied:

cp -pr `ls -F|grep -v 'big_dir/'` dest_dir

Shortcoming of this method is if dest_dir is inside any directory which is to be copied then it will go into loop and will system space full

Posted by: Harish Kumar at November 6, 2004 01:40 AM

cp -pr `ls -F|grep -v 'big_dir/'|grep -v 'dest_dir/'` dest_dir

Above can be written as:

cp -pr `ls -F|grep -v 'big_dir/|dest_dir/'` dest_dir

Posted by: Harish Kumar at November 6, 2004 01:44 AM

Sorry,

This should be
cp -pr `ls -F|egrep -v 'big_dir/|dest_dir/'` dest_dir

Posted by: Harish Kumar at November 6, 2004 02:11 AM

Heh nice solutions everybody. I actually will use one of them, don't know which one yet for my backup script I am writing. Thanks!

Posted by: Designer at January 12, 2005 04:53 PM

how abt symbolic links? i guess cp wud create copies files. if u want to copy symbolic links. use tar and untar

Posted by: arun at August 7, 2005 04:05 AM

Wonder if you could lend some help with a problem. I am tring to copy files from a disk on another disk except I want to leave out a directory and all its files. I am using a SGI host computer. The normal operations that I follow to copy from one disk to another is this..
find . -mount -depth -print | cpio -pdm /mnt3

This copies all the directories and files but I want to leave out a directory and all its files say named eng_load I have tried this

find . ! -name eng_load -mount -print | cpio -pdm /mnt3. This sill encludes the directory eng_load. I have also tried this...

find . ! -name eng_load -prune -print | cpio -pdm /mnt3. Still does not work.

and ideas????

Thanks
Tom

Posted by: Tom at August 11, 2005 08:42 AM

bhag madarchoodh

Posted by: at February 10, 2009 06:10 AM

nice post $ nice solutions !

Posted by: Free Grants at September 9, 2009 05:43 AM

Great post mate, keep it up.

Posted by: free online games at October 25, 2009 04:08 PM

These Women's Classsic Tall 5815 Boots in pink flower print can either be worn at their normal height if you want Ugg boots, but in order for you to create a more casual look to what you are wearing they can be pushed down Women's UGG Classic Tall 5815 Boots in Baked Clay

Posted by: ugg boots on sale at October 27, 2009 02:36 PM

http://www.kimbly.com/blog/000070.html

Posted by: tiffany jewelry at October 28, 2009 06:13 AM

Love this man. Great stuff, great prices, free shipping. Email him if you cant find what you are looking for and he will look for you. Good customer service.louis vuittonSomeone who is knowledgeable in louis vuitton.Another guy from hong Kong (Largest louis vuitton second hand city, I believe) he has great stuff at fair prices but his shipping is insane

Posted by: louis vuitton at October 29, 2009 08:51 AM

Good day! Thx for your great post and Im thinking about how to introduce my ugg boots to you cos Im not sure if u like this. Many people who live in the cold area like the ugg boots, especially the north-europe. Our uggshare.co.uk supply the cheapest classic cardy ugg boots, class tall ugg boots, classic short ugg boots and other series of uggs, hope my words didn’t trouble u, cy!

Posted by: ugg boots at November 11, 2009 10:05 PM

Good day! Thx for your great post and Im thinking about how to introduce my ugg boots to you cos Im not sure if u like this. Many people who live in the cold area like the ugg boots, especially the north-europe. Our uggshare.co.uk supply the cheapest classic cardy ugg boots, class tall ugg boots, classic short ugg boots and other series of uggs, hope my words didn’t trouble u, cy!

Posted by: ugg boots at November 11, 2009 10:05 PM

Good day! Thx for your great post and Im thinking about how to introduce my ugg boots to you cos Im not sure if u like this. Many people who live in the cold area like the ugg boots, especially the north-europe. Our uggshare.co.uk supply the cheapest classic cardy ugg boots, class tall ugg boots, classic short ugg boots and other series of uggs, hope my words didn’t trouble u, cy!

Posted by: ugg boots at November 11, 2009 10:05 PM

Eliminating the need for the extra trouble to wear ugg boots uk
ugg boots saleuggs, in the extreme sports, look for happiness in life, with UGG Classic boots and nike sb showed off your tall body, do not worry about being laughed at, dancing new style ugg australia and nike sb dunk . The flavor of life that is colorful, there are Christian Louboutincheap nike dunksUGG cardy boots、and nike sb for sale also used fear to creep away money from your pocket Mody ? Love beauty, you might even love UGG Classic tall bootsUGG Nightfall bootsUGG Classic short bootsnew nike sb。There are an infinite high power drivers of popular front ugg boots saleugg saleuggsnike dunkdiscount ugg bootsnike dunk sb, our website also have UGG Nightfall bootsbutton ugg bootsbailey button uggugg button bootsbailey button bootsnike dunk midnike dunk highChristian Louboutin Boots, pick your favorite, do not hesitate!

Posted by: ugg boots sale at November 12, 2009 03:58 AM

Uggs on sale now.UGG Classic Cardy Boot makes me different form the other

girls. The UGG Bailey Button Boot is a good choice for female.

Posted by: ugg classic cardy boots at November 16, 2009 11:53 PM

Louis Vuitton , commonly referred to as Louis Vuitton Sneakers and Gucci Diaper Bags on Sale, or sometimes shortened to Canvas Handbags has become one of the most louis vuitton alexandra wallet Agendas luxurybrands Tiffany In Hongkong.

Posted by: 1 at November 17, 2009 01:41 AM

UGG Bailey Button bootsis a new style in 2009.The classic cardy uggs boots is another hot boots that worth of buying.And the classic tall ugg boots will make your winter amusing.And now uggs on sale,if you are looking for such a boot,the ugg boots is good choice this year.

Posted by: UGG Bailey Button Boots at November 17, 2009 01:58 AM

I love Women's UGG Classic Cardy Boot 5819 very much. And the new style UGG Mayfaire Boots is aslo a good choice this year.

Posted by: UGG Bailey Button Fancy Boots at November 17, 2009 03:14 AM

lxjuan08ed hardyis a famous ed hardy store which sell directly ed hardy clothing, shoes, boots, swim suit and other cheap ed hardy.

Browse through our catalogue of hundreds of ed hardy clothing,Ed hardy Shirt,Ed hardy ... Buy Ed Hardy today for a gift, for a friend, or for yourself.

ED HARDY Online Shop,Ed Hardy Store by Christian Audigier,coach handbags,rolex watches.We supply cheapest Newest ed hardy discount 2009 Commodity New styles.


ed hardy sale

Posted by: ed hardy at November 18, 2009 08:20 PM

We are the best online sales for the china wholesale . Here you can have a large of choices of kinds Ugg Boots,Converse Shoes,Timberland Boots,puma shoes,Nike Shox Shoes ,Nike Dunk SB Shoes,Nike Air Max,Links Of London,Tiffany Jewelry,Dior Handbags?,jimmy choo handbags ,Cartier Watches, 8GB Mp4 Players,Bluetooth Car DVDs. All our cheap online cheap goods are high quality and original packages, and best service. We offer our customers the best service, 7 days arrive at your door.Enjoy your easy and happy shopping with us.

Posted by: cheap goods sale. at November 19, 2009 10:04 PM


louis vuitton has affirmed itself over the past decades as a world leading brand name in the fashion industry.


Throughout their stay as one of the best brands, louis vuitton designer handbags have managed to mesmerize the mass with their style, grace and finesse.

Posted by: wang at November 20, 2009 02:25 AM

Do you like sports? If you like it,the NIke Air Max TN and Nike Air Max 360 is a good choice for you.And now the there is a good news,CheapNike Air Max running Shoes is waiting for you.I think The New Nike Air MAX 2009 Running Shoes makes you great comfortable.

Posted by: Nike Air Max shoes at November 20, 2009 03:31 AM

Welcome to our website www.shoes-base.com .You can buy something you like here.Thanks.
air yeezy shoes
nike shoes
cheap nike dunk
nike dunk sb
air force ones
Timberland Shoes
Nike Shox
KOBE shoes
jordan jf retro

Posted by: air yeezy shoes at November 21, 2009 11:11 PM

Do you think I can stay timberland boots uk to become nothing to you ? Do you think I am a timberland boot automaton ? --a discount timberland boots machine without feelings ? and can bear classic timberland boots to have my morsel of bread snatched from my lips, and my drop of kids timberland boots living water dashed from my cup? Do you think, because I am timberland mens shoes poor, obscure, plain, and little, I am soulless and heartless? You think timberland 6 inch boots wrong!--I have as much timberland custom boots soul as you,--and full as much timberland roll top boots heart! And if God had timberland womens shoes gifted me with some beauty and much wealth, I should have made it as hard for you to leave me, as it is now for me to leave you. I am not talking to you now through the medium of timberland hiking boots custom, conventionalities, nor even of mortal flesh.

Posted by: ugg boots at November 22, 2009 11:08 AM

Your article is write very well, I like it very much ~
ghd Hair Straightener
ghd Hair Straighteners
ghd straighteners
GHD
hair straighteners
bose in ear headphones
bose on ear headphones
bose headphones
nike sb dunks
nike dunks
I wish you have a wonderful day!Thank you.

Posted by: ghd straighteners at November 22, 2009 08:38 PM

Timeberland boots on sale now! Do you want moreTimeberland boots information?Just move your mouse and press it to our Timberland shoes online store.

Posted by: Timberland uk at November 22, 2009 11:16 PM

Laptop Battery Laptop Battery Laptop Batteries
Laptop Batteries discount laptop battery
discount laptop battery
notebook battery notebook battery
computer battery computer battery
replacement laptop battery replacement laptop battery
notebook batteries notebook batteries

Posted by: Laptop Battery at November 25, 2009 12:09 AM

People all over the world know the abercrombie and fitch,but not everyone really knows how fashion the abercrombie is,hollister is the Legend maker. Everybody wears the hollister clothing would be the abercrombie mens and the abercrombie womens, if you want know you can search the Ruehl No.925 or abercrombie outlet in the www.google.com .

Posted by: fitch at November 25, 2009 10:14 PM

流水线

Posted by: 生产线 at November 25, 2009 11:01 PM

chaoying nfl jerseys
Troy Polamalu Jersey
Hines Ward Jersey

Posted by: jerseysleague at November 29, 2009 04:24 AM

Paul Smith has been collaborating with a few different organizations recently and his latest venture sees him teaming up with Japanese bicycle saddle, Paul Smith bag, on a limited edition leather saddle. The release features Smith’s signature stripes where only 20 will be available at Paul Smith Clothing store in Japan and 8 at the Floral Street store in London. Get on it quick

Posted by: dasd at November 29, 2009 11:29 AM

Just wanted to say great job with the blog, today is my first visit here and I’ve enjoyed reading your posts so far
ugg bailey button
Wow, my ugg classic mini will not be coming off now! I’ve had them on for 12hrs strait and I do not want to take them off. Thanks for everything, well worth the wait.

Posted by: ugg bailey button boots at November 30, 2009 06:17 AM

Just wanted to say great job with the blog, today is my first visit here and I’ve enjoyed reading your posts so far
ugg bailey button
Wow, my ugg classic mini will not be coming off now! I’ve had them on for 12hrs strait and I do not want to take them off. Thanks for everything, well worth the wait.

Posted by: ugg bailey button boots at November 30, 2009 06:25 AM

It is no doubt that Replica Louis Vuitton Speedy 30 enjoys highest reputation in the world. Louis Vuitton Speedy 30, purses, shoes, watches, clothes, and belts successfully seize loving heart of a majority of people. Speedy 30 is filled with romantic and fantasy, and it is a symbol of noble and high quality of life for gucci ever! Today, let us talk about Tivoli PM.belts have different styles and colors, such as Monogram series belts, Graffiti series belts, both men and women love them crazily. All of them match with various leisure clothes, formal business suits, and beautiful clothes very well. Whenever you wear a Louis Vuitton belt to make your public appearance, you will be naturally concerned and admired by every body. When we talk of high quality, there is no other company to compare with Louis Vuitton Replica Speedy 30. All Louis Vuitton belts adopt top quality cow leather, high quality hard ware, and the color will never be faded. The belts are so durable, and you can wear them to match your clothes for several years. Also, you can clearly see an obvious LV logo as a buckle of each belt.

Posted by: fanqin at December 1, 2009 01:25 AM
Post a comment









Remember info?




Prove you're human. Type "human":