I am struggling with a command to rename directory and files.
- OS: MACOS 11.4
- Objective : rename directories and file by replacing the space
_
- Command tried :
find . -maxdepth 1 -iname "* *" -exec sh -c 'fichier="{}"; mv -- $fichier ${fichier// /_}' \;
I use find
because I want to be able to navigate through the depth of the directory tree 1 level at a time.
The result of the command is:
usage: mv [-f | -i | -n] [-v] source target
mv [-f | -i | -n] [-v] source ... directory
So instead of mv
I used echo
to check the command
find . -maxdepth 1 -iname "* *" -exec sh -c 'fichier="{}"; echo $fichier ${fichier// /_}' \;
That seems to be working as I expected:
./Job offers etc ./Job_offers_etc
./School results ./School_results
./Famille et Souvenirs ./Famille_et_Souvenirs
./KID education ./KID_education
./Template Open Office-MS Office ./Template_Open_Office-MS_Office
./Mode Emplois ./Mode_Emplois
./cartres de voeux ./cartres_de_voeux
What did I do wrong when used mv
?
mv ./Job offers etc ./Job_offers_etc
would be interpreted as having 4 paramters; you need to put"
" around the first "Job offers etc" for it to be a single parameter. – Stephen Harris Jun 23 '21 at 11:43find . -maxdepth 1 -iname "* *" -exec sh -c 'fichier="{}"; ls $fichier ${fichier// /_}' \;
. That is, usels
instead ofecho
...ls
should complain that the expanded filename,${fichier// /_}
, does not exist. But you might be surprised about the first... – C. M. Jun 23 '21 at 12:48mv -- "$fichier" "${fichier// /_}"
? Those quotes are important! – Toby Speight Jun 24 '21 at 10:34