Its a interview question. Interviewer asked this “basic” shell script question when he understand i don’t have experience in shell scripting. Here is question.
Copy files from one directory which has size greater than 500 K to another directory.
I can do it immediately in c lang but seems difficult in shell script as never tried it.I am familiar with unix basic commands so i tried it, but i can just able to extract those file names using below command.
du -sk * | awk '{ if ($1>500) print $2 }'
Also,Let me know good shell script examples book.
durecurses into subdirectories, which is probably not desired (you could have asked for clarification if that point was ambiguous). More likely you were expected to usels -lorls -sto get the sizes.But what you did works to select some files and print their names, so let’s build on it. You have a command that outputs a list of names. You need to put the output of that command into the command line of a
cp. If your du|awk outputs this:you want to run this:
So how you do that is with COMMAND SUBSTITUTION which is written as
$(...)like this:And that’s a functioning script. The du|awk command runs first, and its output is used to build the cp command. There are a lot of subtle drawbacks that would make it unsuitable for general use, but that’s how beginner-level shell scripts usually are.