I want to write a bash script that I can run in a directory and that will do the following:
- Go through all subdirectories (recursively)
- If there are two files in the same given subdirectory whose filenames have the same first 16 characters (e.g. “YourTestFileNameHey.jpg” and “YourTestFileNameFoo.png”), output their file names (or move one of them to a different location).
Does anyone know how to do this?
I don’t know if this will do the job for you, but you can base yourself on the following script:
It will list every second file that has the same first 16 characters as another file in the same directory. You could easily replace the echo command inside printf(“echo \”%s\””, $1) by a mv command.
Now, if you want to use this script in all subdirectories under a specific directory, let’s call it mydir, you could do that with the find command like this, assuming you saved the above script in a file called myscript which resides in the same directory as mydir:
Does this help?