본문 바로가기
Linux

리눅스에서 새로운 파일 만들기(touch, echo, cat)

by chuckolet 2019. 3. 7.

There are several possible solutions:


Create an empty file

touch file

>file

echo -n > file

printf '' > file

The  echo version will work only if your version of  echo supports the  -n switch to suppress newlines. This is a non-standard addition. The other examples will all work in a POSIX shell.

Create a file containing a newline and nothing else

echo '' > file

printf '\n' > file

This is a valid "text file" because it ends in a newline.

Write text into a file

"$EDITOR" file

echo 'text' > file

cat > file <<END \
text
END

printf 'text\n' > file

These are equivalent. The  $EDITOR command assumes that you have an interactive text editor defined in the EDITOR environment variable and that you interactively enter equivalent text. The  cat version presumes a literal newline after the  \ and after each other line. Other than that these will all work in a POSIX shell.

Of course there are many other methods of writing and creating files, too.

References

https://stackoverflow.com/a/9382303


제 글이 도움이 되셨다면 간단하게 '공감', '댓글' 부탁드립니다!



댓글