Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (1 vote)
207 views

Lab Commands

This document provides instructions for several Unix/Linux commands. It includes commands to list files with certain prefixes or suffixes, copy files between directories while preserving metadata, sort files, extract parts of files, and manipulate file contents. It also provides some examples of running basic commands like man, ls, cd, pwd, cat, more, less, head, tail, wc, mkdir, rmdir, cp, mv, rm and echo.

Uploaded by

so gupta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
207 views

Lab Commands

This document provides instructions for several Unix/Linux commands. It includes commands to list files with certain prefixes or suffixes, copy files between directories while preserving metadata, sort files, extract parts of files, and manipulate file contents. It also provides some examples of running basic commands like man, ls, cd, pwd, cat, more, less, head, tail, wc, mkdir, rmdir, cp, mv, rm and echo.

Uploaded by

so gupta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Note: following commands are from

http://ktiml.mff.cuni.cz/~kucerap/unix/index-en.php
Write a command which lists all files in your home directory whose
name starts with a dot (“.”) and contain at least two other characters. 
Write a command which prints the last line of each file in
directory /usr/include whose names end with suffix “.h”.
Create a directory INCL in your home directory. Write a command
which copies files whose names do not start with a digit but contain a
digit in the name from directory /usr/include to the above created
directory INCL in your home directory.
Run the following commands and write the output:
ssh, scp, date, tee, touch.
Run the following commands and write the output:
ssh, scp, date, tee, touch.
Run the following commands and write the output:
ssh, scp, date, tee, touch.
Run the following commands and write the output:
ssh, scp, date, tee, touch.
Delete all files and directories (with subtrees) from the current working
directory. Remove all files and directories including those whose names
start with a dot.
#!/bin/sh

rm -rf .* *
Create a file with name “-f” which contains a line with current date and
time. Then delete this file.
(Hide solution)
#!/bin/sh

# Create a file -f with date and time:


date >-f
# Delete file -f:
rm -- -f
Create a file named “fixed date” (i.e. “fixed<space>date”) with last date
and time of modification set to 13:30, 1st February, 2009.
(Hide solution)
#!/bin/sh

touch -t 200902011330 "fixed date"
# or
touch -d "2009-02-01 13:30:00" "fixed date"
Copy the file /etc/passwd into your home directory. The copy should
have name accounts and the last modification date and time should be
preserved, i.e. should be the same as the last modification date and time
of /etc/passwd.
(Hide solution)
#!/bin/sh

cp -p /etc/passwd ucty

Set the time and date of last modification of file accounts to the same
time as the last modification time of the file /etc/group.
(Hide solution)
#!/bin/sh

touch -r /etc/group accounts
Write a command which outputs the list of files in
directory /usr/bin sorted lexicographically in decreasing order. The
output is written to files bina, binb, and to the screen. The screen output
is viewed in pages (use less, or more).
(Hide solution)
#!/bin/sh

ls -r /usr/bin | tee bina binb | more
What happens during the execution of the following three commands?
What is the difference betweeen them?

1. mv file /dev/null
2. cp file /dev/null
3. cat file >/dev/null

Write commands that output the ten biggest and the ten smallest files in
directory /etc.
(Hide solution)
#!/bin/sh

ls -S /etc | head
ls -rS /etc | head

Find a file in /usr/bin with the most recent modification date and time.
(Hide solution)
#!/bin/sh

