0% found this document useful (0 votes)
6 views

T02 BasicLinuxCommands

Uploaded by

Udaysinha Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

T02 BasicLinuxCommands

Uploaded by

Udaysinha Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Warnings Basic Linux Commands 1

First 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
terminal.

Second, most of the Linux commands have features that are enabled by using command-
line switches; check the man pages for the commands for details!

CS@VT Computer Organization I ©2005-2020 WD McQuain


Getting Started Basic Linux Commands 2

The Linux terminal (or command shell) allows you to enter commands and execute
programs.

A 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 running.

It is likely that by default you will run the bash shell.

CS@VT Computer Organization I ©2005-2020 WD McQuain


What’s Running? Basic Linux Commands 3

The ps command displays information about processes the shell is currently running:

We see that two processes are executing, bash and ps.

Moreover, 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 ps a second time… you’ll notice that the PID for bash is the same as before
but the PID for ps has changed.

Why? (That’s two questions.)

CS@VT Computer Organization I ©2005-2020 WD McQuain


More Information Basic Linux Commands 4

Try running the process snapshot command: ps with 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 ps
command were altered by the use of a “switch” on the command line.

In this case, the –l switch means show detailed information (a long listing).

This is typical of Linux commands and many user programs.

CS@VT Computer Organization I ©2005-2020 WD McQuain


One way to find out more… Basic Linux Commands 5

The man (manual) command can be used to obtain more information about Linux
commands:

The man pages are often the first resort for discovering options.
Try running man man…
CS@VT Computer Organization I ©2005-2020 WD McQuain
The Linux Documentation Project Basic Linux Commands 6

CS@VT Computer Organization I ©2005-2020 WD McQuain


File System Basics Basic Linux Commands 7

The file system is a set of data structures that organizes collections of files.

Files are grouped into directories (although directories are themselves files).

Here’s one possible file system organization:


root

super-directory
(parent)

sub-directory
(child)

regular file

CS@VT Computer Organization I ©2005-2020 WD McQuain


File System Basics Basic Linux Commands 8

For my CentOS 8 installation, here's what the top-level of my file system looks like:

├── bin -> usr/bin many common utilities


├── boot boot-related info and utilities
├── dev info about various devices
├── etc system-wide configuration files
├── home user directories
├── lib -> usr/lib shared binaries
├── lib64 -> usr/lib64
├── media access to removable devices
├── mnt mount points
├── opt optional software (usually non-standard)
├── proc information about processes
├── root home directory of the root user
├── run transient files for running processes
├── sbin -> usr/sbin system administration binaries
├── srv data for system services
├── sys
├── tmp temp files for running processes
├── usr user utilities
└── var user logs, etc.

CS@VT Computer Organization I ©2005-2020 WD McQuain


File Names Basic Linux Commands 9

Each 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 commands

File names are often of the form <name.ext>, such as BashTerminal.jpg.

While file extensions are not mandatory, it is standard (and good) practice to employ them.

You are required to use appropriate file extensions in this course.

It is bad practice to employ extensions incorrectly. Common ones include:


- c C language source files
- h C language header files
- txt plain text files
- gz file compressed with gzip
- tar archive file created with tar
- html hypertext markup language file

CS@VT Computer Organization I ©2005-2020 WD McQuain


Absolute Pathnames Basic Linux Commands 10

Each file (and directory) can be specified by a unique absolute pathname:

/etc

/home/max

/home/hls/bin/log

A pathname is a sequence of directory names, separated by forward slash characters ('/'),


and possibly ending with a file name.
CS@VT Computer Organization I ©2005-2020 WD McQuain
Home Directory Basic Linux Commands 11

When you open a terminal, by default you will be in your home directory.

Typically, this will be /home/<userid>, but you can check the path to your current
directory by using the present working directory, pwd:

CS@VT Computer Organization I ©2005-2020 WD McQuain


What’s in here? Basic Linux Commands 12

The list command, ls lists the files in the current directory:

CS@VT Computer Organization I ©2005-2020 WD McQuain


Directory Tree Basic Linux Commands 13

You 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 later.

CS@VT Computer Organization I ©2005-2020 WD McQuain


Directory Navigation Basic Linux Commands 14

