Linux Terminal 1

Overview: Terminal and Bash

Terminal of Linux/Mac OS is the interface to allow user to interact with OS, just like "cmd" in Windows. Unlike Windows which use cmd.exe or Mac OS which use Z shell, Linux use a powerful shell called "Bash". We can write commands in Terminal to achieve everything we want like managing files, editing files, running a program... In fact, Bash is more than a OS management shell, it can serve as a programming language itself.

e.g. Array

~$ array=("apple" "banana" "cherry")
~$ echo ${array[1]}  # Output: banana

Loop:

~$ for i in {1..3}; do
    echo "This is iteration $i"
done
This is iteration 1
This is iteration 2
This is iteration 3

Function:

greet() {
    echo "Hello, $1!"
}
greet "John"
# Output: Hello, John

(we can indeed execute these program in Linux terminal)

 

But more commonly, we use bash to interact with Linux- the Operation System.

 

File management

Choosing ideal folder

ls: list the documents in the folder

cd: move to a specific folder

~$ ls 
Desktop  Downloads  Music Documents

We can directly list the files in one folder if we write the complete path of the folder

~ means: /home/user(e.g. Karis).

Sometimes there is no "~" in some Linux version. Anyway, we write commands after the symbol "$".

~$ ls Documents/Syllabus
Math171-004C-Fall23.pdf

or we can "cd" to the "Syllabus" folder and then "ls" the files

~$ cd Documents/Syllabus
/Documents/Syllabus$ ls
Math171-004C-Fall23.pdf

btw if we want to know the full path where we are, we use pwd

/Documents/Syllabus$ pwd
/home/user(or your name)/Documents/Syllabus

 

If we want to move back to the previous folder, use cd .. command

(e.g. from "/home/user/Documents/Syllabus" to "/home/user/Documents")

~/Documents/Syllabus$ cd ..
~/Documents$ ls
Syllabus hi.py 'I love u.pdf'

there is quotation mark(' ') in 'I love u.pdf' because the file name includes space

 

Creating/Deleting/Moving files and Directories

touch: Create an empty file.

# Create a new txt file in "Syllabus"
~$ touch ~/Documents/Syllabus/New_Syllabus.txt

~$ ls ~/Documents/Syllabus
Math171-004C-Fall23.pdf   New_Syllabus.txt

 

mkdir: Create a new directory.

# "cd" into Syllabus directory
~$ cd Documents/Syllabus
# Create two Directories called "new" and "lalala"
~/Documents/Syllabus$ mkdir new lalala 

 ~/Documents/Syllabus$ ls 
Math171-004C-Fall23.pdf   New_Syllabus.txt  new lalala

you can now "cd" into your directory "new"

~/Documents/Syllabus$ cd new
~/Documents/Syllabus/new$ touch newnewnew.txt
~/Documents/Syllabus/new$ ls
newnewnew.txt

 

To rename/move the file, we use mv command

mv old_file_name new_file_name

# Change the name of newnewnew.txt to yeah.txt
~/Documents/Syllabus/new$ mv newnewnew.txt yeah.txt
# Move yeah.txt to the "lalala" folder
~/Documents/Syllabus/new$ mv yeah.txt ~/Documents/Syllabus/lalala

 

 

 

To remove(delete) files/ directories, we use different commands

rm : Remove a file/files

rmdir Remove a directory (it should be empty or it says "Directory not empty")

rm -r Remove a directory and every file within this directory

 

So to delete everything I just created, I use the following commands:

~$ cd Documents/Syallbus
~/Documents/Syllabus$ rm New_Syllabus.txt
# Now "new" folder is empty
~/Documents/Syllabus$ rmdir new

# Since there is a file called yeah.txt in the "lalala" folder, we use "-r" to delete folder & file
~/Documents/Syllabus$ rm -r lalala
~/Documents/Syllabus$ ls
Math171-004C-Fall23.pdf

 

Conclusion

We learn ls cd pwd touch mkdir mv rm rmdir today.

We can manage our files with these simple commands.

Comments

Popular Posts