The Linux Command Line

The Linux Command Line

Linux is an open source operating system which is available to anyone. For most coders and developers, learning Linux CLI will help them as they work to develop cross platform applications and access development tools. Here is everything you need to know about using the Linux CLI so you can execute code on Linux based systems and apps.

Understanding and Using the File System

Linux Directories Explained


Born in shell (enhanced version of the shell)

/ to separate directories

Linux is case sensitive in terminal (applicable everywhere!)

Arrows let you go up and down to repeat commands Up or Down

  • History command will show you the command
  • !2 will execute the 2nd command in history list

Managing Packages

Popular Package Managers Include: npm for JavaScrip and Node, pip for Python, brew for homebrew, & yarn.

Apt is the package manager for Linux  (apt-get also exists)

Navigating the File System

$  pwd (print working directory) - see where you are in the file system
$  ls (list) - see the files in the pwd
$  ls -1 (Number One) - will show one list item per line
$  ls -l (letter L) - will show additional information about the item
$  cd (change directory) + relative path / absolute path
     Example: $  cd Desktop  (relative path)
              $  cd Documents/codingExamples/linux  (absolute path)
$  cd .. (go one level up, or back, with this command)
$  cd ../.. (go two levels up)

View Content in Another Directory

$  ls bin
     This will show you all files within the bin directory
$  cd /root 
     changes directory to specified path
$  cd ~ (user root) - go to your home directory no matter where you are

Manipulating Files and Directories

$  cd ~
$  mkdir (create directory) 
$  mkdir test
$  ls
$  mv (rename directory
$  mv test documents
$  ls
$  cd documents

Using the touch and mv Commands


Create a new file using touch
$  touch hello.txt

Create multiple files at once
$  touch file1.txt file2.txt file3.txt
$  ls
     hello.txt   file1.txt   file2.txt

Rename the file with mv
$  mv hello.txt hello-world.txt 

Move file to another directory
$  mv hello-world.txt/etc

Remove one or more files

Delete or remove files
$  rm 
$  rm file1.txt file2.txt 
     rm file* (delete all files starting with file)

rm: cannot remove ‘docker/’: Is a directory  (you can not delete dir with rm)

$  rm -r (-r means recursively and will remove both the file and its contents) 
$  rm -r docker/
$  ls (verify that the pwd is empty)

Manipulating Files and Directories

Nano is the basic file editor in Linux and can be installed with the following command:

apt install nano

Use Nano:

installing nano on your system:

$  sudo apt update

$  apt install nano
Remove Nano command:
$  apt remove nano  

Open file for editing:

$  nano file1.txt

Create content within the new file

hello World

Save and Exit ^X (ctrl + x) && Y (save buffer)

File Name to Write: file1.txt (keep or change)

View the Content of the File in Terminal Window

$  cat 
>> will concatenate the information on the terminal & can be used to combine files.
$  cat file1.txt

Easy accessing content in terminal when dealing with long files:

$  more /etc/adduser.conf 
>> will let you see parts of the content at a time. Use the space key to move to the next page. Enter will let you navigate down. $q will exit. 

$  apt install less
$  less /etc/adduser.conf 
>> will let you scroll up or down with the arrow keys.

Additional Commands for viewing parts of content

$  head  (used to display the first few lines)
$  head -n 5 /etc/adduser.conf

$  tail  (used to display the last few lines)
$  tail -n 5 /etc/adduser.conf

Redirection | Concepts of Standard Input Output / IO

Standard Input – represents the keyboard

Standard Output – represents the terminal window or screen

Change the source of the input and output using Redirection //# cat file1.txt

$  cat file1.txt

Write content from one file to another using the cat command and > character

$  cat file1.txt > file2.txt 

Redirection Operator to combine multiple files

$  cat file1.txt file2.txt
$  cat file.txt file2.txt > combined.txt

The Redirection Operator is not limited to the cat command. It can be used practically anywhere.

Redirection Operator Examples

Echo can write content to a file without using Nano

$  echo hello
$  echo hello > hello.txt

$  echo whatever > whatever.txt
$  ls -l/etc > files.txt 
>> This will create a list of items and write to a .txt file

Searching for Text in Files

$  grep hello file1.txt
>> This will show nothing because of case sensitivity.

To remove case this, use:

$  grep -i hello file1.txt

View list of user accounts:

$  grep -i root /etc/passwd

Search in multiple files:

$  grep -i hello file1.txt file2.txt
$  grep -i hello file*

Search this directory and then subdirectories recursively

$  grep -i -r hello

Linux will let you combine options. Thus: -i -r == -ir

Finding Files and Directories

Use the command to find files and directories:

$  find 
$  find ./        (hidden file that will not display with the ls command)
$  ls -a          (option to show all files, including hidden files)
$  find + path    (Search other files and directories) 
$  find -type d   (show only files in current directory)
$  find -type f -name "f*" 

Search for files of a specific extension type, case insensitive, with the following command:

$  find / -type f -name "*.py" > python-files.txt 

Chaining Commands

$  mkdir test
$  cd test
$  echo done  

Chain commands so all commands are executed one after another:

$  mkdir test;cd test;echo done

Note: you can use spaces between the ; if you want. Doesn’t matter during execution.

How to stop the process if one of your commands fail:

$  mkdir test && cd test && echo done

If the first command fails, the following will not be executed. Unlike the ; which will execute the other commands if one fails.

Use || as the “Either Or” operator for commands:

$  mkdir test || echo "directory exists"

This way, if the first command fails, the second command will execute to alert you to the failure.

Chaining commands using piping

Create a list of the /bin directory and pipe it to the less command.

$  ls /bin | less

Less gets input from first command and doesn’t need its own file name. You can use a lot of other commands after the | while working in your terminal, for example:

Split into multiple lines:

$  mkdir hello ; cd hello ; echo done ......

or split lines with \

$  mkdir hello;\
 >  cd hello;\
 >  echo done;

Environment Variables (Major Area of Confusion)

$  printenv  (see all env variables)
LS_COLORS=rs=0: ....

Manny commands need to be added to path when the code package, software, etc. is installed on your system. For example, when you install Python3, to use the code, you must add the python3 variable to your path.

The OS will look at the path variable for the machine to run commands and code. If the command gives a path error despite the code being installed, it is not added to PATH.

More Environment Commands

$  echo $PATH  ($ = env variable)
$  export DB_USER=alizarin
$  echo $DB_USER

When used, this variable will only available in the current terminal session, and will not be saved.

$  exit 

The exit command will terminate the container/session in terminal.

Want to Try Linux?

If you are interested in trying out Linux, check out our tutorial for using Linux on your Windows 10 Computer. This is an easy way to get started without setting up an entirely new OS on your computers hardware.

Blog Article: Run Linux on Windows

Leave a Reply

Your email address will not be published. Required fields are marked *