LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-14-2018, 09:30 AM   #1
pries2018
LQ Newbie
 
Registered: Oct 2018
Posts: 7

Rep: Reputation: Disabled
script to move files in a folder(A) to multiple folders within the same working directory without creating subfolders


I have a folder(header2018) within a pwd (/goon/gos1/header2018/) that contains hundreds of thousands of files. I have a script that moves files from header2018 into created subfolders, however I don't want the files moved into subfolders. I want them moved into folders created within the pwd. I'm thinking script could look like this.


let fileCount=3000
let dirNum=1

for f in *
do
[ -d $f ] && continue
[ $fileCount -eq 3000 ] && {
dir=$(printf "%03d" $dirNum)
mkdir $dir
let dirNum=$dirNum+1
let fileCount=0
}

mv $f $dir
mv $dir /pwd/header2018/
let fileCount=$fileCount+1
done



Please help
 
Old 10-14-2018, 10:16 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
If I understand your intentions, you create directories whose names are numbers, and move 3000 files into each of them.

You script looks OK except this part will be executed for each file:
Code:
mv $f $dir
mv $dir /pwd/header2018/
After the first file $f, $dir won't exist in this directory and you will get 2999 error messages.

Fix that and test it with a few thousand dummy files in a dummy directory.

Personally, I think a program is more readable when you use if instead of the && construct, but that's just a style question.
 
Old 10-14-2018, 10:26 AM   #3
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 279

Rep: Reputation: 100Reputation: 100
And fileCount initialization
Code:
let fileCount=3000
is wrong.
 
Old 10-22-2018, 05:17 AM   #4
pries2018
LQ Newbie
 
Registered: Oct 2018
Posts: 7

Original Poster
Rep: Reputation: Disabled
Update on Question.

I found this script as the solution to my problem.

#!/bin/bash
i=0 # counter for current file
j=0 # counter for current directory
batchsize=1000 # size of each batch
find folderA -type f -print0 | while read -r -d $'\0' file
do
if (( i % batchsize == 0 ))
then
(( j++ ))
mkdir "folderA_$j"
fi
mv -- "$file" "folderA_$j"
(( i++ ))
done
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Write a script to move specific files in various folders to one folder linuxisthedevil Linux - General 13 11-18-2010 06:29 AM
[SOLVED] Moving files from folders and subfolders to a specific folder mrj2 Linux - Newbie 12 08-22-2010 12:40 PM
Creating a script to move or copy files into multiple directories below the files matthes138 Linux - Newbie 5 08-25-2009 04:57 PM
Trying to move files beyond cutoff date in subfolders and retain directory structure Erik Mesoy Linux - Newbie 2 03-16-2008 08:58 AM
need a script to delete all files from a folder and subfolders cccc Programming 1 03-04-2005 10:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration