All of the code for this tutorial can be found here.
Developers use the command line to navigate through file and perform operations. Once you get used to it, it is definitely the most efficient way to access files and perform operations. Also, when you start virtual machines on the cloud, it becomes the only way of easily communicating with your instance. So, without further due, let’s learn about the terminal and basic commands to get started!
In this article, we’ll cover what is the command line, which commands we can use and we’ll go through a simple tutorial to put those commands in practice.
First, what is the command line?
It is a plain and simple text interface for your computer. It takes commands which are then passed to the OS to run.
And which commands can we use?
The usual commands are given in the table below.
Let’s now go through a small example on how to use those commands.
- Let’s see where our terminal currently is:
pwd
2. Navigate to the folder where you usually put your code (I usually have mine in a folder called `code` in your my folder)
cd path/to/your/folder/of/code`
- for me for instance, I have got a `code` folder under /Users/myusername so I can do the following
cd
cd code
Explanation:
cd
: brings me back to my root foldercd code
: changes my working directory to code
3. Go to /create a folder called command_line_tutorial
echo “Create a folder named command_line_tutorial and go to it”
if [[ -d “command_line_tutorial” ]]; then
echo “command_line_tutorial folder exists, entering it”
cd “command_line_tutorial”
else
echo “command_line_tutorial folder does not exist, creating it”
mkdir “command_line_tutorial”
cd “command_line_tutorial”
fi
Explanation:
echo
: prints the string in the terminalif [[ -d “command_line_tutorial” ]]; then
: if the folder command_line_tutorial exists, then enter itelse
: otherwise create it and enter it
4. create a file called myfile.txt
touch myfile.txt
Explanation:
touch
: creates a file
5. write ‘Hello World!’ in the above created file
echo ‘Hello World!’ > myfile.txt
Explanation:
>
: adds content to the file
6. Add a line to the above created file
echo ‘This is added to the file because of >>, otherwise > overwrites’ >> myfile.txt
Explanation:
>>
: appends the string to the file
7. create 100 files named myfile1.txt, myfile2.txt, myfile3.txt, etc. and write a line in each of them
for i in {1..100}; do echo “This is file number $i” > myfile$i.txt; done
Explanation:
for i in {1..100}; do]
: for each number between 1 and 100, do the followingecho “This is file number $i” > myfile$i.txt
: create a file named myfile1.txt, myfile2.txt, myfile3.txt, etc. and write a line in each of them
8. count the number of files in the folder
ls | wc -l
Explanation:
ls
: list all files in the current folder|
: pipe the output of the previous command to the next commandwc -l
: count the number of lines in the output of the previous command
9. grep all files that are in 90–100 range
ls | grep “myfile[9][0–9].txt”
Explanation:
— ls
: list all files in the current folder
— |
: pipe the output of the previous command to the next command
— grep myfile[9][0-9].txt
: grep all files that are in 90–100 range
10. Copy myfile.txt into a file named myfilecopy.txt
cp myfile.txt myfilecopy.txt
Explanation:
cp
: copy the file myfile.txt into myfilecopy.txt
11. remove all files that start with myfile
rm myfile*
Explanation:
rm
: remove the file myfile.txt
12. go to folder parent
cd ..
Explanation:
cd ..
go to the parent folder
13. remove folder created for this tutorial
rmdir command_line_tutorial
Explanation:
rmdir
: remove the folder command_line_tutorial
Woohoo! You now what is the command line, what are the basic commands and how to use them!
Hope you liked this article! Don’t hesitate if you have any question, or suggestions, in comments, or feel free to contact me on LinkedIn, GitHub or Twitter, or checkout some other tutorials I wrote on DS/ML best practices.