Tutorials

How to Use

Unix Shell

The first commands / Handling the directories / Handling directories and files

As mentioned earlier that Mac OS X is a unix-based, it is true but it has a lot of restrictions and it does not have extra options that can be made with commands, therefore Linux is the best with unix "flavours" that you can experience.

The kernel: The kernel of UNIX is the hub of the Operating System and it handles data processing and the files. For example; if a user types rm myfile, it removes the file "myfile". The command rm means remove and when entering rm, it request the kernel to execute. When the kernel is finished running rm myfile, the shell returns the prompt % and it is then waiting for further commands.

The shell: It is an interface between the user and the kernel. When a user logs in, the program checks the username and password. When granted, it starts another programs which is called the shell. The shell uses the percent sign for a prompt % or a dollar sign $.

The directory structure:

The directory structure is hierarhical and the top of hierarchy is called root and it is written as a forward slash /. It does not use drive letters vice versa Windows.
Unix diagram

If your chosen Linux is installed, you can access to the terminal from Application or Accessories menu. Or for a shortcut, press Ctrl + Alt + T all at once. You will see a screenshot of my terminal with the useful UNIX commands. When you see my terminal that says chrisfiorito@ubuntu:~$, I am logged in using Ubuntu OS.
Terminal

  Try the very first commands you can get the idea! Nothing is being a harm to your computer. Just remember that all commands have to be lowercase.

whoami - "Who am I" tells what your user name is.
Command: whoami

cal - Calendar, it shows the current month of calendar.
Command: cal

cal 2012 - It shows the 12 month calendar of 2012. You can check a calendar within a range of year 1 and 9999.
Command: cal 2012

  Now, what you can do is handling the directories:

ls - List files and directories, so you should be able to find out what is in your currently directory.
Command: ls

ls -a – List all hidden and visible files and directories you are currently in the directory.
Command: ls -a

mkdir - Make a directory, it is like creating a new folder with empty set inside.
Command: mkdir

cd directory_name – Change directory – you are able to change the directory wherever you are inside the current directory. You type cd unixstuff, and it will take you to chrisfiorito@ubuntu:~/unixstuff$.
Command: cd directory_name

cd .. - Please notice that there is a space between cd and two dots, you need to do that otherwise it won't work. This is to change to upper directory. If you are in mechant directory, when you type cd .., it will take you to cjf directory (It goes up in the hierarchical directory).
Command: cd ..

cd – it takes you back to home directory, this would be chrisfiorito@ubuntu:~$ known as /home in hierarchical structure.

pwd – It stands for print working directory. Beware, you may have thought this as password but it doesn't do anything with a password and it doesn't mean password either! It just shows you a full path of the directory. If you are in mechant directory, and when you type pwd, it'll show you /home/cjf/mechant so you can determine where you are at for good track-keeping.
Command: pwd

clear – If you have typed so many commands lately and you want to clear out your activities on the terminal screen, just type clear.

  Now you are going to deal with the directory and files beside making directory (mkdir – you already know that). Take a look at this picture so you can get the idea for the rest of this activity.
Unix diagram

cp – Copy, if you are in the directory /home/cjf/mechant and you see a file report.doc, the proper syntax of the command is cp report.doc report2.doc, you are then duplicating the file report.doc and a new file report2.doc now exists in the same directory. How would you copy a file from different directory to mechant directory? This will do – cp /home/sxg/fou/budget.xls . - Please notice that there is a space between the path of directories with a file and the final dot, otherwise it won't work. The dot at the end is to paste a file budget.xls in the current directory, like if you are in mechant directory, the dot will place the copied files there from other directory.

mv – Move, if you are in the directory /home/cjf/mechant and you see a file info.doc, the proper syntax of the command is mv info.doc /home/cns/relaxe/. , you are then moving the file info.doc to relaxe directory with the same name because of dot at the end. If you would like to move a file and rename. This will do, I am moving info.doc from relaxe to sportifmv /home/cns/relaxe/info.doc /home/cns/sportif/information.doc – (You will have to type a full path of both places if you are in neither one of those. Like I said I am still in mechant directory.) Now, the info.doc is moved to sportif but it is renamed to information.doc.

mv is also the command if you need to rename. If you want to rename reelz.avi to footage.avi, this will do if you are in the current directory dingue with reelz.avimv reelz.avi footage.avi. Mv also can rename the directory, you will need to go up one level of directory to sxg, you type cd .. and then rename the directory is mv dingue cingle. Dingue directory is renamed to cingle.

rm – Remove, it deletes a file. If I'm in mechant directory and I no longer need report.doc. Here's the syntax rm report.doc, then it is gone. Please be careful, once you attempt to delete a file, there will be NO warning "Are you sure to delete the file?" - It'll delete at once and permanently you prompt the command. The files won't be in the trash with the command instead you use graphical user interface.

rm *.doc - Asterisk is a wildcard that removes all .doc files in the current directory.

rm report.* - If you have report.doc, report.xls, report.jpg, report.ppt, and so on, it will delete all the file report regardless the file type.

rm *.* - You know, it erases everything at once.

rmdir – Remove the directory, it deletes a empty directory. You cannot delete a directory if it is not empty. rm *.* will do at first and then cd .. so you can move directory up one level, if I'm in mechant which is empty and I want to remove mechant, I have to be in cjf directory. The syntax is rmdir mechant. It is gone.

If you are all done with the terminal, type exit.

↑ Top