Script to copy certain info from several directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to copy certain info from several directories
# 1  
Old 08-31-2012
Script to copy certain info from several directories

Hi,
I am writing a script to copy certain file name in txt file [1].
It is working fine if I provide a single directory name (for example "/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/" ) where those specific files are present ending with *root [2].
But I want to modify this script to check the directories there [3] look for all the directory for the files ending with *root and then copy them to the $FileName.

Thanks,
Pooja

[1]
Code:
#!/bin/bash                                                                                                                                                            
date
PATHNAME=/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/

FileName=DataFileName
 ls -ltr $PATHNAME grep root | awk '{print "$PATHNAME"$9}' > "$FileName"

[2]
Code:
[pooja04@cmslpc09 tcsh]$ ls /eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/
dataJv1  dataMv2  datav1  datav4
[pooja04@cmslpc09 tcsh]$

[3]
Code:
[pooja04@cmslpc09 tcsh]$ l /eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/
-rw-r--r-- 1 pooja04 us_cms 128761223 Jul  1 20:48 vgtree_276_4_8Dj.root
-rw-r--r-- 1 pooja04 us_cms 153347935 Jul  1 21:36 vgtree_183_2_aZm.root
-rw-r--r-- 1 pooja04 us_cms 139629983 Jul  1 22:01 vgtree_76_5_I3U.root
-rw-r--r-- 1 pooja04 us_cms 128422302 Jul  1 22:40 vgtree_95_4_KXv.root
-rw-r--r-- 1 pooja04 us_cms 139629983 Jul  2 02:36 vgtree_76_6_wEd.root

# 2  
Old 08-31-2012
There's no point doing ls -l if all you're going to do is extract the filename from it. The ordinary listing will do.

Code:
find /eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/ -name '*root' | while read FILE
do
        echo mv "$FILE" /path/to/dest
done

# 3  
Old 08-31-2012
Hi,
Sorry I forgot to mention that I will need the following information in a text file,
Code:
/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/vgtree_434_1_AD5.root
/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/vgtree_253_1_Cie.root
/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/vgtree_252_1_AAy.root
/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/vgtree_253_1_bKi.root
/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/vgtree_107_1_b56.root

And given this, I need to do the "ls -ltr ".

Thanks
Pooja

Quote:
Originally Posted by Corona688
There's no point doing ls -l if all you're going to do is extract the filename from it. The ordinary listing will do.

Code:
find /eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/ -name '*root' | while read FILE
do
        echo mv "$FILE" /path/to/dest
done

# 4  
Old 08-31-2012
Quote:
Originally Posted by nrjrasaxena
...And given this, I need to do the "ls -ltr ".
No you don't. You need -t for time, and -r for reverse. All -l does is add columns that you instantly throw away.

-1, as in one, not ell, might make sense to force single-column output, but you get it anyway whenever ls writes to a non-terminal.

Since you need it sorted by time:

Code:
find /eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/ -name '*.root' | xargs ls -1tr | tee listfile | while read LINE
do
        echo mv "$LINE" /path/to/dest
done

This may fail if there's too many hundreds of files, since they won't all be able to fit in one ls commandline.
# 5  
Old 08-31-2012
Thanks for the suggestion, it could copy one directory content and 10% of the second directory but not all directory info.

And yeah, my files are sometimes 1000 in one directory.

pooja
# 6  
Old 08-31-2012
Hmm, this may or may not be difficult. Do you have stat? It can print epoch times, which you feed into a numeric sort, and then get rid of with awk once they've served their purpose...

Code:
find . -name '*.root' | xargs stat -c "%Z %n" | sort -r -n | awk '{ print $2 }' > listfile

while read LINE
do
        echo mv "$LINE" /path/to/dest
done < listfile


Last edited by Corona688; 08-31-2012 at 06:39 PM..
# 7  
Old 08-31-2012
Quote:
Originally Posted by Corona688
Hmm, this may or may not be difficult. Do you have stat?
Yes, I have.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and Copy Directories ONLY

