I have about 4 million extension less files in one directory.
I need a bash script to copy them into another directory such that those files will be sorted into 1000 files per directory.
Eg. source directory is /temp/hugenooffiles/
The file names are like m_100, m_1005, m_10005
, etc.
destination directory is /temp/sortedfiles/
In the destination directory, a directory named 1 should be created containing files from m_1
through m_999
.
directory 2
should be created containing files m_1000
through m_1999
and so on.
The destination files should be renamed to 1_m, 1000_m
, etc.
Also, if files in destination folder already exist then they should be overwritten.
Kindly help.
#!/bin/bash
SOURCE='/temp/hugenooffiles'
DEST='/temp/sortedfiles'
I=0
for FILE in "$SOURCE"/m_*; do
(( D = 1 + ++I / 1000 ))
[[ -d $DEST/$D ]] || mkdir -p "$DEST/$D" ## You can just skip dir checking but that would be slow.
cp -v "$FILE" "$DEST/$D/m_$I"
done
Note: By default pathname expansions in Bash ( "$SOURCE"/m_*
) are sorted.
I modified konsolebox's script and it did the job. Thank you.
> #!/bin/bash
>
> SOURCE='/temp/hugenooffiles'
> DEST='/temp/sortedfiles'
>
> I=0
> for FILE in "$SOURCE"/m_*;
> do
> (( D = (( ${FILE#*_} / 1000 )) + 1 ))
> [[ -d $DEST/$D ]] || mkdir -p "$DEST/$D"
> mv -v "$FILE" "$DEST/$D/${FILE#*_}_m"
> (( ++I ))
> done
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.