0% found this document useful (0 votes)
17 views11 pages

Tutorial 1

The document provides an overview of essential command-line interface (CLI) commands for managing users, system information, file management, and text editing. It includes commands like 'whoami', 'hostname', 'mkdir', 'cp', 'mv', and 'vim', along with their usage and examples. Additionally, it covers tasks for practicing these commands and manipulating text files using tools like 'grep', 'head', and 'tail'.

Uploaded by

Sithara Bandara
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)
17 views11 pages

Tutorial 1

The document provides an overview of essential command-line interface (CLI) commands for managing users, system information, file management, and text editing. It includes commands like 'whoami', 'hostname', 'mkdir', 'cp', 'mv', and 'vim', along with their usage and examples. Additionally, it covers tasks for practicing these commands and manipulating text files using tools like 'grep', 'head', and 'tail'.

Uploaded by

Sithara Bandara
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

Essential CLI

Essential Commands
1. Start by understanding the whoami command. This command displays
the current user’s username. To use the command, type the following into
the terminal:
whoami
2. Next, we will look at the hostname command. This command displays
the name of the current host. To use the command, type the following
into the terminal:
hostname
To view or set the system hostname, use the following command:
hostnamectl
To change the hostname to [Link]:
hostnamectl set-hostname [Link]
3. The date command allows you to view the current date and time. To use
the command, type the following into the terminal:
date
To format the output to display the time only, use the following command:
date +%H:%M:%S
To generate random numbers, use the following command:
echo $(date +%s-$RANDOM)
The $ sign here tells the shell to execute the block between brackets
To change the date to August 12, 2021:
timedatectl set-time 2021-08-12
Or
date --set “2021-08-12”

1
4. The uname command gives information about the system’s kernel, such as
the kernel version and platform. To use the command, type the following
into the terminal:
uname -a
Don’t forget about –help to know more capabilities about a specific com-
mand
5. The wc command displays the number of lines, words, and characters in
a text file. To use the command, type the following into the terminal,
replacing “/etc/profile” with the file you wish to check:
wc /etc/profile
Output: 76 252 1750 /etc/profile
The first column indicates the number of lines (76) followed by the number
of words (252), number of characters (or bytes) (1750), and file name
(/etc/profile) in subsequent columns.
• –l Prints line count
• –w Prints word count
• –c Prints byte count
• –m Prints character count
To display only the number of lines in a file, use the following command:
wc –l /etc/profile
Output: 76 /etc/profile
Try running wc with the other options and review the results.
6. The clear command clears the terminal screen. To use the command,
type the following into the terminal:
clear
7. The uptime command shows how long the system has been running. To
use the command, type the following into the terminal:
uptime
8. The lscpu command shows information about the processor architecture
and count. To use the command, type the following into the terminal:
lscpu
9. The man command displays manual pages for a program. To use the
command, type the following into the terminal, replacing “passwd” with
the program you wish to check:
man passwd
While we are in man pages, some common keys help us navigate efficiently.

2
• Spacebar Moves forward one page
• b Moves backward one page
• d Moves down
• u Moves up
• g / G Moves to the beginning / end of the man pages
• q Quits the man pages
• /pattern Searches forward for the specified pattern
• ?pattern Searches backward for the specified pattern
• n / N Finds the next / previous occurrence of a pattern
• H Gives help on navigational keys
To search for a specific command by keyword use the following command
man -k password

3
File Management Tools

File Management Tools


Start by understanding the mkdir command, which allows you to create a new
directory. To use the command, type the following into the terminal:
mkdir lab01
Now navigate to the directory you just created:
cd lab01
To go back to the parent directory, use the following command:
cd ..
Next, learn about the ls command, which allows you to list all files and directo-
ries, hidden or not, in the current directory.
First, let’s create some files:
touch file{0..50}
This will create 50 empty files, the .. there means sequential, that is create in
a sequence of 0 and stop at 50.
Use the ls command to list all files:
ls
To list them in long format:
ls -l
To display all and hidden files:
ls -la
To sort files by date:
ls -lrt
And many more, I will let you explore the options, don’t forget:
man ls

