Operating systems 1 -- 2008-2009 -- info.uvt.ro/Laboratory 2

From Wikiversity
Jump to navigation Jump to search

Quick links: front; laboratories agenda, 1, 2, 3, 4, 5, 6, projects, evaluation, tools, references.


File system paths[edit]

  • examples:
    • absolute paths:
/home/ciprian/test.sh
/tmp
    • relative paths:
./test.sh
a/b/c
../a/b/c
test.sh
    • ~ expansion:
~/abc

Basic bash commands[edit]

Simple commands[edit]

  • syntax:
    • a list of blank separated words (strings);
    • the first word specifies the command to be invoked;
    • the remaining words are passed as arguments;
    • the end is marked by a control character:
      • end of line;
      • &, &&, ||, ;;
<command> [<argument>] ...
  • examples:
    • prints Hello world:
echo Hello world!
echo 'Hello world!'
    • displays the manual page for bash:
      • use q to exit;
man bash
    • displays the current user:
whoami

List of commands[edit]

  • syntax:
    • a list of simple commands;
    • separated by: ;, &&, ||;
    • optionally terminated by ; or &;
    • attention to the spacing;
<command> [argument] ... { <; | && | ||> <command> [argument] ... } ...
  • example:
    • displays hello world on two distinct lines:
echo hello ; echo world
    • displays the date at two seconds apart:
date ; sleep 2 ; date

Comments[edit]

  • in almost all situations characters following a # and up to the end of the line are treated as a comment:
echo a # b c

Basic bash script[edit]

  • editing:
nano ./test.sh
  • example script:
#!/bin/bash
echo hello world
exit 0
  • making it executable:
chmod +x ./test.sh
  • executing it:
./test.sh

Generic commands[edit]

File system commands[edit]

Current directory[edit]

  • syntax:
pwd
cd
cd <directory>
  • examples:
pwd
cd /tmp
pwd
cd
pwd

Viewing[edit]

cat[edit]

  • syntax:
cat [flag] ... [files] ...
  • example:
cat ./test.sh
cat -n ./test.sh
  • important flags:
    • -b or --number-nonblank;
    • -n or --number;
    • -s or --squeeze-nonblank;
  • references:

stat and file[edit]

  • syntax:
stat <file> ...
file <file> ...
  • examples:
    • detailed information about a file or directory:
stat /etc
stat /etc/group
    • type information about a file or directory:
file /etc/group
file /bin/bash

less and more[edit]

  • syntax:
less [file]
more [file]
  • examples:
    • displays the content of the /etc/services file:
      • use q to exit;
less /etc/services
more /etc/services
cat /etc/services | less

Directory creation[edit]

  • syntax:
mkdir [flag] ... <directory>
  • example:
    • shows that the directory dir1 does not exist (in the current working directory);
    • creates the directory dir1;
stat ./dir1
mkdir ./dir1
stat ./dir1
    • creates a directory and its parents:
mkdir ./dir2/dir3
mkdir -p ./dir2/dir3
  • important flags:
    • -p or --parrents;
  • references:

Directory deletion[edit]

  • syntax:
rmdir [flag] ... <directory>
  • example:
    • deletes the empty folder dir1:
stat ./dir1
rmdir ./dir1
stat ./dir1
    • deletes the empty folder and its (empty parents):
rmdir ./dir2/dir3
rmdir -p ./dir2/dir3
  • important flags:
    • -p or --parrents;
  • references:

File creation[edit]

  • syntax:
touch <file>
  • example:
    • shows that the file file1 does not exist and creates it:
stat ./file1
touch ./file1
    • shows that if the file already exists it's left as it is (only the timestamp is modified):
stat ./file1
touch ./file1
stat ./file1

File deletion[edit]

  • syntax:
rm [flag] ... <file> ...
  • example:
    • deletes the file file1
stat ./file1
rm ./file1
stat ./file1
    • it demonstrates that we can not delete a directory that contains files or other folders:
mkdir dir4
touch dir4/file4
rm dir4
    • we have to delete its children first:
rm dir4/file4 dir4
    • or we can use the -R (recursive) flag to delete the directory and its children in one step:
mkdir dir5
touch dir5/file5
rm -R dir5
  • important flags:
    • -i or --interactive;
    • -R or --recursive;
  • references:

Copying[edit]

  • syntax:
cp [flag] ... <sourc> <target>
  • example:
    • copying only one file:
cp /etc/group .
stat ./group
    • copying one file under a new name:
cp /etc/passwd ./passwd-copyied
stat ./passwd-copyied
    • creating a directory hierarchy:
mkdir ./dir6
cp ./group ./dir6
stat ./dir6/group
    • trying to copy a directory (does not work unless we give it the -R flag):
cp ./dir6 ./dir7
    • using the -R flag:
cp -R ./dir6 ./dir7
stat ./dir7/group
    • showing that cp overwrites the destination file (if it exists):
cat ./group
cp /etc/passwd ./group
cat ./group
  • important flags:
    • -i or --interactive;
    • -R or --recursive;
  • references:

Moving[edit]

  • syntax:
mv [flag] ... <sourc> <target>
  • examples:
    • renaming a directory under one that already exists should not work:
stat ./dir6
stat ./dir7
mv ./dir6 ./dir7
    • but we can rename it under a name that does not exist:
      • we demonstrate that mv moves the entire hierarchy (without -R);
mv ./dir6 ./dir8
stat ./dir8/group
    • mv overwrites the destination file (just like cp):
cat ./passwd-copyied
cp /etc/group ./group
cat ./group
mv ./passwd-copyed ./group
cat ./group
  • important flags:
    • -i or --interactive;
    • -R or --recursive;
  • references:

Querying[edit]

ls[edit]

  • syntax
ls [flag] ... <directory>
  • examples:
ls /tmp
ls ~
ls -a ~
ls -l ~
ls -a -l ~
ls -al ~
ls /etc
ls -1 /etc
  • important flags:
    • -a or -A;
    • -l (with -h and / or -n);
    • -1;
  • references:

find[edit]

  • syntax:
find <directory> [test] ... [action] ...
  • examples:
find /etc
find /etc -type d
find /etc -type f
find /etc -name 'g*'
  • important (flags) tests:
    • -type;
    • -name and -iname;
  • important actions:
    • -print;
    • -ls;
    • -exec;
  • references: