0% found this document useful (0 votes)
59 views15 pages

Tutorial 2 Slides

This document provides a tutorial on common Linux command line interface (CLI) commands including cp, mv, rm, touch, cut, paste, sort, chmod, and creating shell scripts. It explains what each command does, basic syntax, and provides examples of usage. It also covers useful shell variables, metacharacters, and assigns tasks for the reader to practice using various commands together in pipelines.

Uploaded by

LaurenceDiack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views15 pages

Tutorial 2 Slides

This document provides a tutorial on common Linux command line interface (CLI) commands including cp, mv, rm, touch, cut, paste, sort, chmod, and creating shell scripts. It explains what each command does, basic syntax, and provides examples of usage. It also covers useful shell variables, metacharacters, and assigns tasks for the reader to practice using various commands together in pipelines.

Uploaded by

LaurenceDiack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Beginning Linux CLI Tutorial 2

March 15, 2014


Beginning Linux CLI Tutorial 2
cp Copy les and directories
cp src dist
File src will be copied.
If dist exists and is a le, it will be overwritten.
If dist is a directory, src will be copied into dist.
cp -r src-dir dest-dir
Directory src-dir and its contents will be copied. If dest-dir
exists, src-dir will be copied into dest-dir. If it doesnt exist,
this command will make a copy of src-dir named dest-dir.
Beginning Linux CLI Tutorial 2
mv Move les and directories
mv src dist
File or directory (and its contents) src will be moved.
The semantics is similar to cp. If src is a le and dist is an
existing le, dist will be overwritten. If dist is an existing
directory, src will be moved into dist. If dist doesnt exist, this
command renames src into dist.
Beginning Linux CLI Tutorial 2
rm remove les or directories
rm file
rm -r dir
Remove file or directory dir
How to break your Linux installation run rm -r /* as root
Beginning Linux CLI Tutorial 2
touch update the timestamp of a le; create one if not
exist
touch new-file
Create a new le named new-file
This is the most useful use-case of this command.
Beginning Linux CLI Tutorial 2
cut print certain sections from each line of a le
Best illustrated with an example: /etc/passwd stores the
information of all users in the system.
It has the format
matias-admin:x:944:552:Matias Perez:/home/admin:/bin/bash
Fields are separated by colons. The user names are in the 5th eld.
To get the names of all users in the system you do
cut -d: -f5 /etc/passwd
-d: delimiter is :
-f5 get the 5th eld
Remember no spaces!
Beginning Linux CLI Tutorial 2
paste merge lines of les
Continuing our previous example, suppose we have a le a which
contains the result of cut -d: -f1 passwd, ie. all login names in
the system, and a le b which contains the real names of these
users, ie. cut -d: -f5 passwd. To get a list of who is whom, do
paste a b
You will get something like
luke-admin Luke Duncalfe
benny-admin Benny Chan
matias-admin Matias Perez
pan-admin Pan Hu
Beginning Linux CLI Tutorial 2
sort sort les
sort -t: -k3 -n /etc/passwd
Sort /etc/passwd by the user id, numerically, in ascending order.
uniq delete duplicated lines in a sorted le
Isnt a very useful command.
Beginning Linux CLI Tutorial 2
chmod change access modes of a le
chmod +x script.sh
By default ordinary les are not granted executable permission.
This command adds exec permission to script.sh.
Remember to do this before you try to run a bash script!
If you dont, you get Permission denied from bash.
chmod -R o+w dir
Make an entire directory write-able for everyone (les and
directories are usually write-able for you and users in your group,
but readonly for everyone else). Useful for sharing directories
among users in a system. Said directory will be highlighted when
you ls --color=auto it.
Beginning Linux CLI Tutorial 2
Creating and running a shell script
vim script.sh Create a shell script
or gedit script.sh & if you like GUI
chmod +x script.sh Make it executable
./script.sh Run the script
Remember ./ is important! Unless . is in your PATH, which is an
unsafe practise.
Start with the line #!/bin/bash. Type lines to the le just like
what you would do in an interactive shell (the terminal screen).
Beginning Linux CLI Tutorial 2
Shell variables
echo $PATH
Shells interprets $PATH, replacing it with the content of PATH.
Equivalent to echo /bin:/sbin:/usr/bin:/usr/sbin . . .
When executing a command, shell searches PATH for possible
candidates, and runs what is found rst.
script.sh is in directory ., which in this case is HOME. It is not in
PATH, therefore running script.sh will result in an error: File not
found. Add . (or $HOME in our case) to PATH (bad!), or run the
script le by typing ./script.sh
Beginning Linux CLI Tutorial 2
Other Important Shell Variables
PWD current working directory
OLDPWD previous working directory
USER whoami
HOME home directory
RANDOM a random number between 0 and 2^15-1
To list all shell vars currently in your shell session, use export
To make a new shell var, or edit an existing one, do
export VAR=content
Beginning Linux CLI Tutorial 2
Useful shell metachars
| --- previous output as the input to the next command
& --- run command in background
&& --- run latter command if former succeeds
c1 || c2 --- run c1, if c1 fails, run c2
> --- output redirection
< --- input redirection
Beginning Linux CLI Tutorial 2
Tasks
1 /etc/group, just like /etc/passwd, has the information of
all user groups in your system. Produce a list of all available
groups.
2 Now redirect the output of Task 1 to a le, and sort it
alphabetically. Remember > does output redirection. Show
the content of that le on the screen.
3 Do Task 1 and Task 2, this time without producing an
intermediate le. Remember, | pipes the output of the
previous command to the input of the next command.
4 Produce a list of the login names (and only login names) of
the currently logged in users, sorted alphabetically. Each user
should appear only once. You will need who, cut, sort, uniq,
and |. If you do this on your local Ubuntu system you will
only see yourself. Log into the Uni Linux server and try this.
Beginning Linux CLI Tutorial 2
Tasks
5 Write a shell script to do Task 4 automatically. Remember
writing a shell script is just like typing in an interactive shell
should be easy once you gured out Task 4. Run this
script in the Uni Linux server (your local HOME is
synchronized with your HOME at the Uni Linux server).
6 Write a shell script to show various system congurations like
a Your current shell
b Your home directory
c Your current path setting
d Your current working directory
e Your previous working directory
f Current date
Beginning Linux CLI Tutorial 2

You might also like