1
Next, touch, which allows you to create new empty files. To use the command,
type the following into the terminal, replacing “file01” with the desired file:
touch file01
Learn about the cp command, which allows you to copy files/folders from one
location to another. To use the command, type the following into the terminal:
cp ./file01 ../
This will copy file01 from the current directory to the parent directory. To check
if the file was copied successfully, use the following command:
ls ../
To copy folders recursively, use the following command:
cp -r /etc /tmp
This will copy the directory and all its files to the destination /tmp
Next, mv which renames files and directories. To use the command, type the
following into the terminal:
mv file50 file200
ls -l
Next, find, which is useful in finding files/directories recursively. To use the
command, type the following into the terminal, replacing “[DESTINATION]”
with the desired location and “filename*” with the desired file name:
find [DESTINATION] -name "filename*"
For example, to find a file named “file20” in the current directory:
find . -name "file20"
To find all files created by a specific user, use the following command:
find / -user root
You can also find by modified time or levels:
find ./ -maxdepth K -mtime N
Where K is the maximum level of search, that is how many child directories to
search? N is * 24, search all files that were modified in the last 24 hours.
For example:
find /home -maxdepth 2 -mtime 1
Next, rm which stands for remove files/directory
ls -l
rm file{60..70}
ls -l

2
To remove directories and force use
mkdir dir{01..10}
ls -l
rm -rf dir{01..05}
ls -l
Finally, learn about the tar command, which is used to manage archive files.
Let’s say you want to compress the home directory. To create an archive file of
this folder, use the following command:
tar -czvf [Link] ~/home
• -c: Create an archive.
• -z: Compress the archive with gzip.
• -v: makes tar talk a lot. Verbose output shows you all the files being
archived and much.
• -f: Allows you to specify the filename of the archive.
To extract files from an archive, use the following command:
tar -xvzf [Link] -C /tmp/
This command extracts the contents of the “[Link]” file to the “/tmp”
directory.
• f: this must be the last flag of the command, and the tar file must be
immediately after. It tells tar the name and path of the compressed file.
• z: tells tar to decompress the archive using gzip
• x: tar can collect files or extract them. x does the latter.
• v: makes tar talk a lot. Verbose output shows you all the files being
extracted.

Tasks
Move to the home directory:
cd
1. Create the directory structure /tmp/files/pictures, /tmp/files/photos, and
/tmp/files/videos by using the following command:
Show Solution
mkdir -p /tmp/files/pictures /tmp/files/photos /tmp/files/videos
2. Copy all files that have a name starting with ‘a’, ‘b’, or ‘c’ from the /etc
directory to the /tmp/files directory by using the following command:
Show Solution
find /etc -name ’[abc]*’ -exec cp -r {} /tmp/files ;

3
3. Move all files that have a name starting with ‘a’ or ‘b’ to the
/tmp/files/photos directory, and all files that have a name starting
with ‘c’ to the /tmp/files/videos directory by using the following
command:
Show Solution
find /tmp/files -name ‘[ab]’ -exec mv {} /tmp/files/photos ; find /tmp/files
-name ’c’ -exec mv {} /tmp/files/videos ;
4. Find all files in the /etc directory that have a size smaller than 1000 bytes
and copy those to the /tmp/files/pictures directory by using the following
command:
Show Solution
find /tmp/files -name ’[ab]*’ -exec mv {} /tmp/files/photos ; find /etc
-size -1000c -exec cp -r {} /tmp/files/pictures ;
5. Create a compressed archive of the home directory and extract this com-
pressed archive with relative file names in tmp archive by using the follow-
ing command:
Show Solution
tar -czvf [Link] -C ~/ . && tar -xvf [Link] -C /tmp/archive
–strip-components=1

4
Working with Text Files

The vi/vim Editor


Vim is a powerful text editor that is popular among developers and power
users. This tutorial will guide you through the basics of using Vim and provide
examples of how to perform common tasks.
1. Open the terminal and type “vim” to launch the editor.
2. To enter insert mode and begin editing, press “i” on your keyboard. You
can now type and insert text. For example, you can type Hello, this
is a sample text
3. To save your changes and exit insert mode, press Esc on your keyboard.
4. To navigate within the document, use the arrow keys on your keyboard.
For example, you can use the right arrow key to move the cursor to the
right.
5. To delete a character, use x while in normal mode. For example, you can
press x to delete the character under the cursor.
6. To delete a line, use dd while in normal mode. For example, you can press
dd to delete the current line.
7. To undo an action, use u while in normal mode. For example, you can
press u to undo the last action, such as deleting a line.
8. To redo an action, use Ctrl + r while in normal mode. For example, you
can press Ctrl + r to redo the last undone action.
9. To search for a specific word or phrase within the document, use / followed
by the word or phrase you wish to search for. Press n to move to the next
instance of the word or phrase, and N to move to the previous instance. For
example, you can press /sample and then n to move to the next instance
of the word sample in the document.
10. To save and exit the document, use :wq while in normal mode. For exam-
ple, you can press :wq to save the changes and exit the document.
11. To exit without saving changes, use :q! while in normal mode. For
example, you can press :q! to exit the document without saving any