You can use the change directory command, cd, to change your current (or working)
directory:

Using cd with no destination moves you back to your home directory:

CS@VT Computer Organization I ©2005-2020 WD McQuain


Relative Pathnames Basic Linux Commands 15

You can also specify a pathname that’s relative to the current (working) directory.

Let’s say you’re in a directory ~/2505/C01/alt/:

../C01Grading.tar

../code/checkAnswer.c

There are two special directory names:

. refers to the current directory


.. refers to the parent of the current directory

CS@VT Computer Organization I ©2005-2020 WD McQuain


Making/Removing a Directory Basic Linux Commands 16

mkdir: creates a new subdirectory of the current directory

rmdir: deletes a empty subdirectory

rm -Rf: deletes a subdirectory and all its contents (recursive, very dangerous!)

CS@VT Computer Organization I ©2005-2020 WD McQuain


Copying Files: cp Basic Linux Commands 17

You can create a copy of a file with the cp command.

Assume we’re in a directory containing a file named infloop.c:

cp infloop.c infloop2.c

makes a copy of infloop.c named infloop2.c in the same directory

cp infloop.c ..

makes a copy of infloop.c with the same name in the parent directory

cp infloop.c ../infloop2.c

makes a copy of infloop.c, named infloop2.c, in the parent directory

CS@VT Computer Organization I ©2005-2020 WD McQuain


Renaming/Moving Files: mv Basic Linux Commands 18

As before, assume we’re in a directory containing a file named infloop.c:

mv infloop.c infiniteloop.c

changes the name of the file infloop.c to infinitefloop.c

mv infloop.c ../attic

moves the file infloop.c to the subdirectory of the parent named attic

mv infloop.c ../infiniteloop.c

removes the file infloop.c from this directory, and creates a copy named
infinitefloop.c in the parent directory

Modern Linux also has the command rename; UNIX did not.

CS@VT Computer Organization I ©2005-2020 WD McQuain


Viewing a File: cat Basic Linux Commands 19

You can use the cat command to display the contents of a file to the terminal:

CS@VT Computer Organization I ©2005-2020 WD McQuain


Viewing a File: less Basic Linux Commands 20

You can use the less command to display the contents of a file to the terminal, one
screenful at a time; here we entered less driver.c:

Just hit <space> or f to advance, b to back up, and q to quit.

CS@VT Computer Organization I ©2005-2020 WD McQuain


Viewing a File: head and tail Basic Linux Commands 21

You 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; see the man page.

CS@VT Computer Organization I ©2005-2020 WD McQuain


Counting Words… and More Basic Linux Commands 22

The wc command reports the number of lines, "words", and bytes in a file:

CS@VT Computer Organization I ©2005-2020 WD McQuain


Searching File Contents: grep Basic Linux Commands 23

The grep command can be used to display lines of a file that match a pattern:

CS@VT Computer Organization I ©2005-2020 WD McQuain


Searching File Contents: grep Basic Linux Commands 24

The grep command can also be used to examine a collection of files:

CS@VT Computer Organization I ©2005-2020 WD McQuain


Chaining Commands Basic Linux Commands 25

The pipe symbol (|) connects standard output from one command to standard input for the
next command:

CS@VT Computer Organization I ©2005-2020 WD McQuain


Redirecting Output Basic Linux Commands 26

The 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):

CS@VT Computer Organization I ©2005-2020 WD McQuain


Redirecting Input Basic Linux Commands 27

The contents of a file can be sent, as standard input, to a program by using the input
redirection operator (<):

CS@VT Computer Organization I ©2005-2020 WD McQuain


Checking the File Type: file Basic Linux Commands 28

You can obtain information about a file with the file command:

Using the --mime-type switch produces shorter type strings instead of the more verbose
output shown above.

CS@VT Computer Organization I ©2005-2020 WD McQuain


Traditional Access Permissions Basic Linux Commands 29

There are three types of users:


- owner (aka user)
- group
- other (aka world)

A user may attempt to access an ordinary file in three ways:


- read from
- write to
- execute

Use ls –l to view the file permissions:

1143 wmcquain@centosvm in ~/2505> ls -l C04/code


