Skip to content

Instantly share code, notes, and snippets.

@ynonp
Last active November 15, 2022 09:43
Show Gist options
  • Save ynonp/8518427 to your computer and use it in GitHub Desktop.
Save ynonp/8518427 to your computer and use it in GitHub Desktop.

Part 1: Welcome to Unix

  1. Login to your Unix account
  2. Find your current shell, the default shell and a list of all installed shells.
  3. List all files and directories in /etc folder ordered by size (hint: man ls).
  4. Print the current date and time formatted as follows:
    Sun 21/03/2010, 14:30 (hint: man date)

Unix Commands Summary

Part 2: Files and directories

  1. Create a new directory for the course in your home folder with a unique name. Inside that directory create a folder named lab and inside it create the following files:

    • main.c, game.c, enemy.c, hero.c, a.out
    • monster.h, human.h
    • .highscore
  2. Create the following directories under lab

    • Music, Misc, Drivers
  3. Place copies of /etc/passwd in all 3 directories (Music, Misc and Drivers)

  4. Create a new text file named myinfo in your lab directory and write in it your name, your favorite programming language and why you love unix).

  5. Create a new directory named myinfo and move the new file you created into it. Did you get an error ?

  6. In the file main.c write a small c program that prints "Hello World" on screen. Compile it with gcc and run the result.

  7. Move all .c files to Music folder

  8. Move the folder Music with all its contents to Drivers folder

  9. The program ifconfig displays information about the server's network configuraiton. Find out where it is installed and run it. What is your server's IP address ? (hint: man locate)

  10. Try to run rmdir Drivers. Did it work ? Why ?

  11. Delete the folder Drivers

Part 3: Wildcards

  1. Create a new folder named wildcards, and cd to it.
  2. Create 5 new folders: src, include, lib, img, Doc
  3. 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.
  4. Use ls to print the details of the following:
    1. All folders whose name starts with a lowercase letter
    2. All folders whose name starts with an uppercase letter
    3. All folders whose name ends in a vowel (a, e, i, o, or u)
    4. All folders whose name does not end in a vowel
    5. All .c files in src folder
    6. All .c files in all folders
    7. All files whose name starts with the letter p, which are in a folder whose name starts with the letter i.
  5. Now delete all .png files from all folders
  6. 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 ?
  7. Would that still work if you replaced {...} with [...] ? Why ?

Part 4: File Permissions

  1. cd to wildcards folder from previous part.
  2. Change permissions of src/main.c to: r--r--r-- using letter notation.
  3. Change permissions of include/player.h to r-xrw--w- using numeric notation.
  4. Change permissions of all files under Doc to rw-------, without changing permissions for the Doc folder itself.
  5. Change permissions of lib folder and all files inside it to rwxrwxrwx.
  6. Create a new folder named test and a new file named secret inside test.
  7. Change permissions of test to ---------. Can you delete just the file secret ? Can you delete test ?
  8. Read about umask in help umask. Set the mask so all new files are created with permissions: ------r--.
  9. Restore umask to its original value

Part 5: Links

  1. Create a new folder named links (inside lab folder) and cd to it.
  2. Create 2 files: file1, file2. Inside file1 write names of some programming languages you like, and inside file2 write the names of some programming languages you don't like.
  3. Create file file3 as a symbolic link to file1
  4. Create file file4 as a hard link to file2
  5. Move file3 to ~/tmp folder. Can you still read its contents using cat ?
  6. If the answer was no, create a new file3 as a symbolic link to file1, that would survive the move to /tmp.
  7. Print out the contents of the first file using cat file3.
  8. Delete file1.
  9. Can you still see its contents through file3 ?
  10. Now move file4 to your home folder. Can you still see the contents of file2 using only file4 ?
  11. Use ls to find the inode number of files: file2, file3, file4.

Part 6: Vim

  1. Spend 30 minutes with vimtutor (and see how far you can reach :)

  2. Try to figure out what the following key sequences do in normal mode: gg, dgg, G, dG, 7G, f<letter>, F<letter>, /<word>.

  3. Copy the file /etc/passwd to your home folder

  4. Start vim on the new copy

  5. Delete the first line

  6. Move the next 4 lines to the end of the file

  7. Save and quit (use ZZ), or quit without saving with ZQ.

  8. Try to figure out what the following key sequences do in insert mode: ^u, ^y, ^t, ^n

  9. Start vim on existing file

  10. Delete first paragraph

  11. Write a new first paragraph telling 5 things you love about vim

  12. Save and Quit (using :wq or ZZ)

  13. Try to figure out what the following key sequences do in vim command line: :!ls, :%!tac, %!sort

  14. Create a new key mapping so pressing ^s vim will save the file

  15. Create a new key mapping so pressing ^x vim will save and exit

  16. Insert both new key mappings to your .vimrc file

My vim tips: https://gist.github.com/ynonp/de7e284d543be98cc442b54b2e755ee3

Part 7: Redirections and Pipes

Using ls -l, tr, cut, grep and wc, perform the following:

  1. Print the number of files and folders that have execute permission to their owner.
  2. Print the number of files and folders that have execute permissions to their owner, group and others.