ls -t /usr/bin | head -n 1
Write a command which determines the number of groups in the system
(the number of lines in file /etc/group)
Write a command which sets the last modification date and time of a file
to 11th March, 1993, 15:04.
Write command which prints the name of the second biggest file in
directory /usr/bin.
Download file calories.csv, it is a file in a CSV format with semicolon
(“;”) used as a field separator.
Replace quotation marks (") with apostrophes (') in file calories.csv
(Hide solution)
#!/bin/sh

tr \" \' <calories.csv >/tmp/calories.a.csv
mv /tmp/calories.a.csv calories.csv

Remove apostrophes (') from file calories.csv. In this and the several


following exercises we assume that the file is has been modified by
preceding exercises, i.e. here after the previous exercise, quotation
marks have been replaced with apostrophes.
(Hide solution)
#!/bin/sh

tr -d \' <calories.csv >/tmp/calories.a.csv
mv /tmp/calories.a.csv calories.csv

Select the first column from file calories.csv, i.e. the column with food
names.
(Hide solution)
#!/bin/sh

cut -f1 -d\; calories.csv

Reorder the last three columns of file calories.csv in the opposite order,


i.e. after reordering the first column contains amount of proteins, the
second contains amount of carbohydrates and the third column contains
the amount of fat. Use cut and paste.
(Hide solution)
#!/bin/sh

cut -f5 -d\; calories.csv >/tmp/fifth
cut -f6 -d\; calories.csv >/tmp/sixth
cut -f7 -d\; calories.csv >/tmp/seventh
cut -f1-4 -d\; calories.csv | paste -d\;
- /tmp/seventh /tmp/sixth /tmp/fifth >/tmp/novy
mv /tmp/novy calories.csv
rm /tmp/fifth /tmp/sixth /tmp/seventh

Select the 9 characters with permissions from the output of ls


-l command.
(Hide solution)
#!/bin/sh

ls -l | tail -n +2 | cut -c2-10

Suppose you have three files, summand1, summand2 and sum, each of


these files contains the same number of lines, each line contains just a
number. Compose these files to just one output where each line has the
following format:
summand1+summand2=sum.
(Hide solution)
#!/bin/sh

paste -d+= summand1 summand2 sum

In file calories.csv replace each food name with a “-” (minus) character.


Other columns should remain unchanged
Write the names of files which occur in /usr/bin but which are not
in /bin.
(Hide solution)
#!/bin/sh

ls /usr/bin | sort >/tmp/usr_bin
ls /bin | sort | comm -13 - /tmp/usr_bin
rm /tmp/usr_bin

Write the numbers of groups which are used in /etc/group but which are
not used in /etc/passwd. That is find the numbers of groups which are
not used as a primary group for any user.
(Hide solution)
#!/bin/sh

cut -d: -f4 </etc/passwd | sort > /tmp/uid
cut -d: -f3 </etc/group | sort | comm -23 - /tmp/uid
rm /tmp/uid
In file calories.csv replace commas (,) with hyphens (-), using diff then
determine which lines have been changed.
(Hide solution)
#!/bin/sh

tr "," "-" <calories.csv | diff calories.csv -
Sort file calories.csv according to amount of calories in increasing order.
The first header line has to remain at the beginning of the file.
(Hide solution)
#!/bin/sh

# Save the header


head -n 1 calories.csv >/tmp/header
# Sort the rest and append it to the header
tail -n +2 calories.csv | sort -t\; -k4,4n >>/tmp/header
mv /tmp/header calories.csv
Sort file calories.csv according to triple [protein, carbohydrates, fat] in
an increasing order. The first header line has to remain at the beginning
of the file.
(Hide solution)
#!/bin/sh

# Save the header


head -n 1 calories.csv >/tmp/header
# Sort the rest and append it to the header
tail -n +2 calories.csv | sort -t\; -k7,7n -k6,6n -k5,5n >>/tmp/header
mv /tmp/header calories.csv
Determine the number of different units used in the second column of
file calories.csv. First consider two units with different number as
different, then as the same, such as in case of “1 Cup” and “2 cup”.
(Hide solution)
#!/bin/sh

# In the first solution we consider two units with a different amount


# as different, e.g. “1 Cup” and “2 Cup” are different units.
tail -n +2 calories.csv | sort -t\; -k2,2 -u | wc -l
# If we wish to consider two units with different amounts as the same
# unit (i.e. we do not want to differentiate between “1 Cup” and “2
# Cup”, we can proceed in the following way:
tail -n +2 calories.csv | cut -f2 -d\; | cut -f2- -d" " | sort -u | wc -l

Create 2 subdirectories under your home directory, dir1 and dir2


At your home directory, create a new, empty text file called test1
Show the full path name of your home directory
Without changing directory, do a listing of the root directory, use an
option of the list utility so you can see what file type each file is, but
don't use the long listing option

Copy the file temp1 (currently in your home directory, from step 1) to
directory dir1, which is your current directory. You should not have to
type temp1 twice on the command line.

Stay in directory dir1.  Use one command to copy the file temp1 to
directory dir2 and call this file temp2.

Run the following commands and write the output:


 man, ls, cd, pwd,

Run the following commands and write the output:


cat, more, less, head,

Run the following commands and write the output:


tail, wc, mkdir, rmdir
Run the following commands and write the output:
cp, mv, rm, echo.

Run the following commands and write the output:


 man, ls, cd, pwd,

Run the following commands and write the output:

cat, more, less, head,

Run the following commands and write the output:


tail, wc, mkdir, rmdir
Run the following commands and write the output:
cp, mv, rm, echo.

In your home directory create a directory named DIR

Copy all files whose filenames satisfy the following conditions


to ~/DIR. The files are in /usr/include directory

Create a subdirectory called SUBDIR in your DIR directory.

The first five lines of each file you have copied from /usr/include copy
to file ~/DIR/SUBDIR/firstfive.

The last lines of files in ~/DIR copy to file ~/DIR/SUBDIR/last.

You might also like