Essentials of the Linux Command Line
Total Page:16
File Type:pdf, Size:1020Kb
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 old!ilename and the new!ilename → 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 old!ilepath and/or the new!ilepath ● 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 -! (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 te"t!ile.t"t ● cat not only displays file contents but it can combine multiple files and show you the output → cat calendar.t"t #irthdays.t"t 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