It's a serious question. I test some awk
scripts and I need files with a newline in their names.
Is it possible to add a newline into a filename with mv
?
I now, I can do this with touch
:
touch "foo
bar"
With touch I added the newline character per copy and paste. But I can't write foo
Returnbar
in my shell.
How can I rename a file, to have a newline in the filename?
Edit 2015/06/28; 07:08 pm
To add a newline in zsh
I can use, Alt+Return
Best Answer
It is a bad idea (to have strange characters in file names) but you could do
(you could also have done
mv somefile.txt "$(printf "foo\nbar")"
ormv somefile.txt foo$'\n'bar
, etc... details are specific to your shell. I'm usingzsh
)Read more about globbing, e.g. glob(7). Details could be shell-specific. But understand that
/bin/mv
is given (by your shell), via execve(2), an expanded array of arguments: argument expansion and globbing is the responsibility of the invoking shell.And you could even code a tiny C program to do the same:
Save above program in
foo.c
, compile it withgcc -Wall foo.c -o foo
then run./foo
Likewise, you could code a similar script in Perl, Ruby, Python, Ocaml, etc....
But that is a bad idea. Avoid newlines in filenames (it will confuse the user, and it could break many scripts).
Actually, I even recommend to use only non-accentuated letters, digits, and
+-/._%
characters (with/
being the directory separator) in file paths. "Hidden" files (starting with.
) should be used with caution and parcimony. I believe using any kind of space in a file name is a mistake. Use an underscore instead (e.g.foo/bar_bee1.txt
) or a minus (e.g.foo/bar-bee1.txt
)