I have a directory containing around 24800 .mp4 files how can I split it into 4 directories containg all the files without keeping the original 24800 files in one directory ? is there any MV/move command to do this?
13 Answers
If there is no system in the file names, allowing you to move parts of the files using wildcards, then I would be using the bash text tools to create a list of the files in a text file:
ls > filelist.txtthen split it in different parts, then for each part use sed to edit each line so it constitutes a move command, e.g.
filename.mp3becomes
mv "filename.mp3" targetdirThese text files can then be sourced at the command line to automatically execute each and every command to move a file to a different folder. If for example such text file is called "part_1", then you can source that file as:
. part_1or
source part_1 Alternatively, you can make such file containing valid commands executable, and execute it. Preferably then, you add a shebang (#!/bin/bash) as the first line of the file.
You can remove /bin/echo when you feel the script does what you expect it to do
#!/bin/bash
a=(source/*)
for ((b=0, c=$((${#a[@]} / 4)),d=0; b < ${#a[@]}; b+=c)); do mkdir -p target/$((++d)) printf %s\\0 "${a[@]:$b:$c}" | xargs -r -0 /bin/echo mv -t target/$d --
done #!/bin/bash
# Paste this to a file and make the file executable using chmod +x filename
# Run the script file like so ./filename or bash filename
# This will create 4 new directories in the current directory where you run the script
# It will move the files from the source directory to the new directories and split them equally ~
# Change source_dir/ below to the path of your source directory.
source_dir="source_dir/"
file_extension="mp4"
drectories=4
file_num="$(ls -l $source_dir | wc -l)"
max_files="$(($file_num/$drectories))"
max_files2="$max_files"
dir_num=1
i=0
mkdir -p new_dir"$dir_num"
for f in "$source_dir"*".$file_extension" do if (($dir_num == $drectories)) then # Remove echo below when satisfied with output to do the actual moving echo mv "$f" new_dir"$dir_num" else (($i == $max_files2)) && ((dir_num++)) && max_files2="$(($max_files2+$max_files))" && mkdir -p new_dir"$dir_num" # Remove echo below when satisfied with output to do the actual moving echo mv "$f" new_dir"$dir_num" ((i++)) fi done