Yesterday, I processed a task, moved files in batches, and renamed files in batches according to the rules.
Of course, this process encountered some problems, such as: the file name includes spaces, causing the rename to fail; and the original file name rules are inconsistent.
Let me share the specific problems and processes.
Problem requirement
There is a two-level directory, the first level directory contains several folders, and the sub-directory contains some column files, such as:
➜ ls -lR
./test:
total 0
drwxr-xr-x 5 ylspirit staff 160 Jun 25 22:01 11001
drwxr-xr-x 5 ylspirit staff 160 Jun 25 22:01 11002
drwxr-xr-x 5 ylspirit staff 160 Jun 25 22:01 11003
drwxr-xr-x 5 ylspirit staff 160 Jun 25 22:01 11004
drwxr-xr-x 5 ylspirit staff 160 Jun 25 22:01 11005
drwxr-xr-x 5 ylspirit staff 160 Jun 25 22:01 11006
drwxr-xr-x 5 ylspirit staff 160 Jun 25 22:01 11007
drwxr-xr-x 5 ylspirit staff 160 Jun 25 22:01 11008
drwxr-xr-x 5 ylspirit staff 160 Jun 25 22:01 11009
./test/11001:
total 0
-rw-r--r-- 1 ylspirit staff 0 Jun 25 22:01 11001-aaa-1231-fff
-rw-r--r-- 1 ylspirit staff 0 Jun 25 22:01 11001-fddd-3431-fdff
-rw-r--r-- 1 ylspirit staff 0 Jun 25 22:01 11001_ggg- 1231-ffdff
![](https://www.linuxcommands.site/wp-content/uploads/2019/06/image-53.png)
As shown in the above picture, the test directory contains thousands of folders named by numbers. Each folder contains several files. The file name rules are: number-string-string-string-string-string, and the number is the parent directory name.
Need to move all the files to the test directory, and change the file name to the number -#-#-#-#- the last string of the original file name.
1. Move files in batch
Use the linux find command and execute the mv command.
➜ cd test
➜ find . -type f -exec mv {} ./ \;
2. Delete empty directories in batch
➜ find . -type d -empty -delete
or
➜ find . -type d -empty -exec rm -r {} \;
3. Batch delete spaces in file names
There are some file names in the file that are not uniform. They contain spaces and need to delete the spaces in the file name in batches.
Use linux ls, linux tr and shell while loops.
➜ ls | while read i; do mv "$i" $(echo $i|tr -d ' '); done
4. Batch rename files
Batch rename files using the linux awk command.
➜ find . -type f | awk -F"[-_]" '{system("mv "$0" ./"$1"-#-#-#-#-"$NF"")}'
mission completed.
Here use the linux find command to find files, move files in batches, delete empty directories (folders) in batches, use the linux awk command to batch rename files, and use the shell while loop to delete the spaces character in the file name in batches.
Remarks:
Test the data generation script.
➜ awk 'BEGIN{for(i=11001;i<11010;i++) { system("mkdir "i"");system("cd "i";touch "i"-aaa-1231-fff "i"-fddd-3431-fdff "i"\"_ggg- 1231-ffdff\";") }}'
Reference: