1753373 Members
4921 Online
108792 Solutions
New Discussion

cp -p not working

 
rockstar05
New Member

cp -p not working

Hello,

currently, I'm using HP-UX 11.31.
I would like to copy some of the files, which was generated from 1st Apr to 31st Apr from root user.

I tried with below command but it gives me usage of cp command,


cp -p `ls -lrt |egrep -i "Apr ( [1-9]|1[0-9]|2[0-9]|3[0-1] )"|egrep -v "2004 | 2005 | 2006 | 2007 | 2008 | 2009 | 2010 | 2011 | 2012 | 2013 | 2014 | 2015" | grep root | awk '{print $9}'` /tmp/logs/root/

output,
Usage: cp [-f|-i] [-p] [-S] [-A] [-e warn|force|ignore] source_file target_file
cp [-f|-i] [-p] [-S] [-A] [-e warn|force|ignore] source_file ... target_directory
cp [-f|-i] [-p] [-S] -R|-r [-A] [-e warn|force|ignore] source_directory ... target_directory
cp -R[-H | -L | -P] [-f|-i] [-p] [-S] [-A] [-e warn|force|ignore] source_file ... target
cp -r[-H | -L | -P] [-f|-i] [-p] [-S] [-A] [-e warn|force|ignore] source_file ... target

I think I'm doing something wrong with command.

Anyone please help me where I went wrong.

4 REPLIES 4
Steven Schweda
Honored Contributor

Re: cp -p not working

> I think I'm doing something wrong with command.

   "cp" apparently thinks so, too.

> Anyone please help me where I went wrong.

   With my weak psychic powers, I can't see the output of your complex
"ls" + "egrep" + "awk" pipeline, so I can't see what you're feeding into
"cp".

> [...] ls -lrt [...]

   "rt"?  Why do you care about the order?

> I would like to copy some of the files, which was generated from 1st
> Apr to 31st Apr [...]

   There are "find" options which are useful for this kind of thing
which don't require you to parse output from "ls -l".  Note that parsing
the output of "ls -l" is probably more difficult than you think.

      man find

A forum or Web search should find many examples of "find" used this
way.

Dennis Handly
Acclaimed Contributor

Re: cp files in a date range

>it gives me usage of cp

 

Typically pipelines suffer ultraviolet catastrophe then they produce an empty result.

You may want to assign to a variable and see if empty.

(Either that or it doesn't like you trying to access Apr 31.  :-)

Also it may be easier to understand a procedural language like awk doing the filtering, rather than your nested greps.

awk allows you to easily look at columns and your grep for root, won't find it in a filename or a substring of fooroot.

 

As Steven mentioned, there are plenty of examples of find(1) in the Language forum, using reference files to bracket the start and end dates.

 

>Note that parsing the output of "ls -l" is probably more difficult than you think.

 

If you modify ls.cat, you can make it simpler.  :-)

ranganath ramachandra
Esteemed Contributor

Re: cp -p not working

you could try systematically debugging your command line by  making sure each piece in the pipeline is actually printing its output in the format you expect it to (e.g. first do the ls|egrep part by hand, then pass to grep, etc).

you could give a quick try to using $NF instead of $9 in the awk command.

 
--
ranga
[i work for hpe]

Accept or Kudo

Steven Schweda
Honored Contributor

Re: cp -p not working

> you could try [...]

   True.  Seeing any of the actual data along the pipeline would be
better than seeing none of them.

> you could give a quick try to using $NF instead of $9 in the awk
> command.

   Such techniques may work in some cases, but any file name which
contains a shell-special character can be expected to cause trouble.
"find" avoids these problems, too.