Copy files by automatically creating directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy files by automatically creating directories
# 1  
Old 08-31-2011
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 .dat extension in the directory and have names like 1.dat, 2.dat, 3.dat until the last file number

This is what I intend to do:

1. Automatically create a directory "dir1" and copy first 1000 ".dat" files (present in big_directory) in it.

2. Automatically create a directory "dir2" and then copy the next 1000 ".dat" files (present in big_directory) in it.

3. Automatically create a directory "dir3" and then copy the next 1000 ".dat" files (present in big_directory) in it.

4. and so on until last file

I am using Linux with BASH. I know how to copy first 1000 files but not sure how to create directories automatically using a counter and copy them there.

Code:
cp `ls -ltr | grep ^- | head -1000 | tr -s " " | cut -d " " -f9` destination

# 2  
Old 08-31-2011
This should do:

Code:
#!/bin/bash

count=`find -name "*.dat" | wc -l`
iterations=$(($count/1000))

for j in $(seq $iterations); do
        dir="dir$j"
        mkdir $dir
        mv $j???.dat $dir
done

I used mv instead of cp... Your call, but mv will be faster and with cp You will double this hundreds of thousands of files...

PS.
Oh, and add following line at the begining, before the loop, to copy the first 1000 files:
Code:
mkdir dir0 && mv ?.dat ??.dat ???.dat dir0/

This User Gave Thanks to sulti 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

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

2. Shell Programming and Scripting

Need BASH Script Help to Move Files While Creating Directories

I've got this script to loop through all folders and move files that are more than 2 years old. I'm using the install command because it creates the necessary directories on the destination path and then I remove the source. I'd like to change the script to use the mv command since it is much... (4 Replies)
Discussion started by: consultant
4 Replies

3. Shell Programming and Scripting

Archiving and moving files into directories, creating directories, etc.

how can i move "dataName".sql.gz into a folder called 'database' and then move "$fileName".tar.gz * .htaccess into a folder called 'www' with the entire gzipped file being "$fileName".tar.gz? Is this doable or overly complex. so mydemo--2015-03-23-1500.tar.gz > database -... (5 Replies)
Discussion started by: wyclef
5 Replies

4. Shell Programming and Scripting

Creating timestamped directories/files in expect

Hi, I am looking for the syntax to provide a timestamped log_file in expect. Basically I want the equivalent of: /outputs/`date +%d%m%y`/session`date +%H%M` But scripted in expect so it can be handed over to the logfile function. I have tried playing around with the timestamp... (1 Reply)
Discussion started by: krypton
1 Replies

5. Solaris

How to extract files from a tar file without creating the directories?

Hello all. I have a tar file that contains a number of files that are stored in different directories. If I extract this tar file with -xvf , the directories get created. Is there a way to extract all of the files into one directory without creating the directories stored in the tar file. (9 Replies)
Discussion started by: gkb
9 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

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

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

Bash and Awk for creating directories and moving files

I have a security system that FTPs the camera files to my machine, however I want to sort the pictures (taken every 30s) into directories by hour. Every picture uses the following file format. yymmddhhmmsstt.jpg (where tt is the milliseconds) I am thinking the for loop is best for file... (11 Replies)
Discussion started by: Kiint
11 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