Database Reference
In-Depth Information
exist, its contents would have been overwritten. You can also append the output to a
file with >> , meaning the output is put after the original contents:
$ echo -n "Hello" > hello-world
$ echo " World" >> hello-world
The tool echo simply outputs the value you specify. The -n option specifies that echo
should not output a trailing newline.
Saving the output to a file is useful if you need to store intermediate results (e.g., for
continuing with your analysis at a later stage). To use the contents of the file hello-
world again, we can use cat (Granlund & Stallman, 2012), which reads a file and
prints it:
$ cat hello-world | wc -w
2
(Note that the -w option indicates wc to only count words.) The same result can be
achieved with the following notation:
$ < hello-world wc -w
2
This way, you are directly passing the file to the standard input of wc without running
an additional process. If the command-line tool also allows files to be specified as
command-line arguments, which many do, you can also do the following for wc :
$ wc -w hello-world
2 hello-world
Working with Files
As data scientists, we work with a lot of data, which is often stored in files. It's impor‐
tant to know how to work with files (and the directories they live in) on the com‐
mand line. Every action that you can do using a graphical user interface, you can do
with command-line tools (and much more). In this section, we introduce the most
important ones to create, move, copy, rename, and delete files and directories.
You have already seen how we can create new files by redirecting the output with
either > or >> . In case you need to move a file to a different directory, you can use mv
(Parker, MacKenzie, & Meyering, 2012):
$ mv hello-world data
You can also rename files with mv :
$ cd data
$ mv hello-world old-file
You can also rename or move entire directories. In case you no longer need a file, you
delete (or remove) it with rm (Rubin, MacKenzie, Stallman, & Meyering, 2012):
Search WWH ::




Custom Search