- Login to your Unix account
- Find your current shell, the default shell and a list of all installed shells.
- current shell:
echo $0 - default shell:
echo $SHELL - all shells:
cat /etc/shells
- List all files and directories in
/etcfolder ordered by size (hint:man ls).
ls -lS /etc/
- Print the current date and time formatted as follows:
Sun 21/03/2010, 14:30 (hint:man date)
date +'%a %d/%m/%Y, %H:%M'
- Create a new directory for the course in your home folder called
lab. Inside, create the following files:main.c,game.c,enemy.c,hero.c,a.outmonster.h,human.h.highscore
mkdir lab
cd lab
touch main.c game.c enemy.c hero.c a.out monster.h human.h .highscore
- Create the following directories under
labMusic,Misc,Drivers
mkdir Music Misc Drivers
- Place copies of
/etc/passwdin all 3 directories (Music, Misc and Drivers)
cp /etc/passwd Music
cp /etc/passwd Misc
cp /etc/passwd Drivers
- Create a new text file named
myinfoin your home directory and write in it your name, your favorite programming language and why you love unix).
- pico myinfo
- Create a new directory named
myinfoand move the new file you created into it. Did you get an error ? Can you fix it ?
mkdir myinfo.d
mv myinfo myinfo.d
mv myinfo.d myinfo
- In the file main.c write a small c program that prints "Hello World" on screen. Compile it with
gccand run the result.
gcc main.c
./a.out
- Move all .c files to Music folder
mv main.c game.c enemy.c hero.c Music
- Move the folder Music with all its contents to Drivers folder
mv Music Drivers
- The program
ifconfigdisplays information about the server's network configuraiton. Find out where it is installed and run it. What is your server's IP address ?
locate ifconfig
/sbin/ifconfig
- Try to run
rmdir Drivers. Did it work ? Why ?
- Didn't work. Directory not empty
- Delete the folder
Drivers
rm -r Drivers
-
Create a new folder named wildcards, and
cdto it.mkdir wildcards cd wildcards -
Create 5 new folders:
src,include,lib,img,Docmkdir src include lib img Doc -
Create 7 new files:
src/main.c,src/player.c,include/player.h,lib/zlib.c,img/hero.png,img/player.png,doc/README.txt,doc/test.c.touch src/main.c src/player.c include/player.h lib/zlib.c img/hero.png img/player.png doc/README.txt doc/test.c -
Use
lsto print the details of the following:-
All folders whose name starts with a lowercase letter
ls -d [a-z]* -
All folders whose name starts with an uppercase letter
ls -d [A-Z]* -
All folders whose name ends in a vowel (a, e, i, o, or u)
ls -d *[aeiou] -
All folders whose name does not end in a vowel
ls -d *[^aeiou] -
All .c files in src folder
ls src/*.c -
All .c files in all folders
ls */*.c -
All files whose name starts with a p, which are in a folder whose name starts with an i.
ls i*/[ap]*
-
-
Now delete all .png files from all folders
rm */*.png -
Using curlies we can create command line repeaters that are not bound to current file status. Try to execute the following:
mkdir -p {a,b,c}/{1,2,3}. How many new folders were created ? -
Would that still work if you replaced {...} with [...] ? Why ?
-
cdto wildcards folder from previous part. -
Change permissions of
src/main.cto:r--r--r--using letter notation.chmod a=r src/main.c -
Change permissions of
include/player.htor-xrw--w-using numeric notation.chmod 562 include/player.h -
Change permissions of all files under Doc to
rw-------, without changing permissions for the Doc folder itself.chmod 600 Doc/* -
Change permissions of lib folder and all files inside it to
rwxrwxrwx.chmod -R 777 lib -
Create a new folder named
testand a new file namedsecretinside test.mkdir test touch test/secret -
Change permissions of
testto---------. Can you delete just the filesecret? Can you deletetest?chmod 000 test -
Read about
umaskinman csh. Set the mask so all new files are created with permissions:------r--.umask 773
-
Create a new folder named
linksand cd to it.mkdir links; cd links -
Create 2 files:
file1,file2. Insidefile1write names of some programming languages you like, and insidefile2write the names of some programming languages you don't like.echo perl > file1 echo python > file2 -
Create file
file3as a symbolic link tofile1ln -s file1 file3 -
Create file
file4as a hard link tofile2ln file2 file4 -
Move
file3to /tmp folder. Can you still read its contents usingcat?mv file3 /tmp -
If the answer was no, create a new
file3as a symbolic link tofile1, that would survive the move to/tmp.ln -s `pwd`/file1 file3 mv file3 /tmp -
Print out the contents of the first file using
cat file3.cat tmp/file3 -
Delete
file1.rm file1 -
Can you still see its contents through
file3?no -
Now move
file4to your home folder. Can you still see the contents offile2using onlyfile4?mv file4 ~ cat ~/file4 -
Use
lsto find the inode number of files:file2,file3,file4.ls -li ~/file4 /tmp/file3 file2
- Try to figure out what the following key sequences do in normal mode:
gg,dgg,G,dG,7G,f<letter>,F<letter>,/<word>.
- gg: go to start of file
- dgg: delete till start of file
- G: go to end of file
- dG: delete till end of file
- 7G: go to line 7
- f: go forward in line till letter is found
- F: go backward in line till letter is found
- /word: search word in file
- Copy the file
/etc/passwdto your home folder
- cp /etc/passwd ~
- Start vim on the new copy
- vim ~/passwd
- Delete the first line
- Gdd
- Move the next 4 lines to the end of the file
- 4ddGp
- Save and quit (use ZZ), or quit without saving with ZQ.
- ZZ
- Try to figure out what the following key sequences do in insert mode:
^u,^y,^t,^n
- ^u: delete typed text
- ^y: copy from line above
- ^t: insert tab
- ^n: auto complete
- Start vim on existing file
- vim ~/passwd
- Delete first paragraph
- ggd}
- Write a new first paragraph telling 5 things you love about vim
- imodal editing is cool<Esc>yy5p
-
Save and Quit (using
:wqorZZ) -
Try to figure out what the following key sequences do in vim command line:
:!ls,:%!tac,%!sort
- !ls - run ls
- %!tac - run tac on buffer and send result back to the editor
- %!sort - run sort on buffer and send result back to the editor
- Create a new key mapping so pressing
^svim will save the file
inoremap <C-s> <Esc>:w<cr>a
- Create a new key mapping so pressing
^xvim will save and exit
inoremap <C-x> <Esc>ZZ
- Insert both new key mappings to your
.vimrcfile
-
who -uprints out idle time for each connected user. Print out the 3 users who were idle the longest time.who -u | tr -s ' ' | grep -v old | grep -v \. | sort -t ' ' -k 6 | head -3 -
Print the number of files that have execute permission to their owner.
ls -l | cut -c4 | grep x | wc -l -
Print the number of files that have execute permissions to their owner, group and others.
ls -l | cut -c4,7,10 | grep xxx | wc -l -
Print the number of files that were modified on a given date.
ls -l | tr -s ' ' | cut -d' ' -f5,6 | grep 'Jan 21' | wc -l -
Print a list of all files modified TODAY.
ls -l | tr -s ' ' | cut -d' ' -f5,6 | grep '`date +"%b %d"`' | wc -l -
/sbin/ifconfigprints a lot of information about your network connections. Use filters to get only the IP address of the host.
Remember 127.0.0.1 is just a loopback IP, so we should ignore that one.ifconfig |grep inet|grep -v inet6|grep -v 127.0.0.1 -
Print a list of all Saturday dates on May 2005.
cal 5 2005| cut -c19-20 -
ps -efprints a list of all processes in the system. Find how many processes match the namecsh.ps -ef | grep csh | grep -v grep -
The command
curl http://graph.facebook.com/103113856395914prints out information about the facebook page "Unix". Find out how many likes the page has.curl -s http://graph.facebook.com/103113856395914| rev | cut -d: -f1 | cut -c2-|rev
-
Under your home directory, print all files and folders recursively that:
-
Are owned by you
find . -user `whoami` -
Are not owned by you
find . -not -user `whoami` -
Their name ends with a '.c'
find . -name '*.c' -
Their name starts with an a, e, i, o, or u
find . -name '[aeiou]*' -
Weight above 10M
find . -size +10M -
Were created more than 3 hours ago.
find . -cmin +240 -
Were modified in the past 2 days.
find . -mtime +2 -
Were not accessed in the past week.
find . -ctime +7
-
-
Create the following files:
a.txt,b.txt,c.txt. Now use a singlefindcommand to create 3 backup files:a.txt.bak,b.txt.bakandc.txt.bak(as copies of the original files).find . -type f -exec cp '{}' '{}'.bak \;
-
Given the following lists of positive and negative words, type a regular expression that matches all words in positive, but doesn't math any of the words in negative.
Positive Negative pit pt spot Pot spate peat slap two part respite Regexp: p.t
Positive Negative rap them aleht tapeth happy them apth tarpth wrap/try Apt sap tray peth 87ap9th tarreth apothecary ddapdg apples shape the Regexp: p.?t
-
Write a regular expression to accept any binary string (only 0s and 1s)
^[01]+$ -
A binary string represents an even number if it ends with a 0. Write a regular expression to match an even number represented as a binary string.
^[01]*0$ -
Write a regular expression to accept any hexa-decimal string
^[0-9a-fA-F]+$ -
Print all lines from
ls -lwhich represent a folderls -l | egrep '^d' -
Print all files and folders whose name starts with an uppercase letter and ends with a number.
ls | egrep '^[A-Z].*[0-9]$' -
Print all files (no folders) which have execute permission to owner
ls -l | egrep '^-..x' -
All lines from
ls -lwhich start with a-, anlor adls -l | egrep '^[-ld]' -
All lines from
ls -lthat are longer than 50 charactersls -l | egrep '.{50}' -
All lines from
ls -lthat have an even length (בעלות אורך זוגי)ls -l | egrep '^(..)*$'
-
Write the following aliases:
-
ll => ls -l
-
c => clear
-
rm => rm -i
-
cp => cp -i
alias ll "ls -l" alias c clear alias rm "rm -i" alias cp "cp -i"
-
-
Now try to overwrite an existing file with
cp. Did you get a prompt?yes -
Next, use the unaliased version of
cpto overwrite a file without the prompt (But don't unalias it).\cp /etc/passwd ~/copy -
Write the following aliases:
-
count_files => print the number of files in current directory
-
bysize => list all files ordered by sizes (from large to small)
-
lmod => list all files modified in date (date is a date string from ls -l)
-
ff => find a file by a partial name. For example,
ff bashould print all files and folders whose name containsba.alias count_files "find . -type f -maxdepth 1 | wc -l" alias bysize "ls -lS | egrep -v '^d'" alias lmod "ls -l | egrep " alias ff "find . -name '*'\!*'*'"
-
-
Run the program
xeyesin a terminal.xeyes -
From the same terminal, freeze
xeyeswith ^Z. -
List all jobs in the terminal. Can you find
xeyes?jobs -
Send
xeyesto resume in the backgroundbg -
Terminal
xeyesusing killkill %1 -
The command
ps -u <user>prints out all processes running on the server owned by user. Terminal all processes named vim running under your user, in a single command.ps -ef | grep vim | awk '{print $2}' | xargs kill -9
- Add a blank line after each line of input
ls -l | awk '{ printf("%s\n\n", $0) }'
- Print a '> ' in the beginning of each line
ls -l | awk '{ printf("> %s\n", $0) }'
- Use awk that take a file name as input and performs:
- If a line starts with
#, replace each character with a- - For all other lines, replace each character with a
.(print result to screen)
- If a line starts with
cat /etc/shells | awk '/^[^#]/ { gsub(".", "-", $0) } /^#/ { gsub(".", ".", $0) } { print }'
- Replace the first and last word in every line
- The command
/sbin/ifconfigprints out information about network interfaces. Here's a sample output for the command:
en3: flags=8963 mtu 1500
options=60
ether 32:00:18:24:c0:00
media: autoselect
status: inactive
p2p0: flags=8843 mtu 2304
ether 06:38:35:47:96:24
media: autoselect
status: inactive
Use awk to parse the information generated by ifconfig and create an information file for each network interface. For example the above interface should produce two files: first is named en3.info and has the en3 block, and the second is named p2p0.info and has the second block
/sbin/ifconfig | awk '/^[a-z]/ { fname = $1 } { print > fname }'