List all files and copying to different location

Hello All,

I want to copy all files from the list below to a path /qa0/repo/AS/CPY

(lnx17:qa0:/qa0/repo/AS>) ls -lrt | grep 'Feb 14'
-rw-rw-r-- 1 Ks1 qa0         39 Feb 14 20:11 HHH.dat
-rw-rw-r-- 1 Ks1 qa0         32 Feb 14 20:11 HHH1.dat
-rw-rw-r-- 1 Ks1 qa0         29 Feb 14 20:11 HHH2.dat
-rw-rw-r-- 1 Ks1 qa0         38 Feb 14 20:11 HHH3.dat
-rw-rw-r-- 1 Ks1 qa0         30 Feb 14 20:11 HHH4.dat
-rw-rw-r-- 1 Ks1 qa0         36 Feb 14 20:11 HHH5.dat

I tried this below and it didn't worked. Any help

 ls -lrt | grep 'Feb 14' | xargs -I '{}' cp {} /qa0/repo/AS/CPY

You did not pass the proper output to xargs.
Filenames are needed, and you passed entire output of ls -lrt | grep .. command.

What would happen if you issue cp against one line of input ?

cp -rw-rw-r-- 1 Ks1 qa0         39 Feb 14 20:11 HHH.dat /qa0/repo/AS/CPY

A cp would output an error cp: invalid option -- 'w'

In reality, you got unpredictable result,only luck and coincidence helped no damage is done.

You can achieve desired output using awk , cut or similar tools.
But that code can break on filenames with spaces, and possibly others.
If you are sure those will not exist...

So, for travesting filesystem and doing operations the safest tools would be find and xargs with print0 and option -0 .

Hope that helps
Regards
Peasant.

Hello Arun,

the below command works for your purpose. However keep in mind about the issues pointed out by Peasant.

ls -ltr | grep "Feb 14" | awk '{print $NF}' | xargs -I '{}' cp {} /qa0/repo/AS/CPY
1 Like

Why not

ls -l | awk '/Feb 14/ {print $NF}' | xargs -I '{}' cp {} /qa0/repo/AS/CPY

, then? Works only if there are no spaces et al. in the file names.

1 Like