Essentials of the Linux Command Line
Learning the Basics
1 What We Will Cover
● pwd → print working (current) directory
● Directory traversal special operators
● ls → show the contents inside a directory ● cd → change directory ● mkdir → create a directory ● cp → copy files ● mv → move and rename files/directories ● rm → remove files/directories ● cat → display the contents of a file ● less → examine the contents of larger files ● nano → a basic command line text editor ● man, help, and the --help option → getting help
● Tab completion and other helpful time savers! 2 pwd → print working directory
● This command will tell you where you are in the hierarchal directory tree of Linux
● The first directory in the Linux file system is named the root directory which is denoted with a / ● Inside the root directory are loads of other files and folders that branch down
● The location of these files and directories are referred to as paths
● If you want to navigate this file system it is good to know where you are currently located on this tree and the pwd command will do just that!
3 pwd → print working directory
● Below is an example of a common directory tree and how it is structured
● When examining printed directory paths, start at the top or root directory and go downward
4 pwd → print working directory
5 Directory Shortcuts
● Directory shortcuts help when directory paths are long and/or you don’t want to type out an absolute path
● . represents your current working directory
● .. represents the parent directory (this is the directory above your current working directory)
● ~ represents the home directory for each user this is usually listed as /home/samuel
● - represents the previous directory you were in before the most recent cd 6 Directory Shortcuts
7 ls → list contents inside a directory
● The ls command by itself will list the directories and files currently housed inside your working directory → ls
● You can also specify a path you want to see the contents of, even if it is not your working directory → ls /home/samuel ● It is important to remember that not all files or directories will be visible (ex. filenames that start with a . are hidden) ● This is where special flags for the ls command come in handy 8 ls → list contents inside a directory
9 ls → list contents inside a directory ● Adding the -a flag (stands for all) to the end of the ls command will show files that start with a . → ls -a
10 ls → list contents inside a directory ● Adding -l flag (stand for long format) to the end of the ls command will show information such as size, date, owner, and permissions → ls -l
11 ls → list contents inside a directory ● Adding -t flag to the end of the ls command will show contents sorted by time modified
● Newest are listed first → ls -t
12 ls → list contents inside a directory
● Flags are also referred to options or arguments synonymously
● Flags add more functionality to the command
● Flags are not limited to the ls command
● Flags can be combined
● The order of the flags determines the order the information output is formatted in
● Most of the time the order of flags does not matter → ls -la and ls -al will have the exact same output 13 ls → list contents inside a directory
14 cd → change directory
● Before changing your working directory, it is important to differentiate absolute and relative paths in relation to the file system tree
● Absolute paths when written, start at the root directory and move downward
● Relative path is the path branching downward from your current working directory or a of directory that employs directory shortcuts
● Instead of typing the absolute path /home/samuel/Documents if your are in current working directory of /home/samuel, a relative path name is Documents ● Relative paths are convenient in that you no longer have to type long paths out by hand 15 cd → change directory
● When using the cd command you can change your working directory by either using an absolute or relative path name
● For example, you could change your working directory to your Documents folder using an absolute pathname → cd /home/samuel/Documents ● Now that we are in the /home/samuel/Documents we can use a relative pathname → cd school
16 mkdir → create a directory
● Either absolute or relative path names can be specified when choosing where your newly created directory will be placed
● In order to create new directories in addition to already existing directories you must use the mkdir command → mkdir /path/to/newdir → mkdir newdir ● The first example creates a new directory called newdir in the path /path/to ● In the second example a new directory called newdir is created in the current working directory
● If your newdir contains a space like My Documents place quotations around the name → mkdir “My Documents” 17 mkdir → create a directory
● You can even make multiple directories in one command → mkdir directory1 directory2 ● In this example, we are creating directory1 and directory2 inside the current working directory ● Use ls after mkdir to verify that your directory has been successfully created
18 mkdir → create a directory
19 mkdir → create a directory
● You can use the -p flag (stands for parent) that allows you to create nested directories (directories inside other directories) in one command
● The -p option assumes that the directories you are creating do not already exist → mkdir path/to/dir
20 mkdir → create a directory
21 cp → copy files
● Think of the cp command as an effective copy and paste that you might be accustomed to in other operating systems
● A source path is the path that contains the file you want to copy
● A destination path is the path that the copied file will be stored in
● The source and destination paths can be the same as long as you give a unique name to the file being copied inside the current directory
● You can copy one or multiple files to a destination path → cp source_path destination_path → cp /path/to/dir/file1.txt /path/to/dir/ → cp source_path1 source_path2 destination_path 22 cp → copy files
● The -r flag can be used to copy an entire directory (folder) ● The -r stands for recursively copying the directory and all the files inside it
● It is important to note that if you copy a file to a destination path that as the same file name, the file (in the destination path) will be automatically overwritten with the new file being copied
● If you want to avoid this, use the -i flag to prompt you to confirm the operation before potentially overwriting a file
● Use the ls command to verify that your files were copied correctly
23 cp → copy files
24 cp → copy files
25 mv → move and rename files/directories
● To rename a file use the mv command followed by the oldfilename and the newfilename → mv oldfilename newfilename
● → mv ~/Documents/file1 /home/samuel/Documents/file2 OR → mv /home/samuel/Documents/file1 /home/samuel/Documents/file2
● Absolute and relative path names can be used for either the oldfilepath and/or the newfilepath
● A file can be moved inside a different directory
→ mv file2 /home/samuel/Documents 26 mv → move and rename files/directories
27 mv → move and rename files/directories
● More than one file can be moved at once → mv file1 file2 /somedirectory ● mv can also be used to rename directories → mv olddirpath newdirpath
● Again, absolute and relative path names can be used for either the olddirpath and/or the newdirpath
● Remember that if you move a file or directory, this operation by default will overwrite anything with the same directory or file naming
● Use the -i flag for an interactive prompt to confirm your intended actions before the command is executed 28 mv → move and rename files/directories
29 rm → remove files/directories
● Some operating systems have a trash can or recycle bin equivalent that holds on to “deleted” files before they are permanently destroyed
● The Linux command rm will remove files and directories, but once executed there is no way to retrieve what you might have accidentally destroyed
● Because of this, always be super careful and review what you have typed in your terminal before executing
● Just like with other important commands, the -i flag can be added on to rm to prompt the user before the command is executed 30 rm → remove files/directories
● Fortunately, some files and directories are write protected
● This is a safety measure that will prompt you for confirmation before deletion even without the -i flag
● Even with write protection, you can only remove files or directories that you have been given permission to do so
● If you want to ignore messages about write protection use the -f (force) flag to silence these messages
31 rm → remove files/directories
32 rm → remove files/directories
● To remove directories with rm you must use the -r (recursive) flag to delete the files inside the directory and then the directory itself
33 cat → display the contents of a file
● This is one of the more simpler commands
● It prints out to the console the text (contents) inside a file ● cat is short for concatenate ● Type the file path after cat → cat textfile.txt ● cat not only displays file contents but it can combine multiple files and show you the output → cat calendar.txt birthdays.txt 34 cat → display the contents of a file
● cat is usually used to view small files with small content
● It is not so great at viewing larger files 35 less → examine the contents of larger files
● Although counter-intuitive, the less command will actually show you more of the contents inside a large file
● Remember less is more
● Type the file path after less → less ~/textfile.txt
36 less → examine the contents of larger files
37 less → examine the contents of larger files
● Maneuvering in a file using less – Moving up and down the page ● page up and page down keys ● up (↑) and down (↓) arrow keys – g to go to the beginning of the file contents – G to go to the end of the file contents – h if you need additional help using less – q to leave the viewing window and exit
38 nano → a basic command line text editor
● To create a blank new file to start editing simply type nano
● From here you can start typing the contents you desire to put in your new file
● Since this option does not specify the file name from the start type Ctrl + O
● In the bottom of the screen you will be prompted to type a name for this file
● Press the Enter key to execute the operation ● Finally press Ctrl + X to return to the command line 39 nano → a basic command line text editor
● To edit an existing file or a new file with a defined file name from the start, type nano filepath
● Like before, you can start typing the contents you desire to put in your new file
● When ready to save, press Ctrl + O
● Make sure that the file name to write to is correct
● Press the Enter key to execute the operation ● Finally press Ctrl + X to return to the command line 40 nano → a basic command line text editor
41 man, help, and the --help option → getting help
● Like the manual in your car, the commands mentioned in this presentation and loads of commands have manuals
● Manual pages are often abbreviated man pages hence the name of the command to access them called man
● Manual pages help us find more information about what commands do, their options, and other important notes
● Below are just two simple examples → man ls → man cd 42 man, help, and the --help option → getting help
43 man, help, and the --help option → getting help
● Another great built-in tool that can help you use the command line interface is the help command
● Can be executed by itself to list all commands available → help ● The help command will also give you a short description and the options/flags you can use for other commands → help pwd → help mkdir ● It is common convention for developers to also include a --help option or something similar for Linux executables
44 man, help, and the --help option → getting help
45 man, help, and the --help option → getting help
46 man, help, and the --help option → getting help
47 Tab completion and other helpful time savers!
48 Tab completion and other helpful time savers!
49