Introduction to the Command Line and Filesystem

The Linux Filesystem

If thought of like a tree there is a “Root” Directory denoted at/ . Then after the root it begins to branch out into directories or more commonly known as folders. On the branches there are more branches but also leaves which would be individual files.

Every directory and file name is CaSe SeNsItIvE.


Everything is a file

So, remember how I said that there are a difference between directories and files? Well directories are also considered files because in linux everything is still a file, but the file “tree” still is there with its different levels.

Common examples of what directories there are and what type of files are found in them.


Now its time to open a terminal!

  1. Click the ubuntu button in the top left corner
  2. Typeterminal
  3. Hitenter or click the icon labeled Terminal

If you are on a virtual machine hosted on your own computer or have a computer running ubuntu you can use the shortcutctrl + alt + T to open a terminal.


What can you do with the Terminal?


Basic Navigation in the Terminal

pwd

pwd stands for “Present Working Directory”. It prints the current directory. In my personal experience it is barely used due to the current working directory being shown on the cursor of the terminal.

ls

ls stands for “List Segments” if it is run with no options will list all non hidden files or directories inside of the working directory. If you put a directory after the command it will run in that directory.

cd

cd stands for “change directory”. Most of the time you will be running cd with a directory as an option to brings you to the directory listed. Both cd and ls can accept both relative file paths or absolute paths.

Paths

Absolute paths start from the root directory/ an example /home/cyberpatriot/Music

Relative paths start from the current working directory./ and example ./Music . Relative paths can also have two dots to signal the directory above ../John/ .


Basic Navigation hands on if possible

This basic list of commands will give you a bit of a taste of the behavior of these commands do. (The text after the pound sign# are comments that tell you what each command will be doing.)

cd #Change directory to home ~
pwd #Prints the current directory
ls #Prints whats inside the current directory
cd ./Music #Changes directory to ~/Music
pwd #Prints the current directory
ls #Prints whats inside the current directory
cd ../Documents #Changes the directory to ~/Documents
pwd #Prints the Current Working directory
cd #Change directory to home ~
pwd #Prints the Current Working directory

Manuals and Command usage

Theman command is a vital tool when learning linux systems. These contain manuals that are published with most commands used. This documentation shows every option that there is to configure about a command.

Try runningman man now to take a look at the manual for man. To quit out of the manual hit Q .

Most commands also have a-h or --help command which give a brief explanation of what options exist and if the option requires any arguments (other inputs).

Try runningls --help now to take a look at the help summary for ls

Think about what the-1 option will do on ls then try it yourself.


File Manipulation Commands

cp

cp stands for “CoPy”. This copies a file or directory to a new location. It can copy from many sources and into one destination by listing multiple sources before the destination.

mv

mv stands for “MoVe”. This is used to move or rename a file or directory. Just like cp it can copy from many sources and into one destination by listing multiple sources before the destination.

touch

touch touches (open and immediately close it) a file and can create empty files if the file name specified does not exist.

rm

rm stands for “ReMove”. This command removes a file (or directory with the right options). rm can remove multiple files or directories at the same time by having multiple paths after rm


Basic file manipulation practical

This basic list of commands will give you a bit of a taste of the behavior of these commands do. (The text after the pound sign# are comments that tell you what each command will be doing.)

cd #Changes directory to home
touch Documents/a #Creates the file a inside Documents
cp Documents/a b #Copys the file a to the file b
mv b c #Moves file b into file b
ls ./Documents #Prints the files inside the Documents directory
rm c ./Documents/a #Removes the files c and and a from the
ls ./Documents #Prints the files inside the Documents directory

File Contents and Output Redirection

In linux there is the ability to redirect the output of a command to be sent to a file via the file redirection

cd #Changes your directory to your home directory
echo "Hello" > hello #Prints hello to the file hello
echo "World!" > world #Prints world to the file world
ls #Prints the files inside the Documents directory
cat hello world > helloworld #Prints the two files hello and world to the file helloworld
ls #Prints the files inside the Documents directory
cat helloworld #Prints the file to the terminal
file helloworld #Prints the file type of the file helloworld
file Music/4.mp3 #Prints the file type of the file 4.mp3
rm hello world helloworld #Removes the files created during the lesson

File editing

gedit

gedit is the easiest text editor on ubuntu. It is a graphical program and allows you to edit text files. To open a file typegedit then the file name.

Try runninggedit Documents/napier.txt now to take a look at the file napier.txt . You can exit by hitting the red x in the left corner.

nano

nano is the easiest terminal text editor on ubuntu. It is a text only program and allows you to edit text files. To open a file typenano then the file name.

Try runningnano Documents/napier.txt now to take a look at the file napier.txt . You can exit by hitting ctrl + x and following th prompts.


Small Practical on file editing

#This makes a folder
sudo mkdir /etc/lightdm/lightdm.conf.d
#Output redirection to make a config file
sudo sh -c 'printf "[SeatDefaults]\nallow-guest=false\n" > /etc/lightdm/lightdm.conf.d/50-no-guest.conf'
#Change directory to to the lightdm config area
cd /etc/lightdm/lightdm.conf.d/
#opens the config file that was just made in nano
sudo nano 50-no-guest.conf