I'm trying to make a bash script that goes to every single folder and then renames all the files in the folders numerically. There are multiple folders and multiple files within the folders. I want to rename all of them and then transfer all of the files to a single folder. The code I have so far is
#! /bin/bash
FOLDERS=$(ls *)
COUNT=0
for folder in $FOLDERS
do
cd $folder
FILE=$(ls *)
for file in $FILE
do
echo "working on it"
COUNT=$((COUNT+1))
mv $file $COUNT
cd ..
done
but it isn't working...
Any thoughts on how to do this?
Something like this will do the job:
#!/bin/bash
shopt -s globstar
source='/your/source/folder'
target='/your/target/folder'
i=0
for f in ${source}/**; do
if [[ -f "${f}" ]]; then
((i++))
mv "${f}" "${target}/${i}"
fi
done
You can do this in Ruby and hence, you don't have to worry about special characters in filenames :
#!/usr/bin/ruby
require 'find'
require 'fileutils'
source_dir="./"
target_dir="target/"
count = 0
Find.find(source_dir) do |path|
next if path =~ /(.*\.rb$|.*\.\/$|.*\.\.\/$)/
puts "Found : " + path
File.rename(path,count.to_s)
FileUtils.mv(source_dir + count.to_s, target_dir)
count = count + 1
end
Regards!
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.