total 48
-rw-rw----. 1 wmcquain comporg 4653 Aug 7 21:45 comparator.c
-rwxr-xr-x. 1 wmcquain comporg 13340 Aug 7 21:45 compare
-rw-rw----. 1 wmcquain comporg 2612 Aug 7 21:45 driver.c
-rw-rw----. 1 wmcquain comporg 451 Aug 10 19:30 Intersection.h
-rw-rw----. 1 wmcquain comporg 1944 Aug 7 21:45 Intersection.o
drwxrwxr--. 1 wmcquain comporg 3073 Aug 7 21:45 tmpbackup

CS@VT Computer Organization I ©2005-2020 WD McQuain


Traditional Access Permissions Basic Linux Commands 30

File type

File permissions (owner group other)

Number of links to file

Owner Group
Size

Modification time

File name

-rwxr-xr-x. 1 wmcquain comporg 13340 Aug 7 21:45 compare


drwxrwxr--. 1 wmcquain comporg 3073 Aug 7 21:45 tmpbackup

CS@VT Computer Organization I ©2005-2020 WD McQuain


Changing Access Permissions: chmod Basic Linux Commands 31

Use the chmod command to set or alter traditional file permissions:


1143 wmcquain@centosvm in ~/2505> ls -l C04/code/comparator.c
-rw-rw----. 1 wmcquain comporg 4653 Aug 7 21:45 comparator.c

1144 wmcquain@centosvs in ~/2505> chmod g-rw C04/code/comparator.c

1143 wmcquain@centosvm in ~/2505> ls -l C04/code/comparator.c


-rw-------. 1 wmcquain comporg 4653 Aug 7 21:45 comparator.c

chmod also allows the use of numeric arguments:


0 no access permissions
1 execute permissions
2 write to permissions
4 read from permissions

So, chmod 740 would set


owner permissions to r w x
group permissions to r- -
other permissions to - - -
WHY?
CS@VT Computer Organization I ©2005-2020 WD McQuain
Changing Access Permissions: chmod Basic Linux Commands 32

Binary representations:

none 0 000
x 1 001
w 2 010
r 4 100

Now notice that 7 = 111 which is the logical OR of 001 and 010 and 100

And, 740 thus specifies permissions 7 for the owner, 4 for the group and 0 for others.

CS@VT Computer Organization I ©2005-2020 WD McQuain


The Importance of Access Permissions Basic Linux Commands 33

When working on a shared environment, like the rlogin cluster, it is vital that you make
sure that your access permissions are set correctly.

As a general rule, you will rely on the default access permissions, which are controlled via
shell configuration files we will discuss later.

When in doubt, use ls –l to check!

CS@VT Computer Organization I ©2005-2020 WD McQuain


Bundling Files into an Archive: tar Basic Linux Commands 34

You can create a single file that contains a collection of files, including a directory structure
with the tar utility:

cvf create archive, be verbose, write to a file

