Lab Commands
Lab Commands
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
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
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
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
ls -l | tail -n +2 | cut -c2-10
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
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.
The first five lines of each file you have copied from /usr/include copy
to file ~/DIR/SUBDIR/firstfive.