Visit Our UNIX and Linux User Community


Bash script - move files to matching folders


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script - move files to matching folders
# 1  
Old 04-06-2020
Bash script - move files to matching folders

Hi,
I apologize in advance for the long explanation. I am fairly new at coding.
Any suggestion would be greatly appreciated.

I have the intention to organize the folders from
Code:
parent
|-- out
	|-- 1.Amber
	|   |-- seed-1_clean.e1.log
	|   |-- seed-1_clean.e2.log
	|   |-- seed-1_test.idx
	|   |-- seed-2_clean.e1.log
	|   |-- seed-2_clean.e2.log
	|   `-- seed-2_test.idx
	|-- 2.Dragon
	|   |-- seed-1.ppt
	|   `-- seed-2.ppt
	|-- 3.Chris
	|   |-- seed-1.tar.gz
	    `-- seed-2.tar.gz

to something like this
Code:
parent
|-- seed-1
	|-- 1.Amber
	|   |-- seed-1_clean.e1.log
	|   |-- seed-1_clean.e2.log
	|   `-- seed-1_test.idx
	|-- 2.Dragon
	|   `-- seed-1.ppt
	|-- 3.Chris
	|   `-- seed-1.tar.gz
|-- seed-2
	|-- 1.Amber
	|   |-- seed-2_clean.e1.log
	|   |-- seed-2_clean.e2.log
	|   `-- seed-2_test.idx
	|-- 2.Dragon
	|   `-- seed-2.ppt
	|-- 3.Chris
 	    `-- seed-2.tar.gz

I got it to work with the following code
Code:
#!/usr/bin/env bash

aa='1.Amber'
bb='2.Dragon'
cc='3.Chris'

for s in out/"$aa"/*_c*; do
	sn=${s%%_*}
	sn=${sn##*/}
	echo "$sn"
	mkdir -p "${sn}"/"$aa"
	mkdir -p "${sn}"/"$bb"
	mkdir -p "${sn}"/"$cc"
	
	for i in out/"$aa"/*; do
		if grep -q "$sn" <<< "$i"; then
			mv "$i" "$sn"/"$aa"/.
	for i2 in out/"$bb"/*; do
		if grep -q "$sn" <<< "$i2"; then
			mv "$i2" "$sn"/"$bb"/.
	for i3 in out/"$cc"/*; do
		if grep -q "$sn" <<< "$i3"; then
			mv "$i3" "$sn"/"$cc"/.
		fi
	done
		fi
	done
		fi
	done
done

However, if I have more files to organize, it would become very long and tedious.
Is there a way that I could shorten this?

Also, for the echo "$sn" part, I got repeated output of seed-1 and seed-2. I'm guessing it's because I have 2 similar files (_clean.e1.log, _clean.e2.log). How do I make it so the same file name only echo once?

Please help
Thanks so much

--- Post updated at 01:43 PM ---

In addition, I've tried
Code:
for s in out/"$aa"/*_c*; do
	sn=${s%%_*}
	sn=${sn##*/}
	echo "$sn"
	mkdir -p "${sn}"/{"$aa", "$bb", "$cc"}
done

It came out like this
Code:
parent
|-- 2.Dragon,
|-- 3.Chris}
|-- seed-1
	|-- {1.Amber,
|-- seed-2
	|-- {1.Amber,

This User Gave Thanks to aly For This Post:
# 2  
Old 04-06-2020
Thanks for the clean, well formatted, easy to read request!


Here's a fragment with gnu awk, which may help you....

My directory structure:

Code:
cd $basedir
tree -n
.
├── 1.Amber
│   ├── seed-1.txt
│   ├── seed-2.txt
│   └── seed-3.txt
├── 2.Chris
│   ├── seed-1.ppt
│   └── seed-2.ppt
└── 3.Dragon
    ├── seed-1.tgz
    ├── seed-2.tgz
    └── seed-3.tgz

Code that gets you the directory and the filename into variables:

Code:
while read directory file ; do
   echo "$directory" "$file"
done < <(find | gawk 'match($0,/\/([^\/]+)\/(seed-.*)/,matches){print matches[1],matches[2]}')


# output


2.Chris seed-2.ppt
2.Chris seed-1.ppt
1.Amber seed-2.txt
1.Amber seed-3.txt
1.Amber seed-1.txt
3.Dragon seed-1.tgz
3.Dragon seed-3.tgz
3.Dragon seed-2.tgz


Last edited by the-architect; 04-06-2020 at 05:35 PM..
# 3  
Old 04-07-2020
You may want to try this:
Code:
cd out
ls -1 */* | 
while IFS="/_" read _ PRFX _
  do    echo "${PRFX%%.*}"
  done | 
sort -u |
while   read DN
  do    echo mkdir "$DN"
        ls */"$DN"* |
        while   read FN
          do    echo cp "$FN" "$DN/$FN"
          done
  done
mkdir seed-1
cp 1.Amber/seed-1_clean.e1.log seed-1/1.Amber/seed-1_clean.e1.log
cp 1.Amber/seed-1_clean.e2.log seed-1/1.Amber/seed-1_clean.e2.log
cp 1.Amber/seed-1_test.idx seed-1/1.Amber/seed-1_test.idx
cp 2.Dragon/seed-1.ppt seed-1/2.Dragon/seed-1.ppt
cp 3.Chris/seed-1.tar.gz seed-1/3.Chris/seed-1.tar.gz
mkdir seed-2
cp 1.Amber/seed-2_clean.e1.log seed-2/1.Amber/seed-2_clean.e1.log
cp 1.Amber/seed-2_clean.e2.log seed-2/1.Amber/seed-2_clean.e2.log
cp 1.Amber/seed-2_test.idx seed-2/1.Amber/seed-2_test.idx
cp 2.Dragon/seed-2.ppt seed-2/2.Dragon/seed-2.ppt
cp 3.Chris/seed-2.tar.gz seed-2/3.Chris/seed-2.tar.gz

Remove the last two echoes if happy with what you see.

Previous Thread | Next Thread
Test Your Knowledge in Computers #910
Difficulty: Easy
In C, you can declare a pointer as follows: data_type * pointer_variable_name;
True or False?

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

How can i move folders and its content if folder is older than 1,5 days and keep subdirs in bash?

Hello all, do you know any way i can i move folders and its content if folder is older than 1,5 days in bash? I tried: find /home/xyz/DATA/* -type d -ctime +1.5 -exec mv "{}" /home/xyz/move_data_here/ \;All i got was that Files from DATA /home/xyz/DATA/* ended messed up in... (1 Reply)
Discussion started by: ZerO13
1 Replies

3. Shell Programming and Scripting

Bash to move specific files from folders in find file

I have a directory /home/cmccabe/nfs/exportedReports that contains multiple folders in it. The find writes the name of each folder to out.txt. A new directory is then created in a new location /home/cmccabe/Desktop/NGS/API, named with the date. What I am trying to do, unsuccessfully at the moment,... (7 Replies)
Discussion started by: cmccabe
7 Replies

4. Shell Programming and Scripting

Script to move files in multiple folders

Hello all, I would appreciate any help to write a script. I have folder A which contains over 30 thousands xml files, I would like create multiple folders and move those files (500 in each folders). Thank you (1 Reply)
Discussion started by: mmsiddig
1 Replies

5. Shell Programming and Scripting

bash script for testing existence of files/folders and creating if neither exist

Hi, I am new to shell-scripting, and doing a lot of reading. I am having some trouble getting started with a simple testing of scripting. I have been experimenting with if, loops, for, test, etc., but still unsure. I seem to have the hang of it when it comes to creating a single file or... (6 Replies)
Discussion started by: me2
6 Replies

6. Shell Programming and Scripting

Help with auto-detect new files/folders then zip and move script

Hello, I need a simple script to Auto-detect new files and folders in the directory. And then I need to zip the new files and bzip2 new folders and move them out of that folder where I am detecting changes to the other folder. Remember, I need simple one. If anyone could do it fast, I may... (1 Reply)
Discussion started by: juzt1s
1 Replies

7. Shell Programming and Scripting

Move files to Folders

Hi Friends, Below is my requirement and i am not clear how to approach this issue in unix programming. I have a folder with 2500 files. The files are in below format. 1234_name1.txt 1234_name123.txt 4567_name1.txt 4567_name123.txt and i need a program which will read each file from this... (5 Replies)
Discussion started by: diva_thilak
5 Replies

8. Shell Programming and Scripting

Shell script to move files to 3 different folders

Hi guys: I've got this problem, I want to move a bunch of files to 3 different folders, without any specific order, and I'm trying to automatize it with a shell script. I'm a newbie at shell scripting so this is my first try: #!/bin/bash COUNTER=`ls -1 | wc -l` while do ARRAY=(... (11 Replies)
Discussion started by: wretchedmike
11 Replies

9. UNIX for Dummies Questions & Answers

Move folders containing certain files

Hello, How can I move just the folders that contains files modified n days ago? Source tree: |-- SourceFolder | |-- Subfolder1 | | |-- file1.dat | | `-- file2.dat | |-- Subfolder2 | | |-- filea.dat | | `-- fileb.dat Destination tree: |-- ... (3 Replies)
Discussion started by: xavix
3 Replies

10. Shell Programming and Scripting

moving multiple folders/files in subversion using bash script

Hi, I'm new here an dlearning a lot from this forum. i didnt find any solution for this in the forum. I have already checked in folders in subversion named HTT01,... HTT21.. and have files in each folder like below: HTT01/HTT01_00000.hex HTT01/HTT01_00000_fb_result.hex... (2 Replies)
Discussion started by: ravishan21
2 Replies

Featured Tech Videos