Unix command to remove space from a file name

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












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?










share|improve this question



















  • 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 at 21:00










  • Similar to stackoverflow.com/questions/6911301/… ...
    – Anon
    Aug 11 at 3:20














up vote
0
down vote

favorite












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?










share|improve this question



















  • 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 at 21:00










  • Similar to stackoverflow.com/questions/6911301/… ...
    – Anon
    Aug 11 at 3:20












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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?










share|improve this question















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?







rename filenames whitespace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 11 at 7:16









Kusalananda

106k14209327




106k14209327










asked Aug 10 at 20:55









faujong

82




82







  • 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 at 21:00










  • Similar to stackoverflow.com/questions/6911301/… ...
    – Anon
    Aug 11 at 3:20












  • 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 at 21:00










  • Similar to stackoverflow.com/questions/6911301/… ...
    – Anon
    Aug 11 at 3:20







1




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 at 21:00




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 at 21:00












Similar to stackoverflow.com/questions/6911301/… ...
– Anon
Aug 11 at 3:20




Similar to stackoverflow.com/questions/6911301/… ...
– Anon
Aug 11 at 3:20










3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










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// /"
else
echo "Replacement for '$file' already exists; skipping"
fi
done





share|improve this answer






















  • 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 at 6:50

















up vote
1
down vote













Use rename command:



rename "s/ //g" *.csv


man rename :examples






share|improve this answer



























    up vote
    0
    down vote













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



    mv First Name_20180810.csv FirstName_20180810.csv






    share|improve this answer






















    • ya I'll just assume they only need 1 file changed. It's not clear from the question
      – GNUzilla
      Aug 10 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 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 at 21:06










    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461899%2funix-command-to-remove-space-from-a-file-name%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    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// /"
    else
    echo "Replacement for '$file' already exists; skipping"
    fi
    done





    share|improve this answer






















    • 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 at 6:50














    up vote
    1
    down vote



    accepted










    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// /"
    else
    echo "Replacement for '$file' already exists; skipping"
    fi
    done





    share|improve this answer






















    • 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 at 6:50












    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    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// /"
    else
    echo "Replacement for '$file' already exists; skipping"
    fi
    done





    share|improve this answer














    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// /"
    else
    echo "Replacement for '$file' already exists; skipping"
    fi
    done






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 11 at 8:18









    confetti

    25513




    25513










    answered Aug 10 at 22:27









    DopeGhoti

    41k55080




    41k55080











    • 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 at 6:50
















    • 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 at 6:50















    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 at 6:50




    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 at 6:50












    up vote
    1
    down vote













    Use rename command:



    rename "s/ //g" *.csv


    man rename :examples






    share|improve this answer
























      up vote
      1
      down vote













      Use rename command:



      rename "s/ //g" *.csv


      man rename :examples






      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        Use rename command:



        rename "s/ //g" *.csv


        man rename :examples






        share|improve this answer












        Use rename command:



        rename "s/ //g" *.csv


        man rename :examples







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 10 at 22:00









        GAD3R

        22.8k154895




        22.8k154895




















            up vote
            0
            down vote













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



            mv First Name_20180810.csv FirstName_20180810.csv






            share|improve this answer






















            • ya I'll just assume they only need 1 file changed. It's not clear from the question
              – GNUzilla
              Aug 10 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 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 at 21:06














            up vote
            0
            down vote













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



            mv First Name_20180810.csv FirstName_20180810.csv






            share|improve this answer






















            • ya I'll just assume they only need 1 file changed. It's not clear from the question
              – GNUzilla
              Aug 10 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 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 at 21:06












            up vote
            0
            down vote










            up vote
            0
            down vote









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



            mv First Name_20180810.csv FirstName_20180810.csv






            share|improve this answer














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



            mv First Name_20180810.csv FirstName_20180810.csv







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 10 at 21:06

























            answered Aug 10 at 21:02









            GNUzilla

            11




            11











            • ya I'll just assume they only need 1 file changed. It's not clear from the question
              – GNUzilla
              Aug 10 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 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 at 21:06
















            • ya I'll just assume they only need 1 file changed. It's not clear from the question
              – GNUzilla
              Aug 10 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 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 at 21:06















            ya I'll just assume they only need 1 file changed. It's not clear from the question
            – GNUzilla
            Aug 10 at 21:06




            ya I'll just assume they only need 1 file changed. It's not clear from the question
            – GNUzilla
            Aug 10 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 at 23:35




            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 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 at 21:06




            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 at 21:06

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461899%2funix-command-to-remove-space-from-a-file-name%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay