List all files and copying to different location

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers List all files and copying to different location
# 1  
Old 02-23-2018
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

Code:
(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

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

# 2  
Old 02-23-2018
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 ?
Code:
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.
# 3  
Old 02-23-2018
Hello Arun,

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

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

This User Gave Thanks to Kesavan For This Post:
# 4  
Old 02-24-2018
Why not

Code:
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.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying the files in to multiple location using shell script

Hi All, i'm trying to copy the 1.txt files (sample files) in to different path location using the below command. But it is not copying the files , when i tried for single location able to copy the file. can any one please assist here. Please find the below path :- /ckr_mkr1/licencekey... (2 Replies)
Discussion started by: venkat918
2 Replies

2. Shell Programming and Scripting

Copying files from various folders to similar folder structure in another location

Hi, I need to write a script the has to copy the files from folders and subfolders to the same folder structure located in another location. Ex: mainfolder1 file1,file2,file3 subfolder1(file1,etc) subfolder2(file1,etc) to another folder location of same folder structure. rsync is not... (7 Replies)
Discussion started by: Raji Perumal
7 Replies

3. Shell Programming and Scripting

Help with copying the list of files from one location to other location

A) I would like to achive following actions using shell script. can someone help me with writing the shell script 1) Go to some dir ( say /xyz/logs ) and then perform find operation in this dir and list of subdir using find . -name "*" -print | xargs grep -li 1367A49001CP0162 >... (1 Reply)
Discussion started by: GG2
1 Replies

4. Programming

Copying folders from one location to another..

Hi all, I need a suggestion in the following case, I have folder1, folder2 ,folder3 and file1 inside /home/test/source .. I need to copy all the folders and files to another location /home/test/destination Pls suggest any way to program this in C++.. :confused: (1 Reply)
Discussion started by: selvarajvs
1 Replies

5. Shell Programming and Scripting

Copying files from windows shared location to linux box

Hi, i am new to shell scripting. i have a requirement to copy the files from a windows shared location( vendor will place the files here) to Linux server( my Informatica will pick the files from this location). Files should be loaded in the order in which they were placed in windows box.(Based on... (1 Reply)
Discussion started by: Haari
1 Replies

6. Shell Programming and Scripting

Copying List of Files Under Different File Names ?!

Hi to all here , Excuse me for the lamer question but I need UNIX command for copying List of Files Under Different File Names ! for example I have this list: new_001.jpg new_002.jpg new_003.jpg ..... I want to copy all files that start with "new_" to the same directory but under... (7 Replies)
Discussion started by: boboweb_
7 Replies

7. Solaris

list of files in a txt file from sftp location

I want equivalent of ftp in sftp for listing of files into local machine from sftp location. ftp>ls -l list.txt the above creates a file list.txt in the local machine's current directory. sftp>ls -l list.txt it is giving Couldn't stat remote file: No such file or directory is there... (1 Reply)
Discussion started by: megh
1 Replies

8. Shell Programming and Scripting

copying a list of files from one machine to another with the same path

Hello All, I would like to download a list of files from a remote server to my local machine preserving the path i.e, create a similar directory tree. ex: files on the remote machine with path /remoteserver/005_S_0221/mpr/m1/I72128/dat/06A515T.dat... (3 Replies)
Discussion started by: avatar_007
3 Replies

9. UNIX for Dummies Questions & Answers

Copying a list of files from one 'server' to another?

I have two servers. I would like to copy some files from one directory on server A into the same directory on server B. Its not all the files in in the directory, just some of them. Is there a way to make a file list in a txt file and then somehow copy all the files in that list in one go to... (6 Replies)
Discussion started by: Sepia
6 Replies

10. UNIX for Dummies Questions & Answers

copying files from one location to another based on a list

I have a text list of about 3,000 file names (image files), which exist on a server and that I want to copy over to another location. I understand the Unix cp code, but what's the string to have it copy multiple files based on an external list? Many thanks! (4 Replies)
Discussion started by: rebornhonest
4 Replies
Login or Register to Ask a Question