1
changes.

Text Manipulation Tools


more and less commands are used to work with reading content of files
The difference is that more reads the entire content of the file before showing
the first page whereas less only reads one page at a time, thus faster.
more /etc/passwd
less /etc/passwd
head and tail allows to read the beginning or end of file
head /etc/passwd
tail /etc/passwd
Use -n to specify count of lines, to display the first three lines
head -n 3 /etc/passwd
To display last 3 lines
tail -n 3 /etc/passwd
cat allows you to read content of a file
cat /etc/passwd
grep allows you to filter, search content of a file using Regular Expressions
grep root /etc/passwd
To search for the pattern nologin in /etc/passwd and exclude the lines in the
output that contain this pattern. Use –v for pattern exclusion and –n for line
numbers associated with the lines that do not contain the pattern.
grep –nv nologin /etc/passwd
To search for all lines in the /etc/passwd file that begin with the pattern root.
The bash shell treats the caret ^ sign as a special character which marks the
beginning of a line or word. This is useful, for instance, if we wish to know
whether there are more than one users with that name.
grep ^root /etc/passwd
To list all lines from the /etc/passwd file that end with the pattern bash. The
bash shell treats the $ sign as a special character which marks the end of a line
or word. This is useful, for example, to determine which users have their shells
set to the bash shell.
grep bash$ /etc/passwd
To search for all empty lines in the /etc/passwd file:

2
grep ^$ /etc/passwd
To search for all lines in the /etc/passwd file that contain the pattern root. The
–i option instructs the command to perform a case-insensitive search. This is
useful to determine if there are any root user accounts with a combination of
lowercase and uppercase letters.
grep –i root /etc/passwd
To print all lines from the output of the ll command that contain either the
cron or qemu pattern. The pipe character is used as an OR operator in this
example.
ll /etc | grep -E 'cron|qemu'
The * matches zero to an unlimited number of characters (except for the leading
period in a hidden file).
To list names of all files in the /var/log directory that end in .log:
ls /var/log/*.log
The [] can be used to match either a set of characters or a range of characters
for a single character position.
ls /usr/sbin/[yw]*
The pipe, represented by the vertical bar | and residing with the \ on the
keyboard, is a special character that is used to send the output of one command
as input to the next.
The following example runs the ll command on /etc and sends the output to
the more command for displaying the directory listing:
ll /etc/ | more
In another example, the who command is run and its output is piped to the nl
command to number each line:
who | nl
The following example forms a pipeline by piping the output of ll to grep for
the lines that do not contain the pattern root. The filtered output is numbered
and the final result is printed on the screen one screenful at a time.
ll /proc | grep -v root | nl | more

Tasks
• Use head and tail to display the fifth line of the file /etc/passwd.
Show Solution
head -n 5 /etc/passwd | tail -n 1

3
• Use grep to show the names of all files in /etc that have lines starting with
the text root.
Show Solution
grep -l “^root” /etc/*
• Use grep to show all lines from all files in /etc that contain exactly three
characters.
Show Solution
grep -E ‘^.{3}$’ /etc/*
• Create a file named alex, open it in Vi and input in “alex is 40 years old”
in the first line, and in the second line write: “alexander is 42 years old”,
save and quit. Use grep to show only the lines that contains ‘alex’.
Show Solution
touch alex vi alex ( insert mode) alex is 40 years old (esc) o alexander is
42 years old (esc) :wq grep “alex” alex
• Run the ll command on the /etc, /dvd, and the /var dirctories. Have the
output redirected to the /tmp/ioutput file and the errors forwarded to the
/tmp/ioerror file. Check both files after the execution of the command to
validate.
Show Solution
ll /etc /dvd /var > /tmp/ioutput 2> /tmp/ioerror cat /tmp/ioutput cat
/tmp/ioerror Note: Here the assumption is that /dvd and /var are ex-
isting directories otherwise you need to replace them with the existing
directories.

You might also like