Note the name of the new tar file is listed before the target (files to be tar'd up).

DO NOT get that backwards!

CS@VT Computer Organization I ©2005-2020 WD McQuain


Applied Terror: tar Basic Linux Commands 35

As with all commands, your syntax must be precise… but the tar command has the
potential to destroy files:

xkcd.org

CS@VT Computer Organization I ©2005-2020 WD McQuain


A Safer Way Basic Linux Commands 36

There is a bash shell script on the Resources page that provides a safer alternative:

Download safertar.sh, put it in a directory in your path, and make it executable.

This comes with the usual software license…

CS@VT Computer Organization I ©2005-2020 WD McQuain


Checking Contents Basic Linux Commands 37

You can check the contents of a tar file:

This is an example of a flat tar file.

That is, there is no directory structure in the tar file.

CS@VT Computer Organization I ©2005-2020 WD McQuain


A tar File That Is Not Flat Basic Linux Commands 38

If you tar a directory tree, the tar file will (by default) contain path information:

Some situations require a flat tar file, some require creating one that preserves a directory
structure.

Be sure you pay attention to what's required, and create the correct type of tar.

CS@VT Computer Organization I ©2005-2020 WD McQuain


Extracting a tar File Basic Linux Commands 39

Use the x switch to extract the contents of a tar file, and -C to specify a destination:

CS@VT Computer Organization I ©2005-2020 WD McQuain


Compressing Files: gzip, bzip2, etc. Basic Linux Commands 40

A compression tool can frequently reduce the amount of space a file occupies:

A common, older alternative is gzip.

Both of these suffer the same limitation: they can compress, but not bundle.

Therefore, it's common to create a tar archive and then compress that.

CS@VT Computer Organization I ©2005-2020 WD McQuain


Compressing Files: zip Basic Linux Commands 41

The zip utility also compresses, but will bundle as well:

CS@VT Computer Organization I ©2005-2020 WD McQuain


Compressing Files: zip Basic Linux Commands 42

The degree of compression depends on the nature of the file(s) being compressed:

CS@VT Computer Organization I ©2005-2020 WD McQuain


Uncompressing: gunzip, bunzip2, unzip Basic Linux Commands 43

Each compression tool has an analog that will uncompress; for example:

CS@VT Computer Organization I ©2005-2020 WD McQuain


Removing a File: shred and dd Basic Linux Commands 44

rm does not actually delete file contents from your drive; it just deindexes the file.

You can securely remove a file by using the shred command, but see Sobell for a
discussion of the limitations.

See the discussion of dd in Sobell for an alternative way to wipe a file.

CS@VT Computer Organization I ©2005-2020 WD McQuain


Special Characters Basic Linux Commands 45

Many Linux commands support the use of special characters (aka wildcards) to specify a
pattern that identifies a set of files:

? matches any single character (in the name of an existing file)


* matches zero or more characters (in the name of an existing file)
[] matches any of the characters within the braces (in the name of an existing file)

*.txt
matches any file with extension "txt"

foo?.*
matches a file with any extension and name consisting of "foo"
followed by a single character

[abc]foo.html
matches a file with extension "html" and name "afoo" or "bfoo" or
"cfoo"

CS@VT Computer Organization I ©2005-2020 WD McQuain


Copying a File Remotely: scp Basic Linux Commands 46

scp can be used to copy a file between the local machine and a remote machine (or
between two remote machines).

For example, the following command would copy GettysburgAddress.txt from my


computer to a directory named documents on rlogin:

scp GettysburgAddress.txt [email protected]:documents

If you haven’t set up password-less login, you’ll be prompted for the necessary
authentication information.

And the following command would copy GettysburgAddress.txt from my rlogin


account to my current directory on my machine:

scp [email protected]:documents/GettysburgAddress.txt .

CS@VT Computer Organization I ©2005-2020 WD McQuain


Identifying a Command: which Basic Linux Commands 47

If you’re not sure where a command resides, the which command will tell you:

Many Linux applications also support a --version switch which can help identify which
specific version of an application you’re invoking.

CS@VT Computer Organization I ©2005-2020 WD McQuain


Foreground vs Background Basic Linux Commands 48

By default when you execute a command in a shell, the shell program waits (doesn’t
provide a prompt and allow entry of another command) until the current command
completes (or is otherwise interrupted).

Here, the command is running in the foreground:

1141 wmcquain@centosvm in ~/tmp> sleeper 5 hi


0 995: hi
1 997: hi
2 1: hi
3 7: hi
4 15: hi

CS@VT Computer Organization I ©2005-2020 WD McQuain


Foreground vs Background Basic Linux Commands 49

We may run the command in the background:

1143 wmcquain@centosvm in ~/tmp> sleeper 5 hi > sleeper.txt &


[1] 8672

1144 wmcquain@centosvm in ~/tmp> ps


PID TTY TIME CMD
3928 pts/0 00:00:01 bash
8672 pts/0 00:00:00 sleeper
8676 pts/0 00:00:00 ps

1145 wmcquain@centosvm in ~/tmp>


[1]+ Done sleeper 5 hi > sleeper.txt

1145 wmcquain@centosvm in ~/tmp> cat sleeper.txt


0 270: hi
1 272: hi
2 276: hi
3 282: hi
4 290: hi

CS@VT Computer Organization I ©2005-2020 WD McQuain


Killing a Process Basic Linux Commands 50

A (foreground) running process can be killed by using Ctrl-C.

A (background) running process or a suspended process can be killed by using the kill
command:

CS@VT Computer Organization I ©2005-2020 WD McQuain

You might also like