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

Unix-Like-Systems

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Unix-Like-Systems

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

Introduction to UNIX-like systems

Computing Laboratory

http://www.isical.ac.in/~dfslab

Computing Lab (ISI) Introduction to UNIX-like systems 1 / 20


Outline

1 Getting started

2 File system hierarchy

3 Commands

Computing Lab (ISI) Introduction to UNIX-like systems 2 / 20


Your desktop

File manager

Search for applications

Computing Lab (ISI) Introduction to UNIX-like systems 3 / 20


What you will need
Code editor: Visual Studio Code (if you do not already have a
preferred editor)
search for “linux vscode setup for c programming”
Text editor (gedit): for quick edits
Terminal (image source: https://linux.how2shout.com/)

Computing Lab (ISI) Introduction to UNIX-like systems 4 / 20


Outline

1 Getting started

2 File system hierarchy

3 Commands

Computing Lab (ISI) Introduction to UNIX-like systems 5 / 20


File system structure

Files are organised in a hierarchical structure of folders, sub-folders, and


files.

Courtesy: https://www.slideshare.net/okmomwalking/windows-7-unit-b-ppt

Computing Lab (ISI) Introduction to UNIX-like systems 6 / 20


File system hierarchy

Computing Lab (ISI) Introduction to UNIX-like systems 7 / 20


File system hierarchy

Computing Lab (ISI) Introduction to UNIX-like systems 7 / 20


File system structure: terminology

Folders ≡ directories
Top of the hierarchy: root directory (/)
Default starting location: home directory (~)
Location of a file or directory: specified by path
Current location in terminal or file browser: current working directory
Paths: absolute or relative
absolute path: from root (starts with /)
Example: /usr/bin/firefox, /tmp, /user1/student
relative path: from current working directory (does not start with /)
Example: clab/assignment1/hello.c

Note the difference between (forward) slash (/, used in Unix-like


systems) and backslash (\, used in Windows-like systems) !

Computing Lab (ISI) Introduction to UNIX-like systems 8 / 20


Graphical file manager

Computing Lab (ISI) Introduction to UNIX-like systems 9 / 20


Managing files from the terminal

Navigating the file system


cd : change directory
Example:
cd /home/student/Desktop
cd clab/day1/
cd ←− go to home directory
pwd : print current working directory

Computing Lab (ISI) Introduction to UNIX-like systems 10 / 20


Managing files from the terminal

Navigating the file system


cd : change directory
Example:
cd /home/student/Desktop
cd clab/day1/
cd ←− go to home directory
pwd : print current working directory

Special directory names


∼ : home directory
Example: cd ∼/Desktop
. (dot) : current working directory
Example: ./program1
.. (dot dot) : parent directory (one level up)
Example: cd .., cd ../assignment2, cd ../..
Computing Lab (ISI) Introduction to UNIX-like systems 10 / 20
Managing files from the terminal
Listing files
ls : view list of files in current directory
ls <path> : view list of files in specified path
ls -l : view detailed list of files
ls -lt : view detailed list of files sorted by modification time
ls -ltr : view detailed list of files sorted by modification time in
reverse order

Computing Lab (ISI) Introduction to UNIX-like systems 11 / 20


Managing files from the terminal
Listing files
ls : view list of files in current directory
ls <path> : view list of files in specified path
ls -l : view detailed list of files
ls -lt : view detailed list of files sorted by modification time
ls -ltr : view detailed list of files sorted by modification time in
reverse order

Example:
$ /bin/ls -l
total 68
drwx------ 2 mandar mandar 4096 Jul 19 00:45 assignments
drwx------ 2 mandar mandar 4096 Jul 22 2016 exams
-rw-r--r-- 1 mandar mandar 13521 Jul 19 00:41 index.html
drwx------ 2 mandar mandar 4096 Jul 19 00:45 lectures
Computing Lab (ISI) Introduction to UNIX-like systems 11 / 20
Essential commands: permissions

drwx- - - - - - 2 mandar mandar 4096 Jul 19 00:45 lectures

Permissions Size Modification time

Computing Lab (ISI) Introduction to UNIX-like systems 12 / 20


Essential commands: permissions

drwx- - - - - - 2 mandar mandar 4096 Jul 19 00:45 lectures

Permissions Size Modification time

Permissions:
9 possible permissions:
{ read, write, execute } × { user (owner), group, other (everyone else) }
9 bits (1 ≡ permission granted)
ur uw ux gr gw gx or ow ox
chmod: changing permissions
Example:
chmod g+wx <path>
chmod og-wx <path>
chmod 644 <path> ←− 644 ≡ 110 100 100 ≡ rw-r--r--
chmod 700 <path> ←− 700 ≡ 111 000 000 ≡ rwx------

Computing Lab (ISI) Introduction to UNIX-like systems 12 / 20


Essential commands: directories

mkdir : create a directory


Examples:
mkdir clab
mkdir clab/assignment1
Create directories as appropriate.
OR
mkdir -p clab/assignment1 clab/assignment2
rmdir : remove an (empty) directory
Example: rmdir assignment2, rmdir clab/programs

Computing Lab (ISI) Introduction to UNIX-like systems 13 / 20


Outline

1 Getting started

2 File system hierarchy

3 Commands

Computing Lab (ISI) Introduction to UNIX-like systems 14 / 20


Compiling and running your program

Compiling
command options / flags other arguments
gcc -g -Wall -o prog1 prog1.c

OR

gcc -g -Wall prog1.c


Running
./prog1 OR ./a.out
OR
./prog1 input.txt output.txt
etc.

Computing Lab (ISI) Introduction to UNIX-like systems 15 / 20


Essential commands: files

cp : copy a file
Example:
cp program1.c program2.c
cp -i source-file target-file
cp -i source-file target-directory

Computing Lab (ISI) Introduction to UNIX-like systems 16 / 20


Essential commands: files

cp : copy a file
Example:
cp program1.c program2.c
cp -i source-file target-file
cp -i source-file target-directory
mv : rename (move) a file
Example:
mv program1.c program2.c
mv -i source-file target-file
mv -i source-file target-directory

Computing Lab (ISI) Introduction to UNIX-like systems 16 / 20


Essential commands: files

cp : copy a file
Example:
cp program1.c program2.c
cp -i source-file target-file
cp -i source-file target-directory
mv : rename (move) a file
Example: -i ≡ interactive
mv program1.c program2.c (asks for confirmation)
mv -i source-file target-file
mv -i source-file target-directory
rm : remove (delete) a file
Example:
rm program1.c
rm -i file1 file2.c *.bak
rm -r some-directory (remove directory and everything inside it)

Computing Lab (ISI) Introduction to UNIX-like systems 16 / 20


Viewers / pagers

Useful for quickly viewing a file (not editing)


Use less
Example: less cs19xx-day0-prog1.c
space: move forward one page
backspace or b: move backward one page
q : exit the pager
/ : search for a string in the file
run man less for more information

Computing Lab (ISI) Introduction to UNIX-like systems 17 / 20


Input/output redirection

Input/output
involves a file or a terminal
requires a file pointer
stdin ≡ file pointer corresponding to reading input from keyboard
stdout ≡ file pointer corresponding to printing output to terminal
Taking stdin from a file: ./prog1 < input.txt
Printing stdout to a file: ./prog1 > input.txt
Taking stdin / printing stdout from / to a program: use the vertical bar /
pipe character (|)
./prog1 | less
cat input.txt | ./prog1
Input/output from/to file / program may be combined
./prog1 < input.txt > output.txt

Computing Lab (ISI) Introduction to UNIX-like systems 18 / 20


Other commands

man
Example: man ls, man cp, man rm

Computing Lab (ISI) Introduction to UNIX-like systems 19 / 20


Other commands

man
Example: man ls, man cp, man rm

Find out more about these on your own.


alias (giving your own, easy-to-remember names to commands)
wc (counting characters, words, lines)
sort
head, tail (first few / last few lines)
cmp, diff (comparing two files)
ps, top, kill (checking what programs are running)
find (finding files or directories)
grep (searching for patterns)
awk, sed (programming)

Computing Lab (ISI) Introduction to UNIX-like systems 19 / 20


Useful references / cheat-sheets

http://cli.learncodethehardway.org/bash_cheat_sheet.pdf
https://ubuntudanmark.dk/filer/fwunixref.pdf
http://www.ucs.cam.ac.uk/docs/leaflets/u5
http://mally.stanford.edu/~sr/compuGng/basic-unix.html
http://www.math.utah.edu/lab/unix/unix-commands.html

Computing Lab (ISI) Introduction to UNIX-like systems 20 / 20

You might also like