T02 BasicLinuxCommands
T02 BasicLinuxCommands
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.
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!
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.
The ps command displays information about processes the shell is currently running:
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.
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).
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
The file system is a set of data structures that organizes collections of files.
Files are grouped into directories (although directories are themselves files).
super-directory
(parent)
sub-directory
(child)
regular file
For my CentOS 8 installation, here's what the top-level of my file system looks like:
While file extensions are not mandatory, it is standard (and good) practice to employ them.
/etc
/home/max
/home/hls/bin/log
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:
You can display a map of the directory tree rooted at your current directory:
You can use the change directory command, cd, to change your current (or working)
directory:
You can also specify a pathname that’s relative to the current (working) directory.
../C01Grading.tar
../code/checkAnswer.c
rm -Rf: deletes a subdirectory and all its contents (recursive, very dangerous!)
cp infloop.c infloop2.c
cp infloop.c ..
makes a copy of infloop.c with the same name in the parent directory
cp infloop.c ../infloop2.c
mv infloop.c infiniteloop.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.
You can use the cat command to display the contents of a file to the terminal:
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:
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.
The wc command reports the number of lines, "words", and bytes in a file:
The grep command can be used to display lines of a file that match a pattern:
The pipe symbol (|) connects standard output from one command to standard input for the
next command:
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):
The contents of a file can be sent, as standard input, to a program by using the input
redirection operator (<):
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.
File type
Owner Group
Size
Modification time
File name
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.
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.
You can create a single file that contains a collection of files, including a directory structure
with the tar utility:
Note the name of the new tar file is listed before the target (files to be tar'd up).
As with all commands, your syntax must be precise… but the tar command has the
potential to destroy files:
xkcd.org
There is a bash shell script on the Resources page that provides a safer alternative:
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.
Use the x switch to extract the contents of a tar file, and -C to specify a destination:
A compression tool can frequently reduce the amount of space a file occupies:
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.
The degree of compression depends on the nature of the file(s) being compressed:
Each compression tool has an analog that will uncompress; for example:
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.
Many Linux commands support the use of special characters (aka wildcards) to specify a
pattern that identifies a set of files:
*.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"
scp can be used to copy a file between the local machine and a remote machine (or
between two remote machines).
If you haven’t set up password-less login, you’ll be prompted for the necessary
authentication information.
scp [email protected]:documents/GettysburgAddress.txt .
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.
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).
A (background) running process or a suspended process can be killed by using the kill
command: