Data Analysts/Scientists should have a basic knowledge of Unix Commands, the goal of this post is to give some examples of how the shell commands would help them on their daily tasks. For the first examples we will consider the following eg1.csv:

ID,Name,Dept,Gender
1,George,DS,M
2,Billy,DS,M
3,Nick,IT,M
4,George,IT,M 
5,Nikki,HR,F 
6,Claudia,HR,F 
7,Maria,Sales,F 
8,Jimmy,Sales,M 
9,Jane,Marketing,F 
10,George,DS,M

Image for post

Artificial Intelligence Jobs

Examples of Basic Unix Commands

**Q: **How to print the first or the last 3 rows of the files.

## The first 
head -n 3 eg1.csv 

## The last 
tail -n 3 eg1.csv

**Q: **How to skip the first line(s) or the last line(s).

Sometimes we want to skip the first line which usually is the headers. The command is:

## it skips the first line 
tail -n +2 eg1.csv 

## it skips the last 4 lines 
head -n -4 eg1.csv
## skip first line
1,George,DS,M
2,Billy,DS,M
3,Nick,IT,M
4,George,IT,M 
5,Nikki,HR,F 
6,Claudia,HR,F 
7,Maria,Sales,F 
8,Jimmy,Sales,M 
9,Jane,Marketing,F 
10,George,DS,M

Q: How to print the whole file.

## for the whole file 
cat eg1.csv 

## the first rows - then type space more more or q to quit 
less eg1.csv

Q: How to copy a file.

cp eg1.csv copy_eg1.csv

Q: How to **rename **a file.

mv copy_eg1.csv backup_eg1.csv

**Q: **How to remove a file.

rm backup_eg1.csv

Q: How to get a list of information about files in the working directory.

ls -lh

Q: How to check free disk space.

df -h

**Q: **How to get how much space one ore more files or directories is using.

du -sh

**Q: **How can I select columns from a file.

If you want to select columns, you can use the command cut. It has several options (use man cut to explore them), but the most common is something like:

cut -f 1-2,4 -d , eg1.csv

This means “select columns 1 through 2 and columns 4, using comma as the separator”. cut uses -f (meaning “fields”) to specify columns and **-d **(meaning “delimiter”) to specify the separator.

#data-science #bash #ai #shell #unix

Basic Unix Commands for Data Scientists
1.40 GEEK