Using any command you want, perform the following:

  1. /sbin/ifconfig (or on some systems ip addr show) prints 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.

  2. Print a list of all Saturday dates on May 2005 (hint: use cal or ncal).

  3. ps -ef prints a list of all processes in the system. Find how many processes match the name bash.

  4. The following input:

pbga:
xhth:
ebii:
havc:
ktlj:
fwft: ktlj, cntj, xhth
qoyq:
padx: pbga, havc, qoyq
tknk: ugml, padx, fwft
jptl:
ugml: gyxo, ebii, jptl
gyxo: 
cntj: 

Describes a "towers" structure where every named block on the right "stands" on the corresponding named block on the left. The tower described by the text looks like this:

               gyxo
              /     
         ugml - ebii
       /      \     
      |         jptl
      |        
      |         pbga
     /        /
tknk --- padx - havc
     \        \
      |         qoyq
      |             
      |         ktlj
       \      /     
         fwft - cntj
              \     
                xhth

Because fwft "stands on" tknk and "tklj", "cntj" and "xhth" stands on fwft.

Given only the input file, use unix tools to find the bottom of the tower (in our example that's tknk)

  1. Create four files in a new directory: touch one.txt two.txt three.txt four.txt, And a text file with the lines:
three.txt
one.txt

Use unix to delete from the directory all files that don't appear in the file.

Part 8: Find

  1. Under your home directory, print all files and folders recursively that:

    1. Are owned by you
    2. Are not owned by you
    3. Their name ends with a '.c'
    4. Their name starts with an a, e, i, o, or u
    5. Weight above 10M
    6. Were modified in the past 2 days.
    7. Were not modified in the past week.
  2. Create a folder called secret and inside it create a hard link to a file in your home folder. Now use find to locate both original file and the link.

  3. Create the following files: a.txt, b.txt, c.txt. Now use a single find command to create 3 backup files: a.txt.bak, b.txt.bak and c.txt.bak (as copies of the original files).

Part 9: Regular Expressions

Create a file with the following lines:

one number
two numbers
0 + 0 = 0
10101010101 is a binray odd number
10101010100 is an even number
and so is 0
and 10
0x11 is a hexa number
also 10
and also c9c8c7
no numbers here (really?)

Now use regular expressions to print only the lines from the file:

  1. That contain binary number (only 0s and 1s)
  2. That contains an even binary number. A binary string represents an even number if it ends with a 0.
  3. That contains a hexa-decimal string

Now use ls and egrep to:

  1. Print all lines from ls -l which represent a folder
  2. Print all files and folders whose name starts with an uppercase letter and ends with a number.
  3. Print all files (no folders) which have execute permission to owner
  4. All lines from ls -l which start with a d, a - or an l
  5. All lines from ls -l that are longer than 50 characters
  6. All lines from ls -l that have an even length (בעלות אורך זוגי)

Part 10: Aliases and Startup Files

  1. Write the following aliases:
    1. ll => ls -l
    2. c => clear
    3. rm => rm -i
    4. cp => cp -i
  2. Now try to overwrite an existing file with cp. Did you get a prompt?
  3. Next, use the unaliased version of cp to overwrite a file without the prompt (But don't unalias it).
  4. Write the following aliases:
    1. count_files => print the number of files in current directory
    2. bysize => list all files ordered by sizes (from large to small)
    3. lmod => list all files modified in date (date is a date string from ls -l)
    4. ff => find a file by a partial name. For example, ff ba should print all files and folders whose name contains ba.
    5. lsf => ls that show only files
    6. lsd => ls that shows only directories
    7. lsg => ls that groups first files and then directories

Part 11: User Management

  1. Create 3 new users: bob, jane, brad
  2. Create 2 new groups: marketing, sales
  3. Add user bob to group marketing
  4. Add users jane and brad to group sales
  5. Create a new system user named myapp with no home directory
  6. Create a new directory called /opt/app that can only be read and written by user myapp
  7. Create a new directory called /opt/app/data that can be read and written by user myapp and each user in group marketing
  8. Login as user bob and write some files into /opt/app/data
  9. delete user bob
  10. delete group marketing

Part 12: Package Management

  1. Install program cowsay
  2. Show all files in package cowsay
  3. The following server fails when we try to run apt-get update. Find the problem and fix it. Server address https://codepicnic.com/consoles/229751
  4. Install package apache2 and browse to your new website
  5. Delete program cowsay

console: https://codepicnic.com/consoles/bash-console-5

Part 13: Processes and Jobs

  1. Run the program xeyes in a terminal.
  2. From the same terminal, freeze xeyes with ^Z.
  3. List all jobs in the terminal. Can you find xeyes ?
  4. Send xeyes to resume in the background
  5. Terminate xeyes using kill
  6. The command ps -u <user> prints out all processes running on the server owned by user. Terminate all processes named vim running under your user, in a single command.

Part 14: Awk and Sed

  1. Add a blank line after each line of input

  2. Print a '> ' in the beginning of each line

  3. Use awk that take a file name as input and performs:

    1. If a line starts with #, replace each character with a -
    2. For all other lines, replace each character with a . (print result to screen)
  4. Replace the first and last word in every line

  5. The command /sbin/ifconfig prints 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment