Copying multiplies files into multiplies directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying multiplies files into multiplies directories
# 1  
Old 02-12-2011
Copying multiplies files into multiplies directories

Hello !

I have a file structure like this:

1_0/file1.tiff
1_1/file2.tiff
1_2/file3.tiff
1_3/file4.tiff
1_4/file5.tiff
1_5/file6.tiff
1_6/file7.tiff
2_0/file8.tiff
2_1/file9.tiff
2_2/file10.tiff
2_3/file11.tiff
etc.

There is only one file in one directory.
I'd like to have files from each folder starting from 1_ moved into folder 1, 2_ into 2 etc.
The final structure of directory would look like:
1/file1.tiff
1/file2.tiff
1/file3.tiff
1/file4.tiff
1/file5.tiff
1/file6.tiff
1/file7.tiff
2/file8.tiff
2/file9.tiff
2/file10.tiff
2/file11.tiff
etc.

I've tried with bash scripting, but without any success

Thank you in advance
# 2  
Old 02-12-2011
No usability warranty
Code:
#! /bin/bash

# range of directories to look into, change as needed
for d in {1,2}_{1..6} 
do
    dst=$(echo "$d" | cut -d"_" -f1) # name of destination directory
    [ -d "$dst" ] || mkdir "$dst"    # create new directory if it does not exist
    cp "$d"/* "$dst"  2> /dev/null   # copy content to new directory
done

Just a visual example
This User Gave Thanks to Aia For This Post:
# 3  
Old 02-13-2011
Thank You Aia !
This is quite nice.

Although, the problem is that the numeration of directories may be not contiguous:
Code:
$ ls | sort
33_0
33_1
33_2
33_3
33_4
33_5
33_6
39_0
39_1
39_2
39_3
39_4
39_5
39_6
41_0
41_1
41_2
41_3
41_4
41_5
41_6

Then obviously it creates additional empty folders.
I modified for loop like:
Code:
for d in {33..41}_{0..6}

It would be great that it looks for the directories that actually exists, without typing them into the script manually, each time.

I want also to move the files, and delete source directories, which i suppose will be like:
Code:
#! /bin/bash

# range of directories to look into, change as needed
for d in {33..43}_{0..6} 
do
    dst=$(echo "$d" | cut -d"_" -f1) # name of destination directory
    [ -d "$dst" ] || mkdir "$dst"    # create new directory if it does not exist
    mv "$d"/* "$dst"  2> /dev/null   # copy content to new directory
    rm -R "$d"
done

Or maybe it will be easier to delete additional empty folders by:
Code:
find -depth -type d -empty -exec rmdir {} \;

?
# 4  
Old 02-13-2011
Untested:
Code:
for dir in $(echo *_* | sed -e 's^_.*^^')
do
mkdir -p ${dir}
mv ${dir}_*/* ${dir}
done

BUG: directories whose names end in _ could pose a problem. I don't check for files with the same name either.

Hope this helps.

Andrew
# 5  
Old 02-13-2011
Thank You Andrew.
Unfortunately, your script copies only one directory set (0...6) and then stops
Code:
$ ls -R
.:
11    11_1  11_3  11_5  17_0  17_2  17_4  17_6
11_0  11_2  11_4  11_6  17_1  17_3  17_5  sortBack2

./11:
ZN_000204_sRGB_g2.tiff  ZN_234121_sRGB_g2.tiff  ZN_235652_sRGB_g2.tiff
ZN_233105_sRGB_g2.tiff  ZN_234630_sRGB_g2.tiff
ZN_233613_sRGB_g2.tiff  ZN_235140_sRGB_g2.tiff

./11_0:

./11_1:

./11_2:

./11_3:

./11_4:

./11_5:

./11_6:

./17_0:
ZN_032122_sRGB_g2.tiff

./17_1:
ZN_032714_sRGB_g2.tiff

./17_2:
ZN_033307_sRGB_g2.tiff

./17_3:
ZN_033901_sRGB_g2.tiff

./17_4:
ZN_034455_sRGB_g2.tiff

./17_5:
ZN_035051_sRGB_g2.tiff

./17_6:
ZN_035648_sRGB_g2.tiff

# 6  
Old 02-13-2011
If you can guarantee that every directory in . are part of the range, then you could

Code:
#! /bin/bash

# range of directories to look into, change as needed
for d in *                               # list everything under current directory
do
    if test -d "$d"; then                # if it's a directory
        dst=${d%%_*}                     # name of destination directory
        [ -d "$dst" ] || mkdir "$dst"    # create new directory if it does not exist
        cp "$d"/* "$dst"  2> /dev/null   # copy content to new directory
    fi
done


Last edited by Aia; 02-13-2011 at 04:36 PM.. Reason: Adjustment
# 7  
Old 02-13-2011
Yes, this is exactly what I was looking for!
Thank you one more time.

Code:
# range of directories to look into, change as needed
for d in *                               # list everything under current directory
do
    if test -d "$d"; then                # if it's a directory
        dst=${d%%_*}                     # name of destination directory
        [ -d "$dst" ] || mkdir "$dst"    # create new directory if it does not exist
        mv "$d"/* "$dst"  2> /dev/null   # move content to new directory
        rm -R "$d"                             # delete the source directory
    fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying files to directories based on first 6 character

guys, i did create a script but its too long, though it function the same. # 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 # but i did not paste it all. this is the desired. (9 Replies)
Discussion started by: kenshinhimura
9 Replies

2. UNIX for Dummies Questions & Answers

Copying Directories from one server to another

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-18 drwxr-xr-x 2 test_user dmfmart ... (3 Replies)
Discussion started by: arunkesi
3 Replies

3. Shell Programming and Scripting

Error check for copying growing directories

I have a simple script which copies directory from one place to another and deleting the source . I am facing a situation when new files gets added when the script has started running. Its resulting in data loss Please suggest a way to avoid data loss. I googled a lot but most are perl... (11 Replies)
Discussion started by: ningy
11 Replies

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

5. Shell Programming and Scripting

Copying all directories while ignoring certain filetypes

I want to write a script that copys over a complete folder including the dirs to another location. However in the process I want to ignore several filetypse that SHOULD NOT get copied over. I know Global Ignore is capable of make the copy command ignore one file type, however I don't know how... (8 Replies)
Discussion started by: pasc
8 Replies

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

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

8. UNIX for Dummies Questions & Answers

Using find -d and copying to the found directories

Hi again All :) After posting my first thread just a few eeks ago and having such a great response (Thank You once again :) ), I thought I'd perhaps ask the experts again. In short I'm trying to achieve a "find" and "copy" where the find needs to find directories: find -d -name outbox and... (6 Replies)
Discussion started by: Dean Rotherham
6 Replies

9. UNIX for Dummies Questions & Answers

copying to multiple directories using wildcard

Can we copy a file to multiple directories using a single command line , i tried with * didnt work for me cp /tmp/a.kool /tmp/folder/*/keys/ I am tryn to copy a.kool file to all keys folder in /tmp folder. is something i am missing ? (4 Replies)
Discussion started by: logic0
4 Replies

10. UNIX for Dummies Questions & Answers

Copying multiple directories at the same time using Unix

Another Unix question. How would I copy multiple directories at the same time? Right now I do: cp -r -f /directory1/ ../backup/directory1/ I do that for each directory one at a time. But there are multiple directories I'd like to copy. So instead of sitting there and doing one at a time, is... (9 Replies)
Discussion started by: JPigford
9 Replies
Login or Register to Ask a Question