6

I have a file with the name:

this is my app.ipa

I'd like to rename this file:

mv $(path)/this is my app.ipa $(path)/this_is_my_app.ipa 

Does not seem right... Thanks in advance.

3
  • 2
    Is path a variable or a program? Variables are expanded with ${...}, $(...) is for running a command and substituting its output.
    – Barmar
    Sep 28, 2022 at 14:06
  • 1
    $(path) most likely does something else than you think. ${path} perhaps? Sep 28, 2022 at 14:21
  • $(path) would try to execute a command called path and substitute its output. There's no such command. $PATH or ${PATH} are not much use either as they both refer to the command search path for your session. It might be better to make it obvious it's an example path in your example eg. mv source_folder/this\ is\ my\ app.ipa destination_folder/this_is_my_app.ipa Sep 30, 2022 at 8:59

7 Answers 7

15

While you could precede each space with a \ character, you would be better of enclosing the entire file name with a pair of double quotes"", as shown below. Here I assume path is a variable, which also may contain spaces.

mv "$path/this is my app.ipa" "$path/this_is_my_app.ipa"

Or, a suppose you could also use the following.

mv "$path"/this\ is\ my\ app.ipa "$path"/this_is_my_app.ipa

If path is executable with output, then you are correct to use $(path).

1
  • Single quotes can be useful if the filename contains $ or other shell metacharacters: mv "$path"/'this is my $$$ money.ipa' "$path"/'this_is_my_$$$_money.ipa'
    – pts
    Sep 29, 2022 at 17:41
15

Other answers explain the various ways to enquote or escape spaces. But as another option, tab-completion is your friend here and whenever you have filenames that are difficult to type — whether due to characters that need escaping, accented or otherwise unusual characters, or just length.

Specifically, if you start typing the command and current filename:

   mv $(path)/thi

and then press tab, your shell should automatically complete to the escaped filename:

   mv $(path)/this\ is\ my\ app.ipa

after which you can continue typing the command. (If you have multiple files whose names begin the same way, tab-completion should complete the name as far as the first ambiguous character.)

1
  • 5
    This depends on which shell you use and how it is configured. It is true for the default given on MacOS. Sep 28, 2022 at 14:21
6

You have to either escape the spaces:

mv $(path)/this\ is\ my\ app.ipa $(path)/this_is_my_app.ipa

or use quote marks:

mv "$(path)/this is my app.ipa" $(path)/this_is_my_app.ipa
0
5

Depends on the shell.

Most are enclose in quotes - either " or ' - they do have different effects in expanding environment variables. POSIX shells don't expand ~ so

mv "$(path)/this is my app.ipa" $(path)/this_is_my_app.ipa 

POSIX shells also allow escaping characters using \

mv $(path)/this\ is\ my\ app.ipa $(path)/this_is_my_app.ipa 

Also if there is only one file with a similar name use wildcards e.g.

mv $(path)/this*.ipa $(path)/this_is_my_app.ipa 

or

mv $(path)/this?is?my?app.ipa $(path)/this_is_my_app.ipa 
0

Here is a bit of a different approach which may be useful to you.

This following code will rename the file "this is my app.ipa", in the current working directory, to this "this_is_my_app.ipa"

for f in ./"this is my app.ipa" ;do echo "$f" |mv "$f" "$(sed 's@ @_@g')" ;done

This following version will rename all files, in the current working directory, ending with ".ipa" which also contain spaces in the name... by replacing blank spaces with underscores.

for f in ./*.ipa ;do echo "$f" |mv "$f" "$(sed 's@ @_@g')" ;done

This following version will rename all files, in the current working directory, which contain spaces in the name... by replacing blank spaces with underscores.

for f in ./* ;do echo "$f" |mv "$f" "$(sed 's@ @_@g')" ;done
0

Or you could type mv and a space then drag and drop the file into the terminal, it will add the full path of the file all properly escaped (and then you type another space and the new name)

Or you could copy the path, then type mv and a space then --V (or in the menu: Edit → Paste Escaped Text) and another space and the new name.

-2

Use quotation marks when specifying long filenames or paths with spaces. For example, typing the copy c:\my file name d:\my new file name command at the command prompt results in the following error message: The system cannot find the file specified. The quotation marks must be used.

Regards, Peter

2
  • 1
    Welcome to Ask Different. Your answer would be a little more credible if your example actually used a Unix file naming convention rather than Windows / MS-DOS commands and conventions. I'd advise you make an amendment to bring it into line with what's generally accepted here. Sep 30, 2022 at 8:55
  • But neither does this add anything that isn't already covered by the existing answers.
    – MrWhite
    Oct 4, 2022 at 12:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .