create more than 100 directories and copy files into them


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting create more than 100 directories and copy files into them
# 1  
Old 10-26-2011
create more than 100 directories and copy files into them

Hi,

I have several files containing experiment measurements per hour (hour_1.txt has measurements for first hour, etc..etc..). I have 720 of these files (i.e. up to hour_720.txt) and i want to create 720 directories and in every one of them i want to copy its associative file (e.g. hour1/hour_1.txt hour2/hour_2.txt etc...etc..)

i believe a combination of mkdir and cp would be hard to use for this case. Do you have any suggestions?

Thanks in advance
# 2  
Old 10-26-2011
The one below will create a directory with name like: hour_1, hour_2, hour_N

Code:
baseToDir="./"
find <Path> -maxdepth 1 -name "hour_*" -type f | \
while read fname
do
	fileBaseName=`basename "${fname}"`
	dirName=`echo "${fileBaseName}" | cut -f1 -d"."`
	
	nDirName="${baseToDir}/${dirName}"
	mkdir -p "${nDirName}"
	cp "${fname}" "${nDirName}"
done

# 3  
Old 10-26-2011
Hi felipe.vinturin and thanks for the quick reply

I've tried your code (i inserted the path in <Path> which is /home/dataset/) and i'm getting the error:

find `1': No such file or directory

(i know it might be a stupid question but is my first time using find and a .sh script)
# 4  
Old 10-26-2011
Please, execute the command below:
Code:
find /home/dataset -maxdepth 1 -name "hour_*.txt" -type f

I think it is an environment issue, because some "find" command distributions does not accept the: "-maxdepth" option.

If you want you can remove it: "-maxdepth 1", but it will try to find all files that match the pattern: "hour_*.txt" under the directory ("/home/dataset"), I mean, it will include subdirectories.
This User Gave Thanks to felipe.vinturin For This Post:
# 5  
Old 10-26-2011
thank you felipe.vintourin! it worked perfectly well now! Cheers!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy of "How to create a long list of directories with mkdir?"

To bakunin and corona688: My result when text in file is ms_ww_546 ms_rrL_99999 ms_nnn_67_756675 is https://www.unix.com/C:\Users\Fejoz\Desktop\ttt.jpg I hope you can see the picture. There is like a "whitespace character" after 2 of the 3 created directories. ---------- Post... (0 Replies)
Discussion started by: setub
0 Replies

2. Shell Programming and Scripting

Copy files in respective directories

Hi Guys, I need to copy the files to respective directories based on name of the file. My script is something like below con=$1 for file in `cat $con` do file_tmp=$(ls -t1 $path| grep -i $file | head -n 1) echo $file_tmp if then cp $path$file_tmp $DIR/ap if then... (16 Replies)
Discussion started by: Master_Mind
16 Replies

3. Shell Programming and Scripting

Copy Directories with Files from One Server to Other using sftp

Hi, I have a requirement where I have to connect to another server and copy directories from one server to another Directories on the Source server look like below (YYYY-MM-DD- 1 to 23) drwxr-xr-x 2 test_user dmfmart 422 Sep 1 23:45 2014-09-01-18drwxr-xr-x 2 test_user dmfmart ... (1 Reply)
Discussion started by: arunkesi
1 Replies

4. Shell Programming and Scripting

Recursive search for files and copy to new directories

So I have extremely limited experience with shell scripting and I was hoping someone could point out a few commands I need to use in order to pull this off with a shell script like BASH or whatnot (this is on OS X). I need to search out for filenames with account numbers in the name itself... (3 Replies)
Discussion started by: flyawaymike
3 Replies

5. Shell Programming and Scripting

Copy files by automatically creating directories

I am in a situation where I have some hundreds of thousands of files in a directory (name of directory: big_directory). Now, working on this directory is extremely slow. I now plan to do this: Copy 1000 files in each of the directories by creating the directories automatically. All files have... (1 Reply)
Discussion started by: shoaibjameel123
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. UNIX for Dummies Questions & Answers

Compare two directories and copy differing files

Hello there, I'm a total noob to shell scripting. :) What I want to do is compare the contents of Folder A and Folder B, and copy any files in Folder A that do not exist in Folder B over to Folder B. I have gotten so far as: diff -rq folderA folderB which returns the names of the files,... (3 Replies)
Discussion started by: raaaaaa
3 Replies

8. UNIX for Dummies Questions & Answers

Copy Directories excluding files

Hi guys, I want to copy folder and sub folders only. I don't want the files. If i use cp -r command it will copy entirely with files. Could any one suggest me. Thanks in advance (1 Reply)
Discussion started by: karthik82
1 Replies

9. Shell Programming and Scripting

Copy unique files between two directories in Solaris

Hi, My requirement is to have a shell script: SOURCE_DIR=/usr/logs/restore/ --> Contains multiple subdirectories TARGET_DIR=/usr/data/logs --> Existing folders which contain similar subdirectories I want to copy only newer or uniquie files which are present in $SOURCE_DIR and its... (2 Replies)
Discussion started by: sujoy101
2 Replies

10. UNIX for Dummies Questions & Answers

copy multiple files in different directories

I have a report file that is generated every day by a scheduled process. Each day the file is written to a directory named .../blah_blah/Y07/MM-DD-YY/reportmmddyy.tab I want to copy all of this reports to a separate directory without having to do it one by one. However, if I try cp... (3 Replies)
Discussion started by: ken2834
3 Replies
Login or Register to Ask a Question