Move multipe files to corresponding directories


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Move multipe files to corresponding directories
# 1  
Old 02-13-2016
Move multipe files to corresponding directories

Hi,

In a parent directory there are several files in the form
Code:
IDENTIFIER1x
IDENTIFIER1.yyy
IDENTIFIER1_Z, etc
IDENTIFIER2x
IDENTIFIER2.yyy
IDENTIFIER2_Z, etc
IDENTIFIER3x
IDENTIFIER3.yyy,
IDENTIFIER3_Z, etc

In the same parent directory there are corresponding directories named IDENTIFIER1, IDENTIFIER2, IDENTIFIER3,...etc

I created a text file (namesUC.txt) with the list of the names of empty directories.

What would be a simple script to read every line of namesUC.txt and execute move all files with IDENTIFIERi* to their corresponding directory /IDENTIFIERi

Thanks


#Sample data below
#First line with no extension is the directory and the rest are the matching files
-------------------
Code:
$ls -1
LC80140322015078LGN00
LC80140322015078LGN00.jpg
LC80140322015078LGN00.tar
LC80140322015078LGN00T
LC80140322015078LGN00T.hdr
LC80140322015078LGN00_B1.TIF
.................
LC80140322014219LGN00
LC80140322014219LGN00.jpg
LC80140322014219LGN00.tar
LC80140322014219LGN00T
LC80140322014219LGN00T.hdr
LC80140322014219LGN00_B1.TIF

..........................etc


#Directory names text file (namesUC.txt)
Code:
$cat namesUC.txt
LC80140322013152LGN00
LC80140322013232LGN00
LC80140322013312LGN00
LC80140322014043LGN00
LC80140322014059LGN00
...etc


Last edited by spirospap; 02-13-2016 at 10:38 PM.. Reason: Lots of code tags....
# 2  
Old 02-14-2016
Given all the files to be moved are at least one char longer than the directories, as in your sample, try
Code:
while read FN; do mv -v ${FN}?* $FN; done < namesUC.txt
‘LC80140322014219LGN00_B1.TIF’ -> ‘LC80140322014219LGN00/LC80140322014219LGN00_B1.TIF’
‘LC80140322014219LGN00.jpg’ -> ‘LC80140322014219LGN00/LC80140322014219LGN00.jpg’
‘LC80140322014219LGN00T’ -> ‘LC80140322014219LGN00/LC80140322014219LGN00T’
‘LC80140322014219LGN00.tar’ -> ‘LC80140322014219LGN00/LC80140322014219LGN00.tar’
‘LC80140322014219LGN00T.hdr’ -> ‘LC80140322014219LGN00/LC80140322014219LGN00T.hdr’
‘LC80140322015078LGN00_B1.TIF’ -> ‘LC80140322015078LGN00/LC80140322015078LGN00_B1.TIF’
‘LC80140322015078LGN00.jpg’ -> ‘LC80140322015078LGN00/LC80140322015078LGN00.jpg’
‘LC80140322015078LGN00T’ -> ‘LC80140322015078LGN00/LC80140322015078LGN00T’
‘LC80140322015078LGN00.tar’ -> ‘LC80140322015078LGN00/LC80140322015078LGN00.tar’
‘LC80140322015078LGN00T.hdr’ -> ‘LC80140322015078LGN00/LC80140322015078LGN00T.hdr’

This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-14-2016
Works perfectly

Thanks!
# 4  
Old 02-14-2016
If you don't want to go to the bother of creating namesUC.txt you could also let the script process all regular files in the directory with names that start with the same name as a directory in that same directory. For example, with a starting file hierarchy like:
Code:
total 40
123274998  0 drwxr-xr-x   13 dwc  staff    442 Feb 14 12:05 .
   545528  0 drwxr-xr-x  336 dwc  staff  11424 Feb 13 19:00 ..
125085846 24 -rw-r--r--    1 dwc  staff  12288 Feb 14 12:04 .tester.swp
125085778  0 drwxr-xr-x    2 dwc  staff     68 Feb 14 12:05 d1
125085791  0 -rw-r--r--    1 dwc  staff      0 Feb 14 11:58 d1.txt
125085792  0 -rw-r--r--    1 dwc  staff      0 Feb 14 11:58 d1.yyy
125085790  0 -rw-r--r--    1 dwc  staff      0 Feb 14 11:58 d1T
125085779  0 drwxr-xr-x    2 dwc  staff     68 Feb 14 12:05 d2
125085793  0 -rw-r--r--    1 dwc  staff      0 Feb 14 11:58 d2_Z
125085794  0 -rw-r--r--    1 dwc  staff      0 Feb 14 11:58 d2a
125085795  0 -rw-r--r--    1 dwc  staff      0 Feb 14 11:58 dt.YYY
125085841  8 -rw-r--r--    1 dwc  staff   1576 Feb 14 12:02 problem
125085871  8 -rwxr-xr-x    1 dwc  staff    126 Feb 14 12:04 tester

./d1:
total 0
125085778 0 drwxr-xr-x   2 dwc  staff   68 Feb 14 12:05 .
123274998 0 drwxr-xr-x  13 dwc  staff  442 Feb 14 12:05 ..

