Chapter 1 - Intro to Linux



AD

1.1 Introduction to Linux Commands
Linux is a stable, hardware efficient operating system. This is an introduction to Linux command line. I will spend the first part showing you how to work with files and directories. This is crucial to learning Linux. After that we will cover other commands that a person needs to know. I have only used two dozen commands. There are literally hundreds. But you can do a lot with these two dozen commands.

1.2 File structure
The Linux file system is composed of files, and directories or folders. Folders and directories are the same thing. The top directory is called the / directory or root directory. Below it all other directories. Files branch off of directories. The directory we are primarily concerned with is the home directory where the directory steve (my user name) resides. We would describe the location of steve as /home/steve. The first slash is the root directory, and the second slash is a separator. Notice that steve directory is inside the home directory, and the home directory is inside the root (or / directory) directory. I like to think of the directories as being above and below each other, as in the schematic below.



Path for root, files, and directories.

Path names for the above include:

/home/steve
/home/steve/directory1
/home/steve/file1.txt
/home/steve/file2.txt

Directory and file names are different colors in the command line. This makes it easier to tell the difference between the two. Note: Linux file and directory names are case sensitive. It makes a difference if something is or is not capitalized. Also, there are no spaces in file or directory names.



Common Linux commands you should know.

1.3 Practice working with files and directories using pwd, ls, mkdir, touch, cd, cd.., ls -al, rm, rm -r

1.3.1 Open a command line terminal with Ctrl + Alt + t. The prompt at the command line is represented with a $ sign. Press Enter after typing a command. Each Output is listed below.

1.3.2 Print out the directory you are in with pwd (print working directory)

pwd

Output:
(steve is the default directory for the command line. It is also called the home directory. On your computer it will be your username.)
/home/steve/

1.3.3 Create a file with touch

touch file1.txt

Output:
There is no output, which means the command worked.

1.3.4 Check to see if file1.txt was created by using the ls command. This command lists all files and directories in the current directory.

ls

Output:
(All files and folders in current directory including:) test1.txt

1.3.5 Make directory called directory1 with the mkdir command

mkdir directory1

Output:
There is no output, which means the command worked.

1.3.6 Check to see if directory1 was created by using the ls command.

ls

Output:
(All files and folders in current directory including:) directory1

1.3.7 Use the cd directory1 command to go down to directory1.

cd directory1

Output:
You will see the prompt change to include directory1.

1.3.8 Use the pwd command again to see which directory you are in. It has changed from /home/steve to /home/steve/directory1

pwd

Output:
/home/steve/directory1

1.3.9 Now use the cd .. command to go up one directory level to /home/steve

cd ..

Output:
(The prompt changes back. directory1 is no longer in the prompt.)

1.3.10 We will now remove the file file1.txt from the steve directory using rm command

rm file1.txt

Output:
There is no output, which means the command worked.

1.3.11 Check to see if file1.txt was deleted by using the ls command. This command lists all files and directories in the current directory.

ls

Output:
(All files and folders are listed except for file1.txt)

1.3.12 We will now remove the directory directory1 from the steve directory using rm -r command

rm -r directory1

Output:
There is no output, which means the command worked.

1.3.13 Check to see if directory1 was deleted by using the ls command. This command lists all files and directories in the current directory.

ls

Output:
(All files and folders are listed except for directory1)

1.3.14 The ls -al command is similar to to the ls command. The ls command shows files and directories. The ls -al command shows the same files and directories plus files and directories that start with a period. Files an directories that start with a period are sometimes called hidden files and directories. It also displays the properties of the files in the directory.

ls -al

Output:
(Shows all files and directories in the current directory.)

1.4 The up arrow, down arrow, and tab keystrokes

1.4.1 The command line keeps a history of the commands entered at the command line. Pressing the up arrow takes you to the previous command. Do not worry, the previous command will not be performed unless you press Enter.

1.4.2 Pressing the down arrow takes you down the list of commands until you reach the current command. Most of the time you will be using up arrow to see previous commands.

1.4.3 Press tab to auto complete the directory or file you are typing. File and directory path names get long in Linux. Use the tab key to autocomplete the path name.

1.5 Sudo

1.5.1 Some commands require root user privileges to run. One example is the updatedb command described in section 1.6.2. The sudo command allows you to run a command as root. It will prompt you for your password after pressing enter.

sudo command

Output: (You will be prompted for your password.)

Output:(The command will be performed.)

1.6 Locate and Updatedb

1.6.1 The locate command is used to find files and path names with the portion of a name that you type in.

locate filename

Output:
(Output is a list of file names and paths that match your input.)

1.6.2 Nightly your computer updates the database of file names and paths using the command updatedb. To update this database manually type the sudo updatedb command.

sudo updatedb

Output: (You will be prompted for your password.)

Output: (There is no output. It takes a minute or two to run. Be patient.)

1.7 A brief introduction to the Nano text editor

1.7.1 The nano text editor is a simple editor to create and modify files. We will not go into detail. A more detailed tutorial is at https://www.thesurfdragon.com/conda/ch3/index.html .

1.7.2 Type the command nano with the name of a file you wish to create.

nano filename.txt

Output: (The nano text editor will be displayed.)

1.7.3 Type in some text.

1.7.4 Press Ctrl + x

Output:
Save modified butter?

1.7.5 Press y.

Output:
File Name to Write: filename.txt

1.7.6 Press Enter.

Output:
(You exit the editor and return to command line.)

1.7.7 Enter the command ls to see the filename.txt in your current directory.

1.8 cat, head, tail

1.8.1 The command cat lists out the contents of a file for viewing, but it is not a text editor.

cat filename.txt

Output:
(Displays the contents of filename.txt)

1.8.2 The command head displays the top lines of a file.

head filename.txt

Output:
(Displays the top lines of filename.txt)

1.8.3 The command tail displays the bottom lines of a file.

tail filename.txt

Output:
(Displays the bottom lines of filename.txt)

1.9 cp, mv

1.9.1 The command cp copies a file from one directory to another. The cp command works on directories also. Be careful with the command. If the file already exists, it will be over written. For example. Copy filename.txt to directory2 .

1.9.2 First create filename2.txt . Second create directory2 .

touch filename2.txt

mkdir directory2

1.9.3 Copy filename.txt to directory2.

cp filename.txt directory2

>

1.9.4 Change directories to directory2 .

cd directory2

1.9.5 List the contents of directory2

ls

Output:
filename2.txt

1.9.6 Change back to the home directory.

cd ..

1.9.7 The command mv renames a file or directory. It can also be used to move a file or directory. We will rename filename2.txt to filename3.txt .

mv filename2.txt filename3.txt

Output: (There is no output. The command worked.)

ls

Output: (filename3.txt will be listed along with your other files and directories.)

1.10 curl

1.10.1 The command curl gets or sends data over the internet using URL syntax. It is used to download files from web pages to the command line. I just want you to know what it is.

1.11 tar

1.11.1 The tar command is used to compress and uncompress files. Most commonly you will uncompress or extract program files dowloaded over the internet. The command to uncompress and extract files is tar -xzf name.tar.gz. To uncompress and extract files to a specific directory use tar -xzf name.tar.gz -C /path/to/directory

1.12 Ctrl + c

1.12.1 Once the command line is open, you may need to terminate a process. Press Ctrl + c to stop the program.





theSurfDragon.com


Linux Navigation

Table of Contents
Ch1-Basic
Ch2-Advanced
Ch3-Downloading
Ch4-Screenshots