Transcription of Basic Linux Commands - Virginia Tech
1 Basic Linux CommandsComputer Organization I1CS@VT 2005-2020 WD McQuainWarningsFirst of all, these notes will cover only a small subset of the available Commands and utilities, and will cover most of those in a shallow fashion. Read the relevant material in Sobell!If you want to follow along with the examples that follow, and you do, open a Linux , most of the Linux Commands have features that are enabled by using command -line switches; check the manpages for the Commands for details! Basic Linux CommandsComputer Organization I2CS@VT 2005-2020 WD McQuainGetting StartedThe Linux terminal (or command shell) allows you to enter Commands and execute terminal displays a prompt and a cursor indicating it s waiting for user input:The prompt itself is configurable; precisely how depends on the particular type of shell you are is likely that by default you will run the Linux CommandsComputer Organization I3CS@VT 2005-2020 WD McQuainWhat s Running?
2 The pscommand displays information about processes the shell is currently running:We see that two processes are executing, bashand , we see that:-each is assigned a unique numeric identifier called a process ID or PID-each is associated with a terminal (TTY) named pts/0 Try executing psa second you ll notice that the PID for bashis the same as before but the PID for pshas (That s twoquestions.) Basic Linux CommandsComputer Organization I4CS@VT 2005-2020 WD McQuainMore InformationTry running the process snapshotcommand: pswith the l(that s ell, not one) switch:Don t worry about the meaning of all that just yet, but do notice that the results of the pscommand were altered by the use of a switch on the command this case, the lswitch means show detailed information (a long listing).
3 This is typical of Linux Commands and many user Linux CommandsComputer Organization I5CS@VT 2005-2020 WD McQuainOne way to find out man(manual) command can be used to obtain more information about Linux Commands :The manpages are often the first resort for discovering running man Linux CommandsComputer Organization I6CS@VT 2005-2020 WD McQuainThe Linux Documentation ProjectBasic Linux CommandsComputer Organization I7CS@VT 2005-2020 WD McQuainFile System BasicsThe file system is a set of data structures that organizes collections of are grouped into directories(although directories are themselves files).
4 Here s one possible file system organization:rootsub-directory(child)sup er-directory(parent)regular fileBasic Linux CommandsComputer Organization I8CS@VT 2005-2020 WD McQuainFile System BasicsFor my CentOS 8 installation, here's what the top-level of my file system looks like: bin -> usr/binmany common utilities bootboot-related info and utilities devinfo about various devices etcsystem-wide configuration files homeuser directories lib -> usr/libshared binaries lib64 -> usr/lib64 mediaaccess to removable devices mntmount points optoptional software (usually non-standard)
5 Procinformation about processes roothome directory of the root user runtransient files for running processes sbin-> usr/sbinsystem administration binaries srvdata for system services sys tmptemp files for running processes usruser utilities varuser logs, Linux CommandsComputer Organization I9CS@VT 2005-2020 WD McQuainFile NamesEach file and directory has a name:-names are case-sensitive-names within the same directory must be unique-the use of characters other than letters, digits, underscores and periods tends to cause extra work when using many commandsFile names are often of the form < >, such as file extensions are not mandatory, it is standard (and good) practice to employ are required to use appropriate file extensions in this is bad practice to employ extensions incorrectly.
6 Common ones include:-cC language source files-hC language header files-txtplain text files-gzfile compressed with gzip-tararchive file created with tar-htmlhypertext markup language fileBasic Linux CommandsComputer Organization I10CS@VT 2005-2020 WD McQuainAbsolute PathnamesEach file (and directory) can be specified by a unique absolute pathname://etc/home/hls/bin/log/home/max A pathnameis a sequence of directory names, separated by forward slash characters ('/'), and possibly ending with a file Linux CommandsComputer Organization I11CS@VT 2005-2020 WD McQuainHome DirectoryWhen you open a terminal, by default you will be in your home , this will be /home/<userid>, but you can check the path to your current directory by using the present working directory, pwd: Basic Linux CommandsComputer Organization I12CS@VT 2005-2020 WD McQuainWhat s in here?
7 The listcommand, lslists the files in the current directory: Basic Linux CommandsComputer Organization I13CS@VT 2005-2020 WD McQuainDirectory TreeYou can display a map of the directory tree rooted at your current directory:The tree program may not be installed by default; we'll cover software package installation a bit Linux CommandsComputer Organization I14CS@VT 2005-2020 WD McQuainDirectory NavigationYou can use the change directorycommand, cd, to change your current (or working) directory:Using cd with no destination moves you back to your home directory: Basic Linux CommandsComputer Organization I15CS@VT 2005-2020 WD McQuainRelative PathnamesYou can also specify a pathname that s relative to the current (working) s say you re in a directory ~/2505/C01/alt/.
8 Are two special directory names:.refers to the current to the parent of the current directoryBasic Linux CommandsComputer Organization I16CS@VT 2005-2020 WD McQuainMaking/Removing a Directorymkdir: creates a new subdirectory of the current directoryrmdir: deletes a emptysubdirectoryrm-Rf: deletes a subdirectory and all its contents (recursive, very dangerous!) Basic Linux CommandsComputer Organization I17CS@VT 2005-2020 WD McQuainCopying Files: cpYou can create a copy of a file with the we re in a directory containing a file named a copy of the same a copy of , named , in the parent a copy of the same name in the parent directoryBasic Linux CommandsComputer Organization I18CS@VT 2005-2020 WD McQuainRenaming/Moving Files: mvAs before, assume we re in a directory containing a file named.
9 Mv the name of the file the file the subdirectory of the parent named atticmv the file this directory, and creates a copy named the parent directoryModern Linux also has the command rename; UNIX did Linux CommandsComputer Organization I19CS@VT 2005-2020 WD McQuainViewing a File: catYou can use the cat command to display the contents of a file to the terminal: Basic Linux CommandsComputer Organization I20CS@VT 2005-2020 WD McQuainViewing a File: lessYou can use the less command to display the contents of a file to the terminal, one screenfulat a time; here we entered less :Just hit <space>or fto advance, bto back up, and qto Linux CommandsComputer Organization I21CS@VT 2005-2020 WD McQuainViewing a File: head and tailYou can view the first (or last) few lines of a file by using the head(or tail) command :You can control how many lines are shown.
10 See the Linux CommandsComputer Organization I22CS@VT 2005-2020 WD McQuainCounting and MoreThe wccommand reports the number of lines, "words", and bytes in a file: Basic Linux CommandsComputer Organization I23CS@VT 2005-2020 WD McQuainSearching File Contents: grep The grepcommand can be used to display lines of a file that match a pattern: Basic Linux CommandsComputer Organization I24CS@VT 2005-2020 WD McQuainSearching File Contents: grep The grepcommand can also be used to examine a collection of files: Basic Linux CommandsComputer Organization I25CS@VT 2005-2020 WD McQuainChaining CommandsThe pipesymbol (|) connects standard output from one command to standard input for the next command : Basic Linux CommandsComputer Organization I26CS@VT 2005-2020 WD McQuainRedirecting OutputThe output a program writes to standard output (the terminal) can be sent to a file by using an output redirection operators>(replaces contents) and >>(appends to contents).