Which of the following commands lets you move to another directory choose only one answer?
If you are new to Unix, read "Unix Survival Guide for Mac & Ubuntu - Terminal, File System and Users". Show
Bash Shell BasicsA shell (or command interpreter, or command prompt) is a program that lets you interact with the Operating System (OS) by issuing text-based commands. It is called a shell, as it protects or shields you from the complicated tasks of interacting with the OS directly. A shell provides a set of commands. It also has its own scripting language for you to write shell scripts (or shell programs), which could be a sequence of commands for automating system administration tasks. There are many shell programs: from the legacy and obsoleted sh (the original Bourne Shell), csh (C Shell), ksh (Korn Shell), zsh (Z Shell), to newer bash (Bourne Again Shell), tcsh (Tenex C Shell) and tsh (T Shell); and Windows' cmd (command prompt). In this article, we shall cover only GNU bash shell, which is based on the Unix's original bourne shell (sh), and provides features of csh and tsh. The mother site for bash shell is www.gnu.org/software/bash/. Unixes provide an application called "Terminal", which runs a shell program. By default, Mac OS X and Linux's Terminal runs the bash shell. Besides the Graphical "Terminal", you can also use Ctrl+Alt+F1 to Ctrl+Alt+F6 to access one of the six virtual console (tty1 to tty6); and use Ctrl+Alt+F7 to switch back to the GUI screen (tty7). In addition, you can also login remotely via the secured ssh (Secure Shell) or the unsecured telnet. Bash shell supports thousands of commands - the philosophy of Unix is using small programs that perform one task, but performed it well. Bash shell is often identified by a '$' sign in the command prompt. Help and Man PagesYou can issue help command: $ help $ helpMost of the commands also support an help option, but may exist in various style: $Unix systems provide so called man pages (or manual pages) for all the commands and utilities. To display the man page for a particular command, use man command: $ manTo search for commands: $ man -kMan pages are comprehensive and can be hard to read on the text-based console. Online HTML-formatted man pages are available, for example, the Ubuntu's man pages @ http://manpages.ubuntu.com, Linux documentation @ http://linux.die.net. You can also google "man command-name" to search for online man pages. The man pages are divided into 10 sections:
A man page identified its section number in bracket, e.g., chmod(1). Some commands have man pages in multiple sections. When a man command is issued, the earliest section will be shown by default. You can also explicitly request for another section. For example, $ man -k chmod chmod(1) ...... chmod(2) ...... $ man chmod chmod(1) ..... .............. $ man 2 chmod chmod(2) ..... ..............The man pages are
kept in /usr/share/man/man Useful Commands/UtilitiesThese are the commands/utilities that a good Unix programmer is expected to know. Check the man pages ("man command-name" or google) to get the detailed description.
More on cd (change directory) commandRead "Change Directory (cd) command" for basic usage. You can use "cd path" to change the current working directory. The new path could be an absolute path, beginning with root "\" or home "~"; or relative to the current working directory (PWD). In cd command, you can use "/" to denote the root directory, "~" to denote home directory of the current login user; ".." (double-dot) to refer to the parent directory; "." (single-dot) to refer to the current directory; and "-" (dash) to refer to the previous working directory (OLDPWD). By default, in "cd relative-path", the new path is relative to the current working directory. Nonetheless, you can set the environment variable CDPATH to change the base. If CDPATH is not set, it is defaulted to current working directory. CDPATH could contain multiple directories separated by ":" (colon). For example, $ cd $ pwd /home/peter $ mkdir local $ export CDPATH=/usr $ cd local /usr/local $ export CDPATH=.:/usr $ cd $ pwd /home/peter $ cd local /home/peter/localPipe and Input/Output RedirectionBy default, the output of a command goes to the screen (called STDOUT), and the input of a command comes from the keyboard (called STDIN). You can use a redirection operator to redirect input and output from/to a file or another command:
An output redirector '>' involves a program and a sink (destination). An input redirector '<' involves a program and a source. A pipe '|' involves two programs. For examples, $ ls -l > listing.txt $ ls -l | less $ ls -l | wc -l $ ls -l | grep xxxx $ ls -l | tee listing.txt $ ls -l | grep xxxx | tee listing1.txt listing2.txtfind, locate and grepfind and grep are extremely powerful commands. findYou can use find to find files. For example, $ find -name "*.c" $ find -iname "*.c" $ find /usr -name "*.c"locateThe find command recursively search thru the directories and is slow. locate, on the other hand, search an index (which is build every night for the entire file system) and is fast. For example, $ locate *.cgrep - Print lines matching the patternYou can use grep to find lines matching a search pattern. The syntax is: $ grep [options] pattern [file...] $ grep search-word filename $ grep search-word file1 file2 file3 $ grep -r search-word directorynameIf input file is not present, grep takes its input from the standard input (STDIN). For examples, $ grep "root" /etc/passwd $ grep "root" /etc/* | less $ grep -r "root" /etc | lessVery often, the input of grep is piped from the output of another command as follows: $ command | grep [options] patternFor examples: $ ls -R | grep "txt" $ ps aux | grep -i www-dataYou can use option -i for case insensitive search, -A n to print n lines after the match, -B n to print n before the match. For example, grep -i -A3 -B3 "www-data" /etc/passwdYou can use option -v to invert the search; -n to print the line number; -c to count the matched lines. grep with Regular ExpressionYou can use regular expression (regex) as the search pattern. For example, $ grep '[0-9]\{5\}' /etc/passwdTake note that you need to use backslash for many of the regex metacharacter such as { and ]. See "Regular Expression" section. nano editorGNU nano (successor of GNU pico) is a text-based non-graphic editor, included in most of the Unix system. The mother site of nano is https://www.nano-editor.org/. It is handy for simple editing (e.g., changing a configuration option or adding a line), especially in a non-graphical environment such as Ubuntu Server. However, it is not meant for heavy-duty editing. nano shows the command options at the bottom of the screen, where ^- denote Ctrl key and M- denotes the Meta Key (Alt in Windows, Cmd in Mac) Open a file$ cd /path/to/directory $ nano filename $ sudo nano filenameSaving/Exiting
Copy, Cut and Paste
Search and Replace (Regex Supported)
Customize Bash ShellSet PATHYou can use one the following commands to check the current PATH settings: $ echo $PATH $ printenv PATHTo modify PATH setting permanently, e.g., to include the current directory into the PATH, edit ~/.profile to include the following statement. export PATH=.:$PATHNotes:
Change PromptThe prompt is kept in environment variable PS1. You can check the current setting using one the following commands: $ echo $PS1 $ printenv PS1The default setting is "username@hostname:directory$", which is set via "\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$". To change the prompt setting, edit "~\.bashrc" to include the following line, where \w denotes the current directory with home denoted as ~. PS1='\w\$ 'Set Starting DirectoryEdit "~\.bashrc" to include a cd (change directory) command at the end of the file. Managing UsersUsers and GroupsUnix is a multi-user operating system. Each user is identified via an username. Each user is assigned to one primary group and zero or more secondary groups (or supplementary groups). User and group are used for access control of resources, such as files and directories. There are three types of users: superuser (or privileged user, or root user, or administrator), regular users (normal day-to-day users or non-administrative users), and system users (or pseudo-users).
User's Home DirectoryEach user is allocated a home directory.
/etc/passwdUser information is stored in /etc/passwd. Each user has an entry in the file. For example, below is the partial contents of the Ubuntu's /etc/passwd after installation with a new user called peter: $ less /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh ...... www-data:x:33:33:www-data:/var/www:/bin/sh mysql:x:116:125:MySQL Server,,,:/nonexistent:/bin/false nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin ...... peter:x:1000:1000:My User:/home/peter:/bin/bashThe format of /etc/passwd is as follows, where items are separated by a colon: username:password:UID:GID:GECOS:home-directory:default-login-shell
/etc/groupGroup information is stored in /etc/group. For example, $ less /etc/group root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4:peter ...... sudo:x:27:peter ...... nogroup:x:65534: ...... peter:x:1000:The format of /etc/group is as follows: GroupName:Password:GID:UserList
getent CommandBeside listing the file, you can use a special command getent to get entries from databases such as /etc/passwd and /etc/group. For examples, getentList Users and GroupsTo list user information, use command whoami and id: $ whoami peter $ id uid=1000(peter) gid=1000(peter) groups=1000(peter),4(adm),27(sudo),... $ idTo list the groups of a particular user, use command groups, for example, $ groups peter adm sudo .... $ groupsThe current user information is kept in the environment, e.g., $ printenv SHELL=/bin/bash USER=peter HOME=/home/peter ......User and Primary GroupWhen a regular user is created (e.g., peter), it is given a UID (e.g., 1000). An identical groupname (peter) and GID (1000) is also created, and assigned as the primary group of the user. If this user creates a file or directory, it is owned by this user and its primary group. For example, $ id uid=1000(peter) gid=1000(peter) groups=1000(peter),.... $ touch temp.txt $ ll temp.txt -rw-rw-r-- 1 peter peter 0 Feb 8 23:49 temp.txt/etc/shadowThe /etc/shadow contain the encrypted password, aging and account lockout information. For example, $ sudo less /etc/shadow root:!:15630:0:99999:7::: nobody:*:17001:0:99999:7::: www-data:*:17001:0:99999:7::: peter:xxxxxxxx:15788:0:99999:7::: ......The format is: username:password:last-changed:min-days:max-days:warn-days:inactive-days:expire-days:reserved
Superuser 'root' and Superuser-do 'sudo'Superuser rootIn Unix, the default superuser is called 'root', which can do everything and anything, including destructive tasks. Doing regular tasks (such as program development) with root can be dangerous, as root could remove important system files and destroy your system. Hence, it is strongly recommended to do regular work with a regular user instead of superuser. However, at times, you need the superuser privilege to perform a task, such as install a new software and writing to a system directory. You can do it in two ways: sudo (superuser-do) or su (switch user). sudo An authorized user can run a command with superuser privilege by prefixing it with 'sudo' (superuser-do). The list of users authorized to do 'sudo' is kept in /etc/sudoers (called sudo list). When
you issue "sudo There are 2 ways to add a user to the sudo list:
When a command is run under 'sudo', it is run as 'root' user and 'root' group (NOT as the sudoer user and group). For example, all the files created are owned by root:root. We can verify as follows: $ id uid=1000(peter) gid=1000(peter) groups=1000(peter),4(adm),27(sudo),... $ sudo id uid=0(root) gid=0(root) groups=0(root) $ sudo printenv PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOME=/home/peter USER=root SUDO_COMMAND=/usr/bin/printenv SUDO_USER=peter SUDO_UID=1000 SUDO_GID=1000 ...... $ printenv USER=peter HOME=/home/peter ...... $ sudo -H printenv ...... HOME=/root ...... $ sudo -u nobody id uid=65534(nobody) gid=65534(nogroup) groups=65534(nogroup) $ sudo touch temp.txt $ ll temp.txt -rw-r--r-- 1 root root 0 Feb 8 22:48 temp.txt/etc/sudoersThe following shows the contents of /etc/sudoers (on Ubuntu): $ sudo less /etc/sudoers # # This file MUST be edited with the 'visudo' command as root. # Defaults env_reset Defaults mail_badpass Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" # User privilege specification root ALL=(ALL:ALL) ALL # Members of the admin group may gain root privileges %admin ALL=(ALL) ALL # Allow members of group sudo to execute any command %sudo ALL=(ALL:ALL) ALL # See sudoers(5) for more information on "#include" directives: #includedir /etc/sudoers.dThe format of /etc/sudoers is as follows: user|%group hostlist = (userlist:group) [NOPASSWD:] commandlist
For Example, nobody ALL=(root) NOPASSWD: /usr/sbin/rndc reload"sudo su" or "sudo -i"Some systems (e.g. Ubuntu) support so-called interactive sudo, or "sudo -i", or "sudo su", which allows you to issue multiple commands under a sudo session. The command prompt will be changed to the root prompt #. You can exit the sudo session via command "exit". Avoid using this command, as you are likely to forget to switch out from sudo. "sudo cd ..." won't work!If you issue "sudo cd gksudo |