Move several files into specific directories with a loop


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Move several files into specific directories with a loop
# 8  
Old 03-03-2020
I'm sorry, I realize I may be over-explaining.
my parent directory is: files2folders
inside the parent are 4 directories and 4 files:
Code:
01_webasset_100
02_webasset_200
03_webasset_300
04_webasset_400

1244081312562_01.jpg
1244081312562_02.jpg
1244081312562_03.jpg
1244081312562_04.jpg

I want to put each of those files into those folders using the file number suffixes and folder prefixes as "strings"?...

Thanks
Moderator's Comments:
Mod Comment
Do not ignore code tags or warnings to use them, thank you!

Last edited by Peasant; 03-03-2020 at 11:06 AM.. Reason: Added code tags.
# 9  
Old 03-03-2020
Start with a decent spec in the first place.


Try
Code:
cd files2folders
for FN in *.jpg
  do    NR="${FN%.*}"
        NR="${NR#*_}"
        echo mv "$FN"  "$NR"*
  done
mv 1244081312562_01.jpg 01_webasset_100
mv 1244081312562_02.jpg 02_webasset_200
mv 1244081312562_03.jpg 03_webasset_300
mv 1244081312562_04.jpg 04_webasset_400

Above may fail if you have directories with non-unique starting numbers so that "$NR"* evaluates to multiple dirs.
This User Gave Thanks to RudiC For This Post:
# 10  
Old 03-03-2020
Rudi,
Fantastic!
It works like a charm.
Would you mind explaining the code for my learning?

Thanks soooooo much!
# 11  
Old 03-03-2020
After changing directory to "files2folders", the for loop runs across all .jpg files. The "number suffixes" are extracted by means of two shell's "Parameter Expansion / Remove matching prefix (suffix) pattern" (man bash), then the mv command is constructed using the FN and NR variables, relying on the assumption that there will be only ONE matching directory.
This User Gave Thanks to RudiC For This Post:
# 12  
Old 03-03-2020
Hi
And if we assume that there are more files than directories
Code:
touch files2folders/1244081312563_04.jpg
cd files2folders
find *[1-4]00 -prune -type d -exec bash -c 'echo mv *_${0/%_*/.jpg} $0' {} \;

mv 1244081312562_01.jpg 01_webasset_100
mv 1244081312562_02.jpg 02_webasset_200
mv 1244081312562_03.jpg 03_webasset_300
mv 1244081312562_04.jpg 1244081312563_04.jpg 04_webasset_400

or
Bring them back
Code:
find *[1-4]00 -prune -type d -exec bash -c 'mv $0/*.jpg ./' {} \;

Code:
#!/bin/bash
cd files2folders
for FN in *[1-4]00; do
        echo mv *_${FN/%_*}.jpg $FN
done

mv 1244081312562_01.jpg 01_webasset_100
mv 1244081312562_02.jpg 02_webasset_200
mv 1244081312562_03.jpg 03_webasset_300
mv 1244081312562_04.jpg 1244081312563_04.jpg 04_webasset_400

# 13  
Old 03-03-2020
Quote:
Originally Posted by nezabudka
Hi
And if we assume that there are more files than directories
Code:
touch files2folders/1244081312563_04.jpg
cd files2folders
find *[1-4]00 -prune -type d -exec bash -c 'echo mv *_${0/%_*/.jpg} $0' {} \;

mv 1244081312562_01.jpg 01_webasset_100
mv 1244081312562_02.jpg 02_webasset_200
mv 1244081312562_03.jpg 03_webasset_300
mv 1244081312562_04.jpg 1244081312563_04.jpg 04_webasset_400

or
Bring them back
Code:
find *[1-4]00 -prune -type d -exec bash -c 'mv $0/*.jpg ./' {} \;

Code:
#!/bin/bash
cd files2folders
for FN in *[1-4]00; do
        echo mv *_${FN/%_*}.jpg $FN
done

mv 1244081312562_01.jpg 01_webasset_100
mv 1244081312562_02.jpg 02_webasset_200
mv 1244081312562_03.jpg 03_webasset_300
mv 1244081312562_04.jpg 1244081312563_04.jpg 04_webasset_400

Thank you nezabudka,
I can't wait to try that.
Question, what if there is a letter code preceding the suffix on the filename like filename_wa01.jpg? would i ruin the code if i added the "wa" to the string? Sorry i am newbie...
# 14  
Old 03-04-2020
Quote:
Originally Posted by SonnyClark
Question, what if there is a letter code preceding the suffix on the filename like filename_wa01.jpg?
Code:
touch filename_wa01.jpg

patterns should change to the following
Code:
find *[1-4]00 -prune -type d -exec bash -c 'echo mv *[!0-9]${0/%_*/.jpg} $0' {} \;

and
Code:
for FN in *[1-4]00; do
	echo mv *[!0-9]${FN/%_*}.jpg $FN
done

Code:
mv 1244081312562_01.jpg filename_wa01.jpg 01_webasset_100
mv 1244081312562_02.jpg 02_webasset_200
mv 1244081312562_03.jpg 03_webasset_300
mv 1244081312562_04.jpg 1244081312563_04.jpg 04_webasset_400


Last edited by nezabudka; 03-04-2020 at 03:24 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 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. UNIX for Dummies Questions & Answers

Move multipe files to corresponding directories

Hi, In a parent directory there are several files in the form IDENTIFIER1x IDENTIFIER1.yyy IDENTIFIER1_Z, etc IDENTIFIER2x IDENTIFIER2.yyy IDENTIFIER2_Z, etc IDENTIFIER3x IDENTIFIER3.yyy, IDENTIFIER3_Z, etcIn the same parent directory there are corresponding directories named... (7 Replies)
Discussion started by: spirospap
7 Replies

3. Shell Programming and Scripting

Recursively move directories along with files/specific files

I would like to transfer all files ending with .log from /tmp and to /tmp/archive (using find ) The directory structure looks like :- /tmp a.log b.log c.log /abcd d.log e.log When I tried the following command , it movies all the log files... (8 Replies)
Discussion started by: frintocf
8 Replies

4. OS X (Apple)

Batch file to move video files and retain sub-directories

I have just purchased my first ever Apple computer - and am therefore new to UNIX also. I would like to create a simple "batch file" (apologies if this is the wrong terminology) to do the following: When I plug my camera into the MAC it automatically downloads photos and videos into a new... (1 Reply)
Discussion started by: mm0mss
1 Replies

5. Shell Programming and Scripting

Loop to move files in different directories

Hi, I have various log files in different paths. e.g. a/b/c/d/e/server.log a/b/c/d/f/server.log a/b/c/d/g/server.log a/b/c/h/e/server.log a/b/c/h/f/server.log a/b/c/h/g/server.log a/b/c/i/e/server.log a/b/c/i/e/server.log a/b/c/i/e/server.log and above these have an archive folder... (6 Replies)
Discussion started by: acc01
6 Replies

6. Shell Programming and Scripting

Help with command to Move files by X number to seperate directories

Hello, I need help finding a script that will allow me to move files from one directory to another directory 10k files at a time. I have a directory that has 100 K files in it. I need to have those 100k files broken apart to separate directories each with 10k files in them. Here is the... (8 Replies)
Discussion started by: Geo_Bean
8 Replies

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

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

9. Shell Programming and Scripting

grep'ing for specific directories, and using the output to move files

Hello, this is probably another really simple tasks for most of you gurus, however I am trying to make a script which takes an input, greps a specific file for that input, prints back to screen the results (which are directory names) and then be able to use the directory names to move files.... (1 Reply)
Discussion started by: JayC89
1 Replies
Login or Register to Ask a Question