A script that batch replaces the special symbol file name of a space

for example, the files in the csv directory below
lf-orders-2018-11-20 (1700) .csv
lf-orders-2018-11-20 17002.csv

file names contain spaces and are interspersed with parentheses in both Chinese and English. You need to replace them with an underscore _
to generate the following:
lf-orders-2018-11-20_1700_.csv
lf-orders-2018-11-20_17002.csv

SAVEIFS=$IFS
IFS=$"\n"
for file in `ls csv | grep .csv`
do
 newfile=`echo ${file} | sed "s/ /_/g"| sed "s//_/g"| sed "s/)/_/g"`
 -sharp echo $newfile
 mv csv/$file csv/$newfile
done
-sharp revert IFS
IFS=$SAVEIFS

I already have the script above, but it doesn"t work very well.
will report mv: cannot stat "csv/lf-orders-2018-11-20 17002. CSV" $"\ n""lf-orders-2018-11-20). Csv": No such file or directory

for a more concise way of writing, thank you!

Feb.15,2022

rename command to understand


execute the following two commands in the same directory as the csv file.

ls *.csv|sed -r 's/(.*)([^)]+)\)(.*)/mv "&" \1_\2_\3/e'
ls *.csv|sed -r 's/(.*)\s+(.*)/mv "&" \1_\2/e'

Test:

answer the questions in the subject's comments, and execute the method remotely to another machine:

ssh lft "ls csv/*.csv|sed -r 's/(.*)([^)]+)\)(.*)/mv \"&\" \1_\2_\3/e'"
ssh lft "ls csv/*.csv|sed -r 's/(.*)\s+(.*)/mv \"&\" \1_\2/e'"

since you are going to use scripts to deal with it, why not use python?
python can be easily done. The script is as follows:

-sharp!/usr/bin/env python3
-sharp-*- coding: utf-8 -*-

import sys
import os
import re

def main():

    path = sys.argv[1]
    for dirpath, dirnames, filenames in os.walk(path):
        for filename in filenames:
            newname = re.sub('[ \(]+', '_', filename)
            newname = re.sub('[\)]+', '', newname)
            if filename != newname:
                print('rename> {}: {} => {}'.format(dirpath, filename, newname))
                os.rename(os.path.join(dirpath, filename), os.path.join(dirpath, newname))

if __name__ == '__main__':
    main()

the test execution results are as follows:

$ find csv
csv
csv/lf-orders-2018-11-20 17002.csv
csv/ccc
csv/ccc/lf-orders-2018-11-20 17002.csv
csv/ccc/csv
csv/ccc/csv/lf-orders-2018-11-20 17002.csv
csv/ccc/csv/ccc
csv/ccc/csv/ccc/lf-orders-2018-11-20 17002.csv
csv/lf-orders-2018-11-201700).csv
$ ./z.py csv
rename> csv: lf-orders-2018-11-20 17002.csv => lf-orders-2018-11-20_17002.csv
rename> csv: lf-orders-2018-11-201700).csv => lf-orders-2018-11-20_1700.csv
rename> csv/ccc: lf-orders-2018-11-20 17002.csv => lf-orders-2018-11-20_17002.csv
rename> csv/ccc/csv: lf-orders-2018-11-20 17002.csv => lf-orders-2018-11-20_17002.csv
rename> csv/ccc/csv/ccc: lf-orders-2018-11-20 17002.csv => lf-orders-2018-11-20_17002.csv
$ find csv
csv
csv/lf-orders-2018-11-20_17002.csv
csv/ccc
csv/ccc/lf-orders-2018-11-20_17002.csv
csv/ccc/csv
csv/ccc/csv/lf-orders-2018-11-20_17002.csv
csv/ccc/csv/ccc
csv/ccc/csv/ccc/lf-orders-2018-11-20_17002.csv
csv/lf-orders-2018-11-20_1700.csv

for name in ./*;do mv "${name}" "${name// /}";done

about a line of shell. Why bother so much

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bdc9c9-53f6f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bdc9c9-53f6f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?