Unix Tutorial
Total Page:16
File Type:pdf, Size:1020Kb
Unix Tutorial The following is a quick overview of essential UNIX commands. Example Command Description Usage ls ls List directory contents ls –l Long listing ls –ld directory List permissions on directory pwd pwd Display current directory cp cp source target Copy file source to target cp –r source Copy directory, recursive copy target mv mv source target Move file or directory from source to target (the source is removed after the move) rm rm filename Remove a file rm –f filename Remove a file in which you have directory write privileges but not file write privileges rm –rf directory Remove non-empty directory mkdir mkdir directory Create a directory rmdir rmdir directory Remove empty directory ps ps Display process statuses ps –ef Display all processes ps –u userid Display all process for userid kill kill PID Terminate process with process ID (PID). Sends UNIX process the TERM signal. This signal should terminate most UNIX processes. kill -9 PID Terminate process with PID. Sends UNIX process the KILL signal. This should only be used when normal attempts to terminate the process are ineffective. grep grep pattern Display all occurrences of pattern in filename filename Display all filename(s) containing pattern grep –l pattern filename(s) cat cat filename Display contents of filename cat file file2 Display contents of file1 and file2 cat file1 Create file3 from contents of file1 and file2 file2>file3 more more filename Display file contents, one page at a time man man command Display UNIX man(ual) page for given command tar tar tvf tarfile.tar Display contents of tarfile tar xvf tarfile.tar Extract contents of tarfile tar cvf tarfile.tar* Create tarfile named tarfile.tar with all files and directories in the current directory chmod chmod 755 Change file or directory permissions directory gunzip gunzip file.gz Uncompress .gz file (generally used with tarfiles). If it is not in your path, check /vol.nsm/tools/bin. find find / -name file Locate file starting in root (/) directory exit exit Logout of your UNIX session id id Display user name, user ID, and primary group ID groups groups Display all UNIX groups to which you belong -- or -- -- or -- id -a id –a hostname hostname Display the hostname of the UNIX machine -- or -- -- or -- uname -n uname -n passwd passwd Change your UNIX password env env Display your environment variables nohup nohup command Execute command in the background, and do not terminate the process & if you logout of your UNIX session. command & command & Execute the command in the background. Output will be sent to a file named nohup.out rather than your display. jobs jobs List background processes Ctrl-Z and Suspend current foreground process, put process in background for bg execution gzip gzip filename Compress file (generally used with tarfiles) File and Directory Permissions Demystified When doing a long listing ( ls –l) of a directory, we see the following entry: drwxr-xr-x 2 hutchib nsm 512 Apr 20 11:09 bin This is a directory named bin that is owned by user hutchib and group nsm . Let's explore the first 10 characters in detail. • The first character is a d, which indicates that we are looking at a directory. Other possibilities for the first character include an - for an ordinary file and an l for a symbolic link (like a shortcut in Windows). • Characters 2 through 4 correspond to user permissions, or permissions corresponding to the owner of the directory. They are rwx , which indicates that the directory is readable, writable, and executable by the user (the owner of the directory, hutchib ). • Characters 5 through 7 correspond to group permissions. In this example, these permissions apply to the group nsm . They are r-x, indicating that the directory is readable and executable by any user belonging to group nsm . • Characters 8 through 10 correspond to other permissions. If you are not the owner of the directory, nor a member of the group, the other permissions are applicable. They are r-x, meaning that the directory is readable and executable by everyone else. What do read, write, and execute permissions mean on a directory? Read = You are able to list contents of the directory and display contents of files. Write = You are able to create, modify, or delete files in the directory. Execute = You are able to change into the directory. (Even if you had read and write privileges on a directory, if you do not have execute privileges on the directory, you would not be able to get into ( cd ) the directory.) Note : If you have write privileges to a directory, you may delete any file in that directory, even if it is not writable, and is owned by somebody else. What do read, write, and execute permissions mean on a file? Read = You are able to list the contents of a file. Write = You are able to modify or delete the file. (If you have permission to write to a file, you may modify it, but may not delete it, if the directory is not writable.) Execute = You are able to execute the file. (This is needed if the file is a shell script or Executable.) How do I change file/directory permissions? You may change file and directory permissions with the chmod command. The syntax is: chmod octal-mode file_or_directory You may recursively change permissions with the chmod –R command. When looking at the following table, remember these values when setting file or directory permissions: Read = 4 Write = 2 Execute = 1 Permissions Octal-mode Command -rwxrwxrwx 777 chmod 777 file_or_directory User: rwx = 4 + 2 + 1 = 7 Group: rwx = 4 + 2 + 1 = 7 Other: rwx = 4 + 2 + 1 = 7 -rwxr-xr-x 755 chmod 755 file_or_directory User: rwx = 4 + 2 + 1 = 7 Group: r-x = 4 + 1 = 5 Other: r-x = 4 + 1 = 5 -rw-r--r-- 644 chmod 644 file_or_directory User: rw- = 4 + 2 = 6 Group: r-- = 4 Other: r-- = 4 .