$ touch example_file.txt
Interacting with Other Programs
The command line interface can be used for much more than just changing file systems, we can run programming scripts, call programs, and edit files.
We will explore some other things that you can do in command line which may make doing your work quicker.
Below is an example of opening a program using the command line.
1 Example
We first create a file in our current directory:
We can then open it with the Notepad program using:
$ notepad example_file.txt
We first create a file in our current directory:
>echo. > example_file.txt
We can then open it with the Notepad program using:
>notepad example_file.txt
2 Calling other languages
Using the command line we can interact with other programming languages, such as R and python. In the following examples we will use python as our case study but it is a similar process for R.
As we ran the Notepad program from our command line we can do the same for python.
NOTE: Please keep in mind which command line interface you are using, and whether or not it is possible to run a program from that specific terminal. For example, you cannot run python from Git Bash, and although you can run python in Windows Command Prompt, it is easier to use Anaconda Prompt.
In this section we will presume you are using MacOS and the terminal command prompt interface.
When we run the python program on it’s own it will open up the interactive mode, allowing us to test our python commands:
$ python
Python 3.6.1 | Anaconda 4.4.0 (g4-bit)|...
Type "help" ...
>>>
We can then type in the commands we want to run in python after the >>>
lines.
To run a python script (a .py
file) we write the python
command followed by the filepath of the script we want to run.
$ python <filepath>
If we want to run new_analysis_copy.py
within /root/Projects/Analysis/
while we are in the /root/
directory we type.
$ python ./Projects/Analysis/new_analysis_copy.py
**Running new_analysis_copy.py**
In this section we will presume you are using Windows Command Prompt.
If you are using Anaconda Prompt, the method is more similar to the UNIX-like section.
When we run the python program on it’s own we need to find the python.exe
file. Running it alone will open up the interactive mode, allowing us to test our python commands:
The typical location for python.exe
on our machines is within the C: drive in a folder called PythonXX where XX is the version.
To run the python interactive environment we type:
><python.exe filepath>
>C:\Python36\python.exe
Python 3.6.1 | Anaconda 4.4.0 (g4-bit)|...
Type "help" ...
>>>
We can then type in the commands we want to run in python after the >>>
lines.
To run a python script (a .py
file) we write the python
command followed by the filepath of the script we want to run.
><python.exe filepath> <script filepath>
If we want to run new_analysis_copy.py
within \root\Projects\Analysis\
while we are in the \root\
directory we type.
>C:\Python36\python.exe .\Projects\Analysis\new_analysis_copy.py
**Running new_analysis_copy.py**
3 Redirecting Output
We will often run scripts or commands that produce some output or printed statement. This will appear in the command line interpreter. This is useful if we are checking something quickly, but if we actually want to store a command line output we can redirect this to a file. This is done using the >
command.
The syntax for redirecting output is as follows. You first write the command of what produces the output, this is followed by >
then the file you wish to store the output in.
Assuming we are in the /root/
directory.
In UNIX, we would redirect the output using:
$ <output command> > <filepath>
For example, if we wanted to store the filenames and structure of our system we would write:
$ find > ./Projects/directory_structure.txt
If you then open up this file you will see the structure:
$ notepad ./Projects/directory_structure.txt
.
./new_analysis.py
./Projects
./Projects/Analysis
./Projects/Analysis/new_analysis_copy.py
./Projects/DataGathering
./Projects/DataGathering/data.csv
./Projects/DataGathering/supplementaryData.sql
./Projects/directory_structure.txt
Assuming we are in the \root\
directory.
In Windows, we would redirect the output using:
><output command> > <filepath>
For example, if we wanted to store the filenames and structure of our system we would write:
>tree /A /F > .\Projects\directory_structure.txt
The /A
option ensures that we get a nice visible output to our file.
If you then open up this file you will see the structure:
>notepad .\Projects\directory_structure.txt
Folder PATH listing for volume Data
Volume serial number is D64A-FC3A
D:.
| new_analysis.py
|
\---Projects
| directory_structure.txt
|
+---Analysis
| new_analysis_copy.py
|
\---DataGathering
data.csv
supplementaryData.sql
4 Editing files
Using certain commands we can change the text in an existing file.
In UNIX-like command prompt interfaces we can use the command cat
to make changes to files. It has the following syntax:
$ cat [options] <filepath>
If we want to create a new file and edit it we write:
$ cat > example_cat.txt
"Here's a line I want to put in the file."
This then gives us a new line where we can write into the file. To end editing the file press Enter
then Ctrl+D
.
We can look at the new contents of the file by writing:
$ cat example_cat.txt
For more intricate editing of files within command line we can use a system called Vim, which allows us to edit existing files without exiting the command line. This is beyond the scope of the course but more information can be found here.
In Windows command prompt interfaces we can use the command copy con
to create a new file with custom input. It has the following syntax:
>copy con <filepath>
If we want to update file with new values:
>copy con example_cat.txt
"Here's a line I want to put in the file."
This then gives us a new line where we can write in to the file. To end editing the file press Enter
then Ctrl+C
.
We can look at the new contents of the file by writing:
>type example_cat.txt
For more intricate editing of the files we however do need to use Notepad.
5 Exercises
You do not need to complete all of these commands within the “file_system” directory alone.
Navigate to each relevant directory to practice moving around file systems.
We presume you are starting in the “file_system” folder.
Open the
notes.txt
file within “reviewer_A” in Notepad with the command line.From “file_system” redirect the output of the file structure into a file called “structure.txt”.
Display what is within the
notes.txt
file in “reviewer_B” within the command line.
Question 1
Open the notes.txt
file within “reviewer_A” in Notepad with the command line.
$ cd Documentation/Code_review/reviewer_A
$ notepad notes.txt
Question 2
From “file_system” redirect the output of the file structure into a file called “structure.txt”.
$ cd ../../..
$ find > structure.txt
Question 3
Display what is within the notes.txt
file in “reviewer_B” within the command line.
$ cd Documentation/Code_review/reviewer_B
$ cat notes.txt
Question 1
Open the notes.txt
file within “reviewer_A” in Notepad with the command line.
>cd Documentation\Code_review\reviewer_A
>notepad notes.txt
Question 2
From “file_system” redirect the output of the file structure into a file called “structure.txt”.
>cd ..\..\..
>tree /F /A > structure.txt
Question 3
Display what is within the notes.txt
file in “reviewer_B” within the command line.
>cd Documentation\Code_review\reviewer_B
>type notes.txt