I have a file whose file name has a space like First Name_20180810.csv. The date (20180810 in the above example) changes daily in the filename. How can I rename First Name_*.csv to be FirstName_*.csv?

  • 1
    difficult to understand, try using the blockquote or code sample to enter the file name or commands from body toolbar while drafting your question – Bharat Aug 10 '18 at 21:00
  • Similar to stackoverflow.com/questions/6911301/… ... – Anon Aug 11 '18 at 3:20

If you do not have access to the rename tool, this should work:

for file in *.csv; do
    if ! [[ -f "${file// /}" ]]; then
        mv "$file" "${file// /}"
        echo "Replacement for '${file}' already exists; skipping"
  • I always recommend using mv -i or -n when doing any sort of bulk move/rename, to avoid silent and irreversible data loss in case there are any name conflicts. – Gordon Davisson Aug 11 '18 at 6:50

Use rename command:

rename "s/ //g" *.csv

man rename :examples


If I understand correctly something like this should work for a single file

mv First\ \Name_20180810.csv FirstName_20180810.csv

  • ya I'll just assume they only need 1 file changed. It's not clear from the question – GNUzilla Aug 10 '18 at 21:06
  • Thank you all. Yes, there is only 1 file whose name I need to change. The command will run daily, and the file name changes daily. For example, today the file name is First Name_20180812.csv, tomorrow the file name is First Name_20180813.csv. So, the command can't be "mv First Name_20180812.csv FirstName_20180812.csv", because tomorrow the file nameis First Name_20180813.csv – faujong Aug 12 '18 at 23:35
  • The solution that DopeGhoti/confetti gave works. Here is my command on 1 line: for file in /dv/DataStage/TEST/MyFile*.csv ; do if ! [[ -f "${file// /}" ]]; then mv "$file" "${file// /}" ; fi ; done – faujong Aug 23 '18 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.