I am trying to copy only the date specific folders/directories using the following command. However, the following copy command is also copying files from the root folder (OriginalFolder). find /OriginalFolder/ -type -d \{ -mtime 1 -o -mtime 2 \ } -exec cp -R {} /CopyTo/'hostname'__CopyTo/ \;... (2 Replies)
Discussion started by: apacheLinux
2 Replies

2. Shell Programming and Scripting

Copy directories in make file

LD:=C:/WindRiver/diab/5.9.3.0/WIN32/bin/dld.exe CFILES:=$(wildcard *.c) OBJFILES:=$(subst .c,.o, $(CFILES)) OBJ_PATH:=$(PRJ_PATH)/out/ ADDOBJFILES := $(addprefix $(OBJ_PATH),$(OBJFILES)) FILES:=C:/EB/tresos/workspace/Test_Spi/output/src copyfiles: cp ... (3 Replies)
Discussion started by: ushacy
3 Replies

3. Shell Programming and Scripting

Shell script to copy particular file from directories recursively

I have directory path in which there are several sub directories. In all these sub dir there will be one env.cnf file. I want to copy this env.cnf file from each sub dir's and place them in destination path by creating same filename as sub dir_env.cnf. After copying env.cnf files from source... (4 Replies)
Discussion started by: Optimus81
4 Replies

4. Shell Programming and Scripting

Compare & Copy Directories : Bash Script Help

Beginner/Intermediate shell; comfortable in the command line. I have been looking for a solution to a backup problem. I need to compare Directory 1 to Directory 2 and copy all modified or new files/directories from Directory 1 to Directory 3. I need the directory and file structure to be... (4 Replies)
Discussion started by: Rod
4 Replies

5. Shell Programming and Scripting

How to extract the day of the year and use that info to copy a file remotely

Hello, Thank you in advance for helping a newbie who is having great trouble with this simple task. I'm allowed to copy one file remotely each night due to bandwidth restrictions. A new file gets generated once a day, and I need to copy the previous day's file. Here is what I'd like to do:... (1 Reply)
Discussion started by: tmozdzen
1 Replies

6. UNIX for Dummies Questions & Answers

how can i copy those files into other directories have the same name

how can i copy those files into other directories have the same name but different in the end i have files in directory called test: 10_10_asdadfsdfad.txt 10_10_11_asdawqefwkjasd.txt 10_10_11_12_asdafjjhoqwd.txt i want to put them in exist directory thart i have on my system i have... (1 Reply)
Discussion started by: t17
1 Replies

7. Shell Programming and Scripting

Need help to copy the lease info block

Hi, am having a lease file which contains lots os lease info. In that i have to copy the whole block(shown below) by identifying the mac and change the IP according to the i/p. I have used like sed s/${ip_addr}/$ch_ip/g $temp_file | grep -B5 "${mac}" >> $persistent_file sed... (2 Replies)
Discussion started by: SMNK
2 Replies

8. UNIX for Dummies Questions & Answers

Copy file into directories and sub-directories

Hello- I need to copy a file into multiple directories, and each directory's sub-directories (of which there are 5) Currently, the parent directory is set up like this: dir1 sub-dir1 sub-dir2 sub-dir3 sub-dir4 sub-dir5 dir2 sub-dir1 sub-dir2 sub-dir3 ... (1 Reply)
Discussion started by: penlok
1 Replies

9. UNIX for Dummies Questions & Answers

Copy selected Directories

I wonder if someone would help me a little here. I have a directory (folder on a mac) with about 100 subfolders and sub-subfolders and files there in. All sub directories have the same name structure, "AAA Name". Like this: ISP CompanyName ITS CompanyName KEL CompanyName KRA CompanyName... (2 Replies)
Discussion started by: sigurarm
2 Replies

10. UNIX for Dummies Questions & Answers

Copy and keep owner info

I need to copy all fiels and directories to another locathon and keep the owner info and rights. I tried cp -r - p * /newdir No good. (5 Replies)
Discussion started by: aarono
5 Replies
Login or Register to Ask a Question