Copying files to directories based on first 6 character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying files to directories based on first 6 character
# 1  
Old 06-12-2015
Copying files to directories based on first 6 character

guys, i did create a script but its too long, though it function the same.

Code:
[root@log Sophos]# cat nightlyscan.sh
#!/usr/ksh

deyt=`date +"%Y-%m-%d"`

for i in `ls -lrt|grep $deyt|awk '{print $9}'`
do
cp -f $i /S1/Sophos/logger/

done
[root@log Sophos]#

but i did not paste it all.

this is the desired.

Code:
if you have 4 directories
DATA11
DATA22
DATA33
DATA43


And you have files like
DATA11.txt
DATA11.tgz
DATA22.xls
DATA33.as


Output
the directory DATA11 shoud have files of DATA11.txt and DATA11.tgz
DATA22 directory should have file of DATA22.xls and so on.

# 2  
Old 06-12-2015
Why didn't you paste it all? If we can't see what you're doing, we're wasting our time trying to improve it.

The code snippet you have shown us seems to be doing something very different from what you said you want this script to do.

Show us what you have done!
# 3  
Old 06-12-2015
Code:
#!/usr/ksh

deyt=`date +"%Y-%m-%d"`

for i in `ls -lrt /NAS1/Sophos/  |grep $deyt|awk '{print $9}'`
do
cp -f /NAS1/Sophos/$i /NAS1/Sophos/logger/
done



for dir in `ls -d /NAS1/Sophos/logger/*/ | sed 's/.$//'`
do
rm -f $dir/*.tar
mv -f $dir*tgz* $dir/
gunzip $dir/*.tgz
done


Last edited by Don Cragun; 06-12-2015 at 05:30 PM.. Reason: Remove duplicate post.
# 4  
Old 06-12-2015
I'm still not sure what you request, and I still don't see any difficulties/problems in the snippets provided. If shoving files into aadequate directory is what you want, try e.g.
Code:
for FN in DATA* ; do mkdir -p ${FN%.*};  mv $FN ${FN%.*}; done

# 5  
Old 06-13-2015
ill try that on monday.. but i can gunzip files,based onmy directorues.. but i cant tar....any idea?

i cant tar xvf
# 6  
Old 06-13-2015
Quote:
Originally Posted by kenshinhimura
ill try that on monday.. but i can gunzip files,based onmy directorues.. but i cant tar....any idea?

i can't tar xvf
I'm lost. I don't see any reference to tar xvf in this thread other than you saying you can't use it. If you can't use tar xvf and no one has suggested that you use tar xvf, why is it important for us to know that you can't use tar xvf?
# 7  
Old 06-15-2015
Hi Don,
i removed the tar xvf line, but the point is.
i cant do this

for i in `In multiple directories`
tar xvf /tar/*.tar
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete Directories and its files that begin with the same character string

I wrote a shell script program that supposed to delete any directories and its files that begin with the same character string . The program is designed to delete directories and its files that are one day old or less and only the directories that begin with the same character string. The problem... (1 Reply)
Discussion started by: dellanicholson
1 Replies

2. Shell Programming and Scripting

Bash: copying lines with specific character to files with same name as copied line.

I am trying to make my script as simple as a possible but, I am not sure if the way I am approaching is necessarily the most efficient or effective it can be. What I am mainly trying to fix is a for loop to remove a string from the specified files and within this loop I am trying to copy the lines... (2 Replies)
Discussion started by: Allie_gastrator
2 Replies

3. Shell Programming and Scripting

Copying data from files to directories

I have the following that I'd like to do: 1. I have split a file into separate files that I placed into the /tmp directory. These files are named F1 F2 F3 F4. 2. In addition, I have several directories which are alphabetized as dira dirb dirc dird. 3. I'd like to be able to copy F1 F2 F3 F4... (2 Replies)
Discussion started by: newbie2010
2 Replies

4. Shell Programming and Scripting

Copying files based on a pattern

Hi All, I need to find and list the last 5 days files in that exact name with "MIM" and copy to another directory. please help me in this there is around 30000 files Thanks Murali (7 Replies)
Discussion started by: 969murali@gmail
7 Replies

5. Shell Programming and Scripting

Copying a files from a filter list and creating their associated parent directories

Hello all, I'm trying to copy all files within a specified directory to another location based on a find filter of mtime -1 (Solaris OS). The issue that I'm having is that in the destination directory, I want to retain the source directory structure while copying over only the files that have... (4 Replies)
Discussion started by: hunter55
4 Replies

6. Shell Programming and Scripting

want to move files in a dir into different directories based on the filename

I want to move the files in a dir to different dirs based on their file names. Ex: i have 4 different files with name - CTS_NONE_10476031_MRL_PFT20081215a.txt CTS_NONE_10633009_MRL_PFT20091020a.txt CTS_NONE_10345673_MRL_PFT20081215a.txt CTS_NONE_10872456_MRL_PFT20091020a.txt and the 1st... (4 Replies)
Discussion started by: Sriranga
4 Replies

7. UNIX for Dummies Questions & Answers

want to move files in a dir into different directories based on the filename

I want to move the files in a dir to different dirs based on their file names. Ex: i have 4 different files with name - CTS_NONE_10476031_MRL_PFT20081215a.txt CTS_NONE_10633009_MRL_PFT20091020a.txt CTS_NONE_10345673_MRL_PFT20081215a.txt CTS_NONE_10872456_MRL_PFT20091020a.txt and the 1st... (2 Replies)
Discussion started by: Sriranga
2 Replies

8. Shell Programming and Scripting

Backup script: Copying and removing directories based on list

I am writing a simple backup script, but I cannot figure out how to remove directories that are found in a list. For example: DONT_COPY=" .adobe/ .bin/google-earth " tar -zcvf - * --exclude=$DONT_COPY | openssl des3 -salt -k $1 | dd of=$(hostname)-$(date +%Y%m%d).tbz > COPIED Note that... (4 Replies)
Discussion started by: dotancohen
4 Replies

9. 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

10. Shell Programming and Scripting

Rename files/directories based on their name

i have hundreds of directories that have to be renamed. the directory structure is fairly uniform which makes the scripting a little simpler. suppose i have many directories like this */*/*/*abc* (in other words i have similar directory names 3 dirs deep that all contain the pattern abc in... (8 Replies)
Discussion started by: quantumechanix
8 Replies
Login or Register to Ask a Question