• Magdalena Konkiewicz

Terminal for Data Scientists





If you are have not been using the command-line tool yet, it is time to start.


This article presents the first commands to get you started on this journey.


You will learn how to navigate through folders and files, and modify them without opening any editing software or using a graphical user interface.


This will make you a more productive and better Data Scientist.



Lunch your terminal window



As you can see I keep my terminal handy in Dock of my Mac, so I can lunch it from there.


Once the application is open you should see the Terminal window similar to the one below. It should open in the current userspace. My user here is called 'konki'.



This is the window where you will be typing your commands!!!


***Note that the commands I will be showing here are valid for Mac and other Unix operating systems. If you are using Windows Command Prompt most of these commands should work but other ones may need some alternation.



1. pwd (print working directory)


The first command will show you the current directory that you are in. Just type 'pwd' as shown below:


konkis-MacBook-Air:~ konki$ pwd

/Users/konki

You can see that I am now in '/Users/konki' directory.



2. ls (list)


This command list all files in a directory. Let's have a look.



konkis-MacBook-Air:~ konki$ ls

Applications Downloads Music  cd
Desktop  Library  Pictures seaborn-data
Documents Movies  Public

You can see I have eleven directories and no files (if I had some files you would see names with some extensions. e.g .txt. or .py)


You can use ls with -a parameter to show hidden files:



konkis-MacBook-Air:~ konki$ ls -a

.   .ipython  Downloads
..   .jupyter  Library
.CFUserTextEncoding .matplotlib  Movies
.DS_Store  .pgAdmin4.startup.log Music
.Trash   .pgadmin  Pictures
.astropy  Applications  Public
.bash_history  Desktop   cd
.bash_sessions  Documents  seaborn-data

It looks like I have quite a lot of hidden files. Those are the ones starting with a dot (.).

You can also inspect the content of the folder that you are not currently in by passing its relative path.


ls Desktop/

The command above should list the contents of the Desktop directory.



3. mkdir (make directory)


This command is used to create a new directory. The following line of code creates a new directory in the current folder named test.


konkis-MacBook-Air:~ konki$ mkdir test

You can use 'ls' command to check if this has been created.



4. cd (change directory)


This command is used to change directory to the one with the path given. Once you have created 'test' directory you should be able to enter into it.


konkis-MacBook-Air:~ konki$ cd test/
konkis-MacBook-Air:test konki$ pwd
/Users/konki/test

You can confirm where you are by using 'pwd' command as above.



5. touch


You can use this to create a new file. The code below creates my_file.py. and then uses 'ls' command to check the content of the directory.


konkis-MacBook-Air:test konki$ touch my_file.py
konkis-MacBook-Air:test konki$ ls
my_file.py

The file we have created is just an empty file with .py extension. We could add some python code there later on.



6. rm (remove)


We can remove files or directories with the remove command. With the file, it is enough to call this command with the filename as shown below.


konkis-MacBook-Air:test konki$ rm my_file.py 
konkis-MacBook-Air:test konki$ ls
konkis-MacBook-Air:test konki$ 

You can always confirm if the file was removed with the 'ls' command as above.


If you want to remove a whole directory you would have to add -r parameter. This would make remove command to work recursively and enter all subdirectories to delete their content as well.


rm -r <directory_name>


7. mv (move)


You can move files and directories around with the command 'mv'. You just need to specify a file or folder name you want to move and the new desired location (folder or filename).


mv <file_name> <new_location>

Let's create a new file and move it to the parent directory.


konkis-MacBook-Air:Downloads konki$ touch file.txt
konkis-MacBook-Air:Downloads konki$ mv file.txt ..

The last line moves the file.txt to the parent directory. Two dots (..) are the shortcut for the parent directory.



8. cp (copy)


You can also copy files and folders instead of moving them. The structure of the command is similar to move command. You need to specify the file you want to copy and the destination.


cp <file_name> <new_location>

Let's create a new file and copy it to the parent directory.


konkis-MacBook-Air:Downloads konki$ touch file_to_copy.txt
konkis-MacBook-Air:Downloads konki$ cp file_to_copy.txt ..

Now if you use 'ls' command you will see that the file is still in its original location.


If you go back to the parent folder with 'cd ..' and use 'ls' again you will see that 'file_to_copy.txt' is there as well. It got copied over there without deleting the original file.



9. man (manual)


This command is useful when you forgot how to use any of the commands presented above. It pulls up a manual for a particular command.



man <command>

You can test in on 'pwd' command.



man pwd 

This should open a description of the 'pwd' command as shown on the screenshot below.




You can scroll down with the arrow to access the text on the bottom and type 'q' to exit the text file.



Summary


I think this is a list of commands that I most often use while navigating my Macbook and working on projects. It saves me a lot of time and effort in comparison to doing all of these using graphical user interfaces.


Start with learning those nine and work towards using the command line on an everyday basis.




88 views0 comments

Recent Posts

See All