Linux Primer

Basic Commands in the Linux Shell

If you have no experience with the Linux command line interface, it will be best to work through this primer.  Here you can find some useful commands for various day to day tasks.

When you receive your account information you will be asked to change your password.  If the user does not change the password quickly, the account will be locked.  If this happens, please write to our helpdesk (help(at)hpcf.upr.edu) and ask for a password reset. To change your password, simply execute the command
$ passwd
For example:
$ passwd
Changing password for user [user].
Enter login(LDAP) password: 
New password: 
Retype new password: 
LDAP password information changed for [user]
passwd: all authentication tokens updated successfully.
 
Navigating your Unix filesystem is one of the most basic skills you will need. The following commands will help you with all your navigation needs.
  • pwd - (Print Working Directory) tells you what directory you are in.
Example:
$ pwd
/home/guest
  • cd - Used to change from one working directory to another. When used without arguments, cd moves you to your home directory.
Example:
$ pwd
/work/guest
$ cd
$ pwd
/home/guest
Passing a directory name as argument to cd moves you to said directory.
$ pwd
/home/guest
$ cd /work/guest
$ pwd
/work/guest
Passing the argument .. to cd takes you up to the parent directory of your current directory. Example:
$ pwd
/home/guest
$ cd ..
$ pwd
/home
Passing the argument - to cd takes you to the previous directory you were in. Example:
$ pwd
/home/guest
$ cd /work/guest
$ pwd
/work/guest
$ cd -
/home/guest
 
This section describes some of the most used directory and file handling commands.

Directory Listings

ls - Use this command to obtain a list of all files in a specified directory.
$ ls /home/guest
alan.txt data
catanp.txt plants
bin
To obtain more information about individual files, use:
  • ls -l - lists files in the long format providing the following info: access modes, link count, owner, group, size, date and time of last modification and finally the filename for each file. Check the following example for the output of this command.
$ ls -l /home/guest
-rw-r--r-- 1 guest staff 2490 Jul 8 11:42 alan.txt
-rw-r--r-- 1 guest staff 3623 Jul 8 11:42 catanp.txt
drwxr-x--x 2 guest staff 512 Jul 8 11:28 bin
drwxr-xr-x 2 guest staff 512 Jul 8 11:53 data
drwxr-xr-x 2 guest staff 512 Jul 8 11:33 plants
  • ls -a - list all files including hidden files.
The files whose names begin with a period (.) are special or 'hidden files' that are only shown with the use of the a option.  The two top files are special files referring respectively to the current directory (.) and the parent directory (..).  If you combine both options (a and l) together on one line you will get the following output.
$ ls -al /home/guest
drwxr-xr-x 5 guest staff 512 Jul 8 11:57 .
drwxr-xr-x 5 guest staff 512 Jun 26 09:24 ..
drwxr-xr-x 5 guest staff 512 Jul 5 10:57 .cshrc
drwxr-xr-x 5 guest staff 512 Jul 5 09:36 .login
drwxr-xr-x 5 guest staff 512 Jul 5 09:47 .profile
-rw-r--r-- 1 guest staff 2490 Jul 8 11:42 alan.txt

Copying, Renaming, and Deleting Files

