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.

/bin

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

Package managers: npn, pip, yarn

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

nano

Apt install nano  (error: unable to locate package …)

See all packages (apt list)

# apt update

//update all of the sources and package manager list database

Run

# apt install nano

(should install correctly)Before installing, run apt update, then install the package.

Ctrl + L will clear the window

# apt remove nano 

// will remove the nano directory

Linux File System

Everything is a File in Linux | Linux Directory List

/ – root directory on top

Bin – includes binaries or programs

Boot – includes files for booting

Dev – short for devices, everything is a file, so to access files, they store here

Etc – Editable Text Configuration / where the config files are stored

Home – home dir for users are stored (each user has a dir)

Root – the home dir for the root user. Only the root user can access this directory

Lib – keeping library files (software library dependencies) 

Var – files that are updated frequently (lock files, app data, etc.)

Proc – includes files that represent running processes

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 (will show one list item per line)

# ls -l (will show additional information about the item)

# cd (change directory) type relative path // bin, boot

# cd (an absolute path always starts from root) cd etc/apt/ 

# cd .. (go one level up, or back, with this command)

# cd ../.. (go two levels up)

Look at content without navigating to the dir

# ls bin 

// you will see all the files within the bin directory

Shortcut: 

# cd /root  –>

# cd ~ (will take you to your home directory no matter where you are)

Manipulating Files and Directories

# cd ~ (go home)

~# mkdir (create directory) # mkdir test

~# ls

test

~# mv (rename or move files) ~# mv test docker

~# ls

docker

Using touch and MV

~/docker# touch hello.txt (touch creates a new file)

# ls

docker.txt

# touch file1.txt file2.txt file3.txt (create multiple files at a time)

# ls -1 (show one per line)

# mv hello.txt hello-docker.txt (rename)

# mv hello.txt /etc (move to another directory)

// ctrl + w will remove an entire section of the command // mv hello.txt hello

Remove one or more files

# rm (command to remove files)

# 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. This will remove the file and its contents) 

# rm -r docker/

# ls (verify that the pwd is empty)

Manipulating Files and Directories

Nano (it is a basic text editor for the linux terminal)

# nano file1.txt

//create content within fileHello
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 (concatenate the information on the terminal) / you can also use this to combine files together

# cat file1.txt
Hello
World

When dealing with long files:

# more /etc/adduser.conf (will let you see parts at a time. Press Space to move to next page. You can use ENTER to go down. #q (exit from more) You can not go up, only down. So we use less

# less (install: apt install less // new command to replace more)

# less /etc/adduser.conf  (scroll down or up with the arrow keys. SPACE and ENTER work the same as the more command.)

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
Hello
World
# cat file1.txt > file2.txt  (> symbol for redirection of output)
// will take content from file1 and write file2

Redirection Operator to combine multiple files

~# cat file1.txt file2.txt
Hello
World
Hello
World

~# cat file1.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 hello
Hello
# echo hello > hello.txt

Echo can write content to a file without using Nano

# echo whatever > whatever.txt 

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

Searching for Text in Files

~# grep hello file1.txt
— Will show nothing because of case sensitivity. To remove this, use:

~# grep -i hello file1.txt (case insensitive)
Hello

~# grep -i root /etc/passwd (contains list of user accounts)

Search in multiple files:
~# grep -i hello file1.txt file2.txt  or # grep -i hello file*
file1.txt:Hello
file2.txt:Hello

~# grep -i -r hello . (Will search this directory and then subdirectories recursively)

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

Finding Files and Directories

# find (command for finding files and directories)
–When used by itself, it will list all files and directories recursively and list them

./. = represents a hidden file. These will not show if you type the ls command

# ls -a (option to show all files including hidden files which are excluded by default)

# find + path will let you search other files and directories on the pc

# find -type d (show only files in current dir)

# find -type f -name “f*” (search is case sensitive) case insensitive, use -inameFind all Python files and write to txt
# find / -type f -name “*.py” > python-files.txt

Chaining Commands

# mkdir test;cd test;echo done  (All commands are executed one after another)

// you can use spaces between the ; if you want. Doesn’t matter

Stop commands if one fails
# 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.)

# mkdir test || echo “directory exists” (Either or operator for commands)

Chaining commands using piping

# ls /bin | less (create a list of the /bin directory and pipe it to the less command. Less gets input from first command and doesn’t need its own file name) 

You can use a lot of other commands after the | 

Split into multiple lines:

# mkdir hello ; cd hello ; echo done ……. (To avoid scrolling to the side use)# mkdir hello;\
> cd hello;\
> …
> echo done;

Environment Variables (Major Area of Confusion)

~# printenv (see all env variables on the machine) HOSTNAME=2f759e6996e9
PWD=/root
HOME=/root
LS_COLORS=rs=0: …..

PATH=/usr/local/sbin:/….  (Commands need to be added to path. The OS will look at the path variable for the machine to run commands and code. If the command gives a path error, it is not added to PATH.)

More Environment Commands

~# echo $PATH  ($ represents and env variable)

~# export DB_USER=mosh

~# echo $DB_USER
Mosh

>> This variable is only available in the current terminal session

~# exit (will terminate the container/session in terminal

Leave a Reply

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