EE485A Lecture 02-UnixCommands
EE485A Lecture 02-UnixCommands
kyoungsoo@eelab5:~$
3
Linux Shell
Super user vs. Normal user
root can run all commands
e.g., root can run ‘shutdown –h now’, which stops the OS and turns it off
Normal users can run only a subset of them
Command whoami: gets the username
4
sudo, su, and man
sudo [command]
Superuser-do: runs a command as root
$ sudo shutdown –h now
Runs ‘shutdown –h now’ as root, asks for the root password before running the command
su [user-to-be-switched-to]
Switches the current user to user-to-be-switched-to
$ su yjwon
Switches to user ‘yjwon’. It would ask for the password of ‘yjwon’ before switching
$ su
Switches to root. It would asks for the root password
man [command]
Shows the manual of command
5
Directory Layout
Where are the programs located?
/: root directory. (not root user) the starting point for all files
6
Commands Related to Directories/Files
ls: list directory contents
$ ls –l // list files with detailed attributes per each file (time, size, permission, etc.)
7
Commands Related to Directories/Files
mkdir/rmdir [dir]: create/remove [dir]
mv [A] [B]
cp [A] [B]
Copy file A to file B è the same as mv except it does not remove the original copy [A]
8
Miscellaneous & Environment Variables
echo [argument]: displays arguments to the screen
9
Environment Variables
A number of variables that are used to run the program
They include
$PATH: a colon-separated list of directories that the shell searches for commands
$ ls
à The bash shell searches ‘ls’ in /bin first, and runs it (/bin/ls) if it’s found.
Otherwise, /usr/bin will be searched and so on. If no executable command is found in the
directories, then the shell says it cannot be found.
10
Environment Variables
Other environment variables
$HOME: the location of the user's home directory
$PWD: this variable points to the current directory
$DISPLAY: the identifier for the display that X11 should use by default
11
which and –h option
which [command]: displays the location of command
$ which ls
/bin/ls
$ which which
/usr/bin/which
Hints: -h option
$ cal -h
12
Working with Directories
Directory: a container of files and other directories
Tree-like structure
Special directories
. : this directory
$ cd /var/tmp
the same as cd /var/tmp/
13
How to Execute a Program in Current Directory?
$ program
$ ./program
$ ~/program
‘~’ represents my home directory
$ /mnt/home/kyoungsoo/program
Full path (/mnt/home/kyoungsoo/) of my home directory
14
Creating/Removing Directories
15
Listing Files with ls
$ ls –l
Other options:
-t: sort by last modified time (i.e., most recently modified files first)
16
Dealing with Spaces in File Names?
How many files do you see?
$ rm ‘a file’ // or rm “a file”
17
File permission
drwxr-xrwx
u g o
• first character: file type: '-' (regular), 'd' (directory), 'l' (symbolic link: acts as a pointer to
another file)
• Permission: r: readable, w: writable, x: executable
Octal representation
dr-x------
5 0 0
18
File and Directory Permissions
19
Change File Permissions
chmod [mode] [file]
Changes the modes (or permissions) of a file
mode format: user-category +-= rwx
User-category: u, g, o
+: add, -: remove, =: explicitly set the mode
20
Default File Creation Mode
Base permission for a newly created files?
777 for a directory (all can read/write/enter)
666 for a file (all can read and write) The first number: setuid/setgid/sticky
Ignore it for now
How to change the behavior above?
umask [-S] [mode]
Sets file creation mode
Applied permissions = base permission – [mode]
Mode: 3 octal numbers
Or symbolic notation with –S option
$ umask 022
Directory: 777 – 022 = 755 (rwxr-xr-x)
File: 666 – 022 = 644 (rw-r--r--)
21
Directory Permissions
Make it readable/enterable
22
Finding Files
find [path…] [expression…]
Recursively finds files in the path that match the expression
No arguments? List all files in the current directory & sub directories
Examples
find . –name pattern // -iname: case-insensitive
$ find . –type d –newer b.txt // directories that are newer than b.txt
find . –exec command {} \; // run command for each searched file
$ find . –exec file {} \; // run ‘file’ (it reports the type) for every search file
If you know file names (or pattern), but don’t know the location?
$ locate file-name-pattern // will show you the path where the file resides
23
Comparing Files
diff [file1] [file2] • ‘3c3’: line 3 (first file) ‘c’ line 3
(second file)
Shows the difference between file1 and file2 • c: line changed
No output if file1 and file2 are identical • d: line deleted
• a: line added
sdiff is similar but the output is slightly different
• ‘<‘: first file content, ‘>’: second file
content
| : different line
<: exists on only the first file
>: exists on only the second file
24
Searching in Files
Other options
25
I/O Redirection
Redirects standard output/input/error of a program to/from a different location
A program runs with three standard “files” by default: standard input/output/error
A file in unix is an abstract concept: any device that can do input and/or output can
be a file
Default standard output (file descriptor 1) or error (file descriptor 2): screen
Examples
$ ls –l /etc/ > etc.txt // the output of ‘ls –l’ is written to a file, etc.txt
26
Pipe
pipe (|) feeds the standard output from the previous command into the standard
input of the next command
$ strings music.mp3 | grep –i BTS
strings: extract strings from a binary file and prints them out to the standard output
The output of strings is fed into the standard input of the next command
Each program does one thing very well. Pipe allows users to connect multiple programs to
flexibly achieve what they want
Commands popular for piping: awk, cat, cut, fmt, join, less, more, n1, pr,
sed, seq, sort, tr, and uniq
27
Not All Commands are Covered
tar // save multiple files into one file (or restore multiple files from one file)
There are many useful tools, but don’t need to learn them all at once
28
Assignment for Lecture 2
Please take a snapshot of taking the following steps and upload the JPG to the
submission site
2. Change directory to ‘/’ (you should run all commands in this directory)
3. Use gcc209 to compile ~/hello.c and save the output (-o) under ~/hello/ with the name ‘hello’
29
30