cp - command used to copy one or more files to another file, a list of files, or a directory.
$ cp file1 file2
This will make a copy of the file1 data, calling the copy file2. If file2 already exists its contents will be overwritten with the contents of data, thus destroying what was originally in file2. The option -i may be used to cause the machine to prompt for confirmation before overwriting the output file.
$ cp -i file1 file2
Overwrite file2?
mv - Use this command to change the name of a file or to move a file to a different location.
$ mv file1 file2
This renames file1 to be called file2. As with cp, if file2 already exists, its contents will be overwritten and lost. Again, there is an option -i to cause the machine to confirm that the second file should be overwritten if it already exists. If you wants to move a file to a different location, use:
$ mv /home/foo/file1 /home/foo/tmp/file1
This will move file1 from source /home/foo to destination /home/foo/tmp. rm - Use this command to delete a file.
$ rm file1
By default the files will simply be deleted with no confirmation; the option -i may be used to cause the machine to ask for each file whether it should be deleted or not.  Please be very careful when using the rm command.
Every user on a Unix system has a unique username, and is a member of at least one group (the primary group for that user). A user can also be a member of one or more other groups. Only the administrator can create new groups or add/delete group members.  Every directory and file on the system has an owner, and also an associated group. It also has a set of permission flags which specify separate read, write and execute permissions for the 'user' (owner), 'group', and 'other' (everyone else with an account on the computer). The following is an example of the output produced by 'ls -l'.
$ ls -l
drwx------ 2 richard staff 2048 Jan 2 1997 private
drwxrws--- 2 richard staff 2048 Jan 2 1997 admin
-rw-rw---- 2 richard staff 12040 Aug 20 1996 admin/userinfo
drwxr-xr-x 3 richard user 2048 May 13 09:27 public
Understanding how to read this output is useful to all linux users, so here is a brief explanation. Field 1: a set of ten permissions flags (explained in the following table) Field 2: link count Field 3: owner of the file Field 4: associated group of the file Field 5: size in bytes Field 6-8: date of last modification Field 7: name of file The permission flags are read as follows (left to right):
POSITION MEANING
1 directory flag, 'd' if a directory, '-' if a normal file, something else occasionally may appear here for special devices.
2,3,4 read, write, execute permission for User (Owner)) of file
5,6,7 read, write, execute permission for Group
8,9,10 read, write, execute permission for Other
VALUE MEANING
- in any position means that flag is not set
r file is readable by owner, group or other
w file is writable. On a directory, write access means you can add or delete files
x file is executable (only for programs and shell scripts not useful for data files). Execute permission on a directory means you can list the files in that directory.
s in the place where 'x' would normally go is called the set-UID or set-groupID flag.
Another common method for representing Unix permissions is octal notation. Octal notation consists of a three or four-digit base-8 value. With three-digit octal notation, each numeral represents a different component of the permission set: user class, group class, and "others" class respectively. Each of these digits is the sum of its component bits. As a result, specific bits add to the sum as it is represented by a numeral:
  • The read bit adds 4 to its total (in binary 100)
  • The write bit adds 2 to its total (in binary 010)
  • The execute bit adds 1 to its total (in binary 001)
The commands to change file attributes (permissions, owner, group, etc.) are these: chmod - This command changes the access mode a file.
$ chmod 777 myfile
The above command gives read, write, and execute permissions to user, group, and others. The following is the output of 'ls -l' which shows the new properties of myfile.
$ ls -l myfile
-rwxrwxrwx 2 richard staff 12040 June 20 2009 myfile

Symbolic Notation Used with chmod

Symbolic Mode Description
Select a Class of User
u Selects user or owner of a file
g Selects group to which the owner belongs
o Selects all other users
a Selects all users,which is the deafult
Select an Operation
+ Adds permissions for the specified calss of user.
- Removes permissions from the specified class of user.
= Sets the permission for the specified user, and resets all other unspecified permissions for thst user class.
All three examples below assign read-only permissions to everyone for the file myfile.txt.
$ chmod 444 myfile.txt
$ chmod =r myfile.txt
$ chmod a-wx,a+r myfile.txt
chgrp - This command changes the group associated with a file or directory. You must be a supervisor or the owner of the file before you can change the group association of a file. To change the group that the file myfile.txt is associated with to that of staff, use:
$ chgrp staff myfile.txt
If you want to change the group that a directory and all its content are associated with to that of for example staff, use 'chmod -R' as follows:
$ chgrp -R staff mydir
'-R' (recursive) option changes the group ID for files subdirectories, and symbolic links. chown - This command changes the ownership of a file. Sometimes, it may be necessary to change the owership of a given file. You can change the owner of a file to affect how its permissions are applied. To change the ownership of the file myfile.txt to that of foo, use:
$ chown foo myfile.txt