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.
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)
.
$
or other shell metacharacters: mv "$path"/'this is my $$$ money.ipa' "$path"/'this_is_my_$$$_money.ipa'
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.)
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
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
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
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.
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
path
a variable or a program? Variables are expanded with${...}
,$(...)
is for running a command and substituting its output.$(path)
most likely does something else than you think.${path}
perhaps?$(path)
would try to execute a command calledpath
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