Essentials of the Linux Command Line

Total Page:16

File Type:pdf, Size:1020Kb

Essentials of the Linux Command Line 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
Recommended publications
  • Windows Command Prompt Cheatsheet
    Windows Command Prompt Cheatsheet - Command line interface (as opposed to a GUI - graphical user interface) - Used to execute programs - Commands are small programs that do something useful - There are many commands already included with Windows, but we will use a few. - A filepath is where you are in the filesystem • C: is the C drive • C:\user\Documents is the Documents folder • C:\user\Documents\hello.c is a file in the Documents folder Command What it Does Usage dir Displays a list of a folder’s files dir (shows current folder) and subfolders dir myfolder cd Displays the name of the current cd filepath chdir directory or changes the current chdir filepath folder. cd .. (goes one directory up) md Creates a folder (directory) md folder-name mkdir mkdir folder-name rm Deletes a folder (directory) rm folder-name rmdir rmdir folder-name rm /s folder-name rmdir /s folder-name Note: if the folder isn’t empty, you must add the /s. copy Copies a file from one location to copy filepath-from filepath-to another move Moves file from one folder to move folder1\file.txt folder2\ another ren Changes the name of a file ren file1 file2 rename del Deletes one or more files del filename exit Exits batch script or current exit command control echo Used to display a message or to echo message turn off/on messages in batch scripts type Displays contents of a text file type myfile.txt fc Compares two files and displays fc file1 file2 the difference between them cls Clears the screen cls help Provides more details about help (lists all commands) DOS/Command Prompt help command commands Source: https://technet.microsoft.com/en-us/library/cc754340.aspx.
    [Show full text]
  • Modern Programming Languages CS508 Virtual University of Pakistan
    Modern Programming Languages (CS508) VU Modern Programming Languages CS508 Virtual University of Pakistan Leaders in Education Technology 1 © Copyright Virtual University of Pakistan Modern Programming Languages (CS508) VU TABLE of CONTENTS Course Objectives...........................................................................................................................4 Introduction and Historical Background (Lecture 1-8)..............................................................5 Language Evaluation Criterion.....................................................................................................6 Language Evaluation Criterion...................................................................................................15 An Introduction to SNOBOL (Lecture 9-12).............................................................................32 Ada Programming Language: An Introduction (Lecture 13-17).............................................45 LISP Programming Language: An Introduction (Lecture 18-21)...........................................63 PROLOG - Programming in Logic (Lecture 22-26) .................................................................77 Java Programming Language (Lecture 27-30)..........................................................................92 C# Programming Language (Lecture 31-34) ...........................................................................111 PHP – Personal Home Page PHP: Hypertext Preprocessor (Lecture 35-37)........................129 Modern Programming Languages-JavaScript
    [Show full text]
  • Mac Keyboard Shortcuts Cut, Copy, Paste, and Other Common Shortcuts
    Mac keyboard shortcuts By pressing a combination of keys, you can do things that normally need a mouse, trackpad, or other input device. To use a keyboard shortcut, hold down one or more modifier keys while pressing the last key of the shortcut. For example, to use the shortcut Command-C (copy), hold down Command, press C, then release both keys. Mac menus and keyboards often use symbols for certain keys, including the modifier keys: Command ⌘ Option ⌥ Caps Lock ⇪ Shift ⇧ Control ⌃ Fn If you're using a keyboard made for Windows PCs, use the Alt key instead of Option, and the Windows logo key instead of Command. Some Mac keyboards and shortcuts use special keys in the top row, which include icons for volume, display brightness, and other functions. Press the icon key to perform that function, or combine it with the Fn key to use it as an F1, F2, F3, or other standard function key. To learn more shortcuts, check the menus of the app you're using. Every app can have its own shortcuts, and shortcuts that work in one app may not work in another. Cut, copy, paste, and other common shortcuts Shortcut Description Command-X Cut: Remove the selected item and copy it to the Clipboard. Command-C Copy the selected item to the Clipboard. This also works for files in the Finder. Command-V Paste the contents of the Clipboard into the current document or app. This also works for files in the Finder. Command-Z Undo the previous command. You can then press Command-Shift-Z to Redo, reversing the undo command.
    [Show full text]
  • 101 Useful Linux Commands - Haydenjames.Io
    101 Useful Linux Commands - haydenjames.io Some of these commands require elevated permissions (sudo) to run. Enjoy! 1. Execute the previous command used: !! 2. Execute a previous command starting with a specific letter. Example: !s 3. Short way to copy or backup a file before you edit it. For example, copy nginx.conf cp nginx.conf{,.bak} 4. Toggle between current directory and last directory cd - 5. Move to parent (higher level) directory. Note the space! cd .. 6. Go to home directory cd ~ 7. Go to home directory cd $HOME 8. Go to home directory (when used alone) cd 9. Set permissions to 755. Corresponds to these permissions: (-rwx-r-x-r-x), arranged in this sequence: (owner-group-other) chmod 755 <filename> 10. Add execute permission to all users. chmod a+x <filename> 11. Changes ownership of a file or directory to . chown <username> 12. Make a backup copy of a file (named file.backup) cp <file> <file>.backup 13. Copy file1, use it to create file2 cp <file1> <file2> 14. Copy directory1 and all its contents (recursively) into directory2 cp -r <directory1> <directory2>/ 15. Display date date 16. Zero the sdb drive. You may want to use GParted to format the drive afterward. You need elevated permissions to run this (sudo). dd if=/dev/zero of=/dev/sdb 17. Display disk space usage df -h 18. Take detailed messages from OS and input to text file dmesg>dmesg.txt 19. Display a LOT of system information. I usually pipe output to less. You need elevated permissions to run this (sudo).
    [Show full text]
  • This Document Explains How to Copy Ondemand5 Data to Your Hard Drive
    Copying Your Repair DVD Data To Your Hard Drive Introduction This document explains how to copy OnDemand5 Repair data to your hard drive, and how to configure your OnDemand software appropriately. The document is intended for your network professional as a practical guide for implementing Mitchell1’s quarterly updates. The document provides two methods; one using the Xcopy command in a DOS window, and the other using standard Windows Copy and Paste functionality. Preparing your System You will need 8 Gigabytes of free space per DVD to be copied onto a hard drive. Be sure you have the necessary space before beginning this procedure. Turn off screen savers, power down options or any other program that may interfere with this process. IMPORTANT NOTICE – USE AT YOUR OWN RISK: This information is provided as a courtesy to assist those who desire to copy their DVD disks to their hard drive. Minimal technical assistance is available for this procedure. It is not recommended due to the high probability of failure due to DVD drive/disk read problems, over heating, hard drive write errors and memory overrun issues. This procedure is very detailed and should only be performed by users who are very familiar with Windows and/or DOS commands. Novice computers users should not attempt this procedure. Copying Repair data from a DVD is a time-consuming process. Depending on the speed of your processor and/or network, could easily require two or more hours per disk. For this reason, we recommend that you perform the actual copying of data during non-business evening or weekend hours.
    [Show full text]
  • “Linux at the Command Line” Don Johnson of BU IS&T  We’Ll Start with a Sign in Sheet
    “Linux at the Command Line” Don Johnson of BU IS&T We’ll start with a sign in sheet. We’ll end with a class evaluation. We’ll cover as much as we can in the time allowed; if we don’t cover everything, you’ll pick it up as you continue working with Linux. This is a hands-on, lab class; ask questions at any time. Commands for you to type are in BOLD The Most Common O/S Used By BU Researchers When Working on a Server or Computer Cluster Linux is a Unix clone begun in 1991 and written from scratch by Linus Torvalds with assistance from a loosely-knit team of hackers across the Net. 64% of the world’s servers run some variant of Unix or Linux. The Android phone and the Kindle run Linux. a set of small Linux is an O/S core programs written by written by Linus Richard Stallman and Torvalds and others others. They are the AND GNU utilities. http://www.gnu.org/ Network: ssh, scp Shells: BASH, TCSH, clear, history, chsh, echo, set, setenv, xargs System Information: w, whoami, man, info, which, free, echo, date, cal, df, free Command Information: man, info Symbols: |, >, >>, <, ;, ~, ., .. Filters: grep, egrep, more, less, head, tail Hotkeys: <ctrl><c>, <ctrl><d> File System: ls, mkdir, cd, pwd, mv, touch, file, find, diff, cmp, du, chmod, find File Editors: gedit, nedit You need a “xterm” emulation – software that emulates an “X” terminal and that connects using the “SSH” Secure Shell protocol. ◦ Windows Use StarNet “X-Win32:” http://www.bu.edu/tech/support/desktop/ distribution/xwindows/xwin32/ ◦ Mac OS X “Terminal” is already installed Why? Darwin, the system on which Apple's Mac OS X is built, is a derivative of 4.4BSD-Lite2 and FreeBSD.
    [Show full text]
  • Dell EMC Powerstore CLI Guide
    Dell EMC PowerStore CLI Guide May 2020 Rev. A01 Notes, cautions, and warnings NOTE: A NOTE indicates important information that helps you make better use of your product. CAUTION: A CAUTION indicates either potential damage to hardware or loss of data and tells you how to avoid the problem. WARNING: A WARNING indicates a potential for property damage, personal injury, or death. © 2020 Dell Inc. or its subsidiaries. All rights reserved. Dell, EMC, and other trademarks are trademarks of Dell Inc. or its subsidiaries. Other trademarks may be trademarks of their respective owners. Contents Additional Resources.......................................................................................................................4 Chapter 1: Introduction................................................................................................................... 5 Overview.................................................................................................................................................................................5 Use PowerStore CLI in scripts.......................................................................................................................................5 Set up the PowerStore CLI client........................................................................................................................................5 Install the PowerStore CLI client..................................................................................................................................
    [Show full text]
  • Powerview Command Reference
    PowerView Command Reference TRACE32 Online Help TRACE32 Directory TRACE32 Index TRACE32 Documents ...................................................................................................................... PowerView User Interface ............................................................................................................ PowerView Command Reference .............................................................................................1 History ...................................................................................................................................... 12 ABORT ...................................................................................................................................... 13 ABORT Abort driver program 13 AREA ........................................................................................................................................ 14 AREA Message windows 14 AREA.CLEAR Clear area 15 AREA.CLOSE Close output file 15 AREA.Create Create or modify message area 16 AREA.Delete Delete message area 17 AREA.List Display a detailed list off all message areas 18 AREA.OPEN Open output file 20 AREA.PIPE Redirect area to stdout 21 AREA.RESet Reset areas 21 AREA.SAVE Save AREA window contents to file 21 AREA.Select Select area 22 AREA.STDERR Redirect area to stderr 23 AREA.STDOUT Redirect area to stdout 23 AREA.view Display message area in AREA window 24 AutoSTOre ..............................................................................................................................
    [Show full text]
  • Exercise I: Basic Unix for Manipulating NGS Data
    Exercise I: Basic Unix for manipulating NGS data C. Hahn, July 2014 The purpose of this exercise is to practice manipulating NGS (Next Generation Sequencing) data using simple but incredibly powerful Unix commands. Try to solve the below tasks using your Unix skills. Do not hesitate to consult Google for help!! Hints for every task can be found on page 3 below. Possible solutions are suggested on page 4. 1. Download the testdata from https://www.dropbox.com/s/wcanmej6z03yfmt/testdata_1.tar?dl=0. Create a directory (exercise_1) in your home directory and copy the archive testdata.tar from ~/Downloads/ to this directory. 2. Decompress the archive. 3. Examine the content of the data files. For basic information on fastq format please visit: http://en.wikipedia.org/wiki/FASTQ_format. With this information try to interpret the content of your data files. Do you Know what all the lines represent? a) Which quality encoding (which offset) do these files use? b) What is the header of the third read in the file? Is this a single-end (forward) or a paired-end (reverse) read? c) What is the header of the last read in the file? Is this a forward or reverse read? 4. How many lines does the file have? 5. How many reads does the file contain? 6. How many single-end (“se” also referred to as forward reads) reads and how many paired-end (“pe” also reverse reads) reads does the file contain? 7. Extract all se/pe reads and write them to separate files testdata_1.fastq and testdata_2.fastq.
    [Show full text]
  • NETSTAT Command
    NETSTAT Command | NETSTAT Command | Use the NETSTAT command to display network status of the local host. | | ┌┐────────────── | 55──NETSTAT─────6─┤ Option ├─┴──┬────────────────────────────────── ┬ ─ ─ ─ ────────────────────────────────────────5% | │┌┐───────────────────── │ | └─(──SELect───6─┤ Select_String ├─┴ ─ ┘ | Option: | ┌┐─COnn────── (1, 2) ──────────────── | ├──┼─────────────────────────── ┼ ─ ──────────────────────────────────────────────────────────────────────────────┤ | ├─ALL───(2)──────────────────── ┤ | ├─ALLConn─────(1, 2) ────────────── ┤ | ├─ARp ipaddress───────────── ┤ | ├─CLients─────────────────── ┤ | ├─DEvlinks────────────────── ┤ | ├─Gate───(3)─────────────────── ┤ | ├─┬─Help─ ┬─ ───────────────── ┤ | │└┘─?──── │ | ├─HOme────────────────────── ┤ | │┌┐─2ð────── │ | ├─Interval─────(1, 2) ─┼───────── ┼─ ┤ | │└┘─seconds─ │ | ├─LEVel───────────────────── ┤ | ├─POOLsize────────────────── ┤ | ├─SOCKets─────────────────── ┤ | ├─TCp serverid───(1) ─────────── ┤ | ├─TELnet───(4)───────────────── ┤ | ├─Up──────────────────────── ┤ | └┘─┤ Command ├───(5)──────────── | Command: | ├──┬─CP cp_command───(6) ─ ┬ ────────────────────────────────────────────────────────────────────────────────────────┤ | ├─DELarp ipaddress─ ┤ | ├─DRop conn_num──── ┤ | └─RESETPool──────── ┘ | Select_String: | ├─ ─┬─ipaddress────(3) ┬ ─ ───────────────────────────────────────────────────────────────────────────────────────────┤ | ├─ldev_num─────(4) ┤ | └─userid────(2) ─── ┘ | Notes: | 1 Only ALLCON, CONN and TCP are valid with INTERVAL. | 2 The userid
    [Show full text]
  • Useful Commands in Linux and Other Tools for Quality Control
    Useful commands in Linux and other tools for quality control Ignacio Aguilar INIA Uruguay 05-2018 Unix Basic Commands pwd show working directory ls list files in working directory ll as before but with more information mkdir d make a directory d cd d change to directory d Copy and moving commands To copy file cp /home/user/is . To copy file directory cp –r /home/folder . to move file aa into bb in folder test mv aa ./test/bb To delete rm yy delete the file yy rm –r xx delete the folder xx Redirections & pipe Redirection useful to read/write from file !! aa < bb program aa reads from file bb blupf90 < in aa > bb program aa write in file bb blupf90 < in > log Redirections & pipe “|” similar to redirection but instead to write to a file, passes content as input to other command tee copy standard input to standard output and save in a file echo copy stream to standard output Example: program blupf90 reads name of parameter file and writes output in terminal and in file log echo par.b90 | blupf90 | tee blup.log Other popular commands head file print first 10 lines list file page-by-page tail file print last 10 lines less file list file line-by-line or page-by-page wc –l file count lines grep text file find lines that contains text cat file1 fiel2 concatenate files sort sort file cut cuts specific columns join join lines of two files on specific columns paste paste lines of two file expand replace TAB with spaces uniq retain unique lines on a sorted file head / tail $ head pedigree.txt 1 0 0 2 0 0 3 0 0 4 0 0 5 0 0 6 0 0 7 0 0 8 0 0 9 0 0 10
    [Show full text]
  • Install and Run External Command Line Softwares
    job monitor and control top: similar to windows task manager (space to refresh, q to exit) w: who is there ps: all running processes, PID, status, type ps -ef | grep yyin bg: move current process to background fg: move current process to foreground jobs: list running and suspended processes kill: kill processes kill pid (could find out using top or ps) 1 sort, cut, uniq, join, paste, sed, grep, awk, wc, diff, comm, cat All types of bioinformatics sequence analyses are essentially text processing. Unix Shell has the above commands that are very useful for processing texts and also allows the output from one command to be passed to another command as input using pipe (“|”). less cosmicRaw.txt | cut -f2,3,4,5,8,13 | awk '$5==22' | cut -f1 | sort -u | wc This makes the processing of files using Shell very convenient and very powerful: you do not need to write output to intermediate files or load all data into the memory. For example, combining different Unix commands for text processing is like passing an item through a manufacturing pipeline when you only care about the final product 2 Hands on example 1: cosmic mutation data - Go to UCSC genome browser website: http://genome.ucsc.edu/ - On the left, find the Downloads link - Click on Human - Click on Annotation database - Ctrl+f and then search “cosmic” - On “cosmic.txt.gz” right-click -> copy link address - Go to the terminal and wget the above link (middle click or Shift+Insert to paste what you copied) - Similarly, download the “cosmicRaw.txt.gz” file - Under your home, create a folder
    [Show full text]