./d2:
total 0
125085779 0 drwxr-xr-x   2 dwc  staff   68 Feb 14 12:05 .
123274998 0 drwxr-xr-x  13 dwc  staff  442 Feb 14 12:05 ..

and the script tester containing:
Code:
#!/bin/ksh
for dir in */
do	for file in "${dir%/}"?*
	do	[ -f "$file" ] && mv -v "$file" "$dir"
	done
done

the command:
Code:
./tester

produces the output:
Code:
d1.txt -> d1/d1.txt
d1.yyy -> d1/d1.yyy
d1T -> d1/d1T
d2_Z -> d2/d2_Z
d2a -> d2/d2a

leaving the file hierarchy afterwards:
Code:
total 16
123274998 0 drwxr-xr-x    7 dwc  staff    238 Feb 14 12:13 .
   545528 0 drwxr-xr-x  336 dwc  staff  11424 Feb 13 19:00 ..
125085778 0 drwxr-xr-x    5 dwc  staff    170 Feb 14 12:13 d1
125085779 0 drwxr-xr-x    4 dwc  staff    136 Feb 14 12:13 d2
125085795 0 -rw-r--r--    1 dwc  staff      0 Feb 14 11:58 dt.YYY
125085841 8 -rw-r--r--    1 dwc  staff   1576 Feb 14 12:02 problem
125085871 8 -rwxr-xr-x    1 dwc  staff    126 Feb 14 12:04 tester

./d1:
total 0
125085778 0 drwxr-xr-x  5 dwc  staff  170 Feb 14 12:13 .
123274998 0 drwxr-xr-x  7 dwc  staff  238 Feb 14 12:13 ..
125085791 0 -rw-r--r--  1 dwc  staff    0 Feb 14 11:58 d1.txt
125085792 0 -rw-r--r--  1 dwc  staff    0 Feb 14 11:58 d1.yyy
125085790 0 -rw-r--r--  1 dwc  staff    0 Feb 14 11:58 d1T

./d2:
total 0
125085779 0 drwxr-xr-x  4 dwc  staff  136 Feb 14 12:13 .
123274998 0 drwxr-xr-x  7 dwc  staff  238 Feb 14 12:13 ..
125085793 0 -rw-r--r--  1 dwc  staff    0 Feb 14 11:58 d2_Z
125085794 0 -rw-r--r--  1 doc  staff    0 Feb 14 11:58 d2a

Although this was written and tested using a 1993 version of the Korn shell, it will work with any POSIX-conforming shell.

Note that if you rerun the above script before adding more regular files to be moved into the various subdirectories, it produces no output and returns a zero exit status. If you rerun RudiC's script under the same circumstances, you'll get an error processing each directory that has no new files to be moved similar to:
Code:
mv: rename d1?* to d1/d1?*: No such file or directory
mv: rename d2?* to d2/d2?*: No such file or directory

assuming namesUC.txt
contained:
Code:
d1
d2

Neither of our scripts reliably return a non-zero exit status if one or more regular files couldn't be moved and another file was moved. Both could be extended to do so if just getting diagnostic messages from mv for failed moves is insufficient for your needs.

Last edited by Don Cragun; 02-23-2016 at 05:43 AM..
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 02-15-2016
Yes, this works perfectly also but originally the empty directories are not there. I create them using code below

Code:
 #create filename list text
$ls -1 directory/filter* > names.txt
#get first 21 characters of the name only and remove duplicates
#create unique-clean filename list text
$cut -c-21 names.txt | grep -v filter|uniq > namesUC.txt
#make directories 
$cat namesUC.txt | xargs mkdir

Is there a way to have the tester include the creation of the directories with the corresponding name as in the example?

Thanks

Last edited by Don Cragun; 02-15-2016 at 02:41 PM.. Reason: Change ICODE tags to CODE tags.
# 6  
Old 02-15-2016
This would create the needed directories:
Code:
ls LC* | cut -c 1-21 | sort -u | xargs mkdir -p

The -p option to mkdir avoids errors if dirs already exists. No reasonable error checking is done.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 02-15-2016
Oh -that's neat!

Code:
spirospap$ cat tester1 
#!/bin/ksh
ls LC* | cut -c 1-21 | sort -u | xargs mkdir -p
for dir in */
do for file in "${dir%/}"?*
    do    [ -f "$file" ] && mv -v "$file" "$dir"
    done

Thanks!
Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) for full-line and multi-line sample input, output, and code segments.

Last edited by Don Cragun; 02-15-2016 at 02:43 PM.. Reason: Change ICODE tags to CODE tags.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Move several files into specific directories with a loop

Hello, I'm a first time poster looking for help in scripting a task in my daily routine. I am new in unix but i am attracted to its use as a mac user. Bear with me... I have several files (20) that I manually drag via the mouse into several named directories over a network. I've used rsync... (14 Replies)
Discussion started by: SonnyClark
14 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

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

Reading only first record from the multipe directories

Hi All, I have a requirement, I had a parent directory Land under that we have sub directories Yesterday, Today and Tommorrow And we have a file test.txt under the above directories Yesterday, Today and Tommorrow The data in the file test.txt under Yesterday folder is ... (5 Replies)
Discussion started by: somu_june
5 Replies

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

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

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

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

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