Commonly Used Basic UNIX Commands
Total Page:16
File Type:pdf, Size:1020Kb
Unix - Basic Commands
Commonly used basic UNIX commands
Change Unix Password passwd --- This changes your password. You should change your password periodically to keep it from being figured out. Never give your password to anyone, and don't write it down.
List Content of a Directory ls --- ls lists the contents of a directory. The output of "ls" looks like: apple.txt pgm1* web welcome.msg ls can be used with other options. The options will change the output. For instance, ls -l --- option "-l" lists the directory using the long or detail format, indicating the creation date and time, the file size, the file owner, and file permissions. Example: ls -l
-rw-r--r-- 1 sultans 23397 Feb 12 22:51 apple.txt -rw-rw-rw- 1 sultans 961 Feb 12 23:17 orange.file -r-xr-xr-x 1 sultans 2064961 Feb 12 23:17 pgm1 drwxr-xr-x 1 sultans 4096 Feb 12 23:17 sultans-bin drwxr-xr-x 1 sultans 4096 Feb 12 23:17 web -rw-r--r-- 1 sultans 235 Feb 12 20:17 welcome.msg ls -a --- option "-a" lists all files in a directory including hidden files, (hidden files in Unix are files that start with "." such as .login). You can combine various options together. For example to list all files including hidden files, in a detail format, you can issue the "ls" command with both options, as follows: ls -la
Change Directory cd dirname --- Change directory down to "dirname". cd .. --- Change directory up one level from where you are. cd --- "cd" alone will take you back to your home directory. The home directory is where you first arrive when you login.
Where Am I? pwd --- causes the system to Print the current Working Directory so that you can find out what directory you're in. pwd
/home/s/sultans/web You may not need this command if your Unix prompt has been set up to constantly display your current directory. In this case, there is no need to ever issue the "pwd" command.
Make a New Directory mkdir dirname --- Make a directory - This command creates a new directory below the current directory. mkdir images --- creates a new directory called "images" below the current directory.
Remove a Directory rmdir dirname --- Remove a directory - This command deletes an existing directory. rmdir web --- delete the directory called "web".
Create a New File touch filename --- Create a new empty file. touch index.html --- creates a new empty file called "index.html".
Remove a File rm filename --- Remove a file or a group of files. rm index.html --- delete the file called "index.html". The "rm" command also has an '-i' option. You should use the -i option with rm since in UNIX, if you remove a file, it is gone forever. rm -i index.html --- prompt me before deleting the file called "index.html".
Copy a File cp file1 file2 --- Copy from file1 to file2. cp index.html index2.html --- make a copy of "index.html" and call it "index2.html"
Move a File mv file directory --- Moves a file from one directory to another. Example: mv index.html web --- move "index.html" from current directory to the "web" directory. Rename a File mv file1 file2 --- Rename file1 to file2 another. The "mv" command is also used to rename a file. If the receiving directory is only a file name rather than a directory, the "mv" command renames the file instead. Example: mv index.html index2.html --- rename "index.html" to "index2.html"
Edit a File pico --- Pico is a plain text editor installed on our system (similar to notepad on your PC). You can start pico with or without a filename. If you start pico without the filename, it will be created for you. If you start pico with a filename, that file will be opened up for editing. To start pico, enter: pico filename Once pico is started, you can see all the editor options at the bottom of the screen. vi --- vi is another plain text editor - not nearly as easy to use as pico pine --- Pine is a menu driven mail program that allows users to send and receive electronic mail over the Internet.
File System Overview Note on learning UNIX: There are a small number of concepts that are key to using UNIX. It is possible that you will not understand them in the course of the lecture. Instead you will need to do some hands on work with UNIX and through the combination of both lecture and lab, you will understand. We will be exploring the commandline interface into UNIX. There is also a popular windowing interface called X windows, but the commandline interface can be easily used from remote sites to administer and operate the UNIX site and services. File System Hierarchy UNIX uses the notion of a hierarchical file system. That is, there is a system of files and directories structured in an inverted tree: A portion of a typical UNIX filesystem tree.
/ (root) | | +------+------+------+------+------.. | | | | | bin etc usr lib home | | | | +------+------+------+------+ +------+------+------.. | | | | | | | | bin etc lib local spool a b c | | | | | | http perl +------+------+----.. bairde barns bushj The designers of UNIX attempted to make as much of UNIX as possible accessible through the file system. As a result, most devices can be accessed as files. For example, you can open and write to a file called /dev/console just as though it were a text file. Of course, all regular files are kept in this hierarchy and all executable programs are stored as files as well. So understanding and using this file system is really the key to UNIX. You may already have an idea of how hierarchical file systems work from your previous experience with Windows or Macintosh folders. In UNIX, these folders are called directories. The root directory is at the top of the file system tree. The slash / character is used to identify the root directory. All other directories and files lie beneath the root. cd / (this will take you to the root directory) The current directory is an important concept. You always have a current directory when you are operating in a UNIX file system. The current directory is whichever directory you are currently in. As you move around the UNIX file system, your current directory always changes to the directory that you are currently in. The concept of a current directory allows you to refer to objects in the directory without having to give the full pathname to those objects. The home directory is the directory that is set up for you as your "home". The system automatically assigns you a directory when you login. This initial position in the file system for you is known as your home directory. A typical home directory for a user named "barns" might be: /home/b/barns The Path A path to a directory or a file is identified by naming all directories that must be passed through to reach from one end to the other. For example, the mailbox for a user named "barns" might be the file identified as: /home/b/barns/mbox If you are in the "barns" directory above, the mailbox file can simply be accessed as "mbox". However, if you are not in the "barns" directory, then a path will be needed to access the "mbox" file.
A relative path to a directory, means that you are starting from your current directory. So to navigate up it's simply .. To navigate down, you simply refer to that directory by name. For example, If I have the following structure: /home/s/sultans | +------+------+ | | web web2
And I am inside the "web" directory, I can navigate go to the parent directory by issuing: cd .. If I am in the "sultans" directory, I can navigate to the child directory "web" by issuing: cd web If I am in the "web" directory, I can navigate to the "web2" directory as follows: cd ../web2 Note: relative paths never start with a slash /. An absolute path to a directory, means that you are always starting from the root directory of the Unix computer. Therefore, to navigate or refer to an object, you must always start from the top of the tree. To change from the "web" directory to the "web2" directory in the above diagram, you must issue: cd /home/s/sultans/web2 To create to a document (say "new.html") inside the "web" directory, you issue: pico /home/s/sultans/web/new.html Note: An absolute path must always start with a slash /.
There are a couple of abbreviations that are commonly used to navigate the file system. These are . (dot) .. (dot-dot) ~ (tilte) The single dot (.) refers to the current directory, dot-dot (..) refers to the parent directory of the current directory, and tilde (~) refers to your home directory. Here is a subsection of a filesystem that looks like a typical user's home directory system: / | home | s | sultans | +------+------+------+------+------+------.. | | | | | | mail bin web file.txt letter perl | | | | +------+------+------+ +----+----+ | | | | | | index.html doc.html my.gif doc2.html show.pl pgm2.pl
If I wish to go to my home directory, no matter where I am, I can issue the following command: cd ~ (cd by itself will also work) If my current directory is "/home/s/sultans/web", and I wish to make my current directory "/home/s/sultans/perl", I can use the tilde (~) abreviation and issue the command: cd ~/perl If I wish to traverse to the "perl" directory of a user named "barns", I can issue the following command: cd ~barns/perl I can use the dot-dot abreviation to change the current directory to the parent directory. For example, if my current directory is "/home/s/sultans/web", and I want to change this to "/home/s/sultans". I can issue the command: cd ..