Unix Commands Unix Is a Command-Line Environment Similar to Microsoft's DOS Command Shell
Total Page:16
File Type:pdf, Size:1020Kb
Unix Commands Unix is a command-line environment similar to Microsoft's DOS command shell. We'll be using the bash shell as our environment. Unix has many features that make it an excellent development environment. Most of the commands are stored in directory /usr/bin and /bin. Note that directory separators in Unix are forward slashes. To list the contents of these directories, enter the following at the prompt ($): $ ls /usr/bin $ ls /usr Press the Tab key while entering a command and bash will complete the command using available file names. The ls command can be used to list files and directories. If you want to know more about the ls command enter the following: $ man ls This brings up a man page, or manual page, for the ls command. Most commands have man pages that describe command-line options. Use vi commands to navigate within man pages. Information can also be found with the info command: $ info ls Use the following commands to navigate within info: n next node p previous node u up menu tree l last node space scroll down DEL scroll up b beginning of page ? help q quit When you encounter menu links, position your cursor on the link and press ENTER. Typically more detailed and up-to-date information is available from info. Regular Expressions A regular expression can represent a pattern of zero or more characters. Consider the following regular expressions used with the ls command: $ ls *.c # list all files that end with '.c' $ ls [aA]* # list all files that start with 'a' or 'A' $ ls prog??.c # list all files prog01.c prog02.c progXX.c On the command line you can use the following glob characters for regular expressions: * zero or more characters ? one character [] character class Within applications, such as vi and grep, other more powerful expressions are allowed. Command Summary passwd Change password. Syntax passwd pwd Print name of current/working directory. Syntax pwd cd Change directory. Syntax cd [directory] Examples cd # go to $HOME cd ~ # go to $HOME cd ~/mydir # go to $HOME/mydir cd . # go to current directory (noop) cd ./dir1 # go to dir1 cd dir1 # go to dir1 cd /usr/bin # go to /usr/bin cd .. # go up 1 level (parent directory) cd ../.. # go up 2 levels mkdir Make a new directory. Syntax mkdir directory... Examples mkdir abc # make new directory abc mkdir abc def # make directories abc and def ls List files or directories. Syntax ls [option]... [file]... Options -R list subdirectories recursively -l use a long listing format -s sort by file size -t sort by modification time -r reverse sort order -a do not hide entries starting with "." -d list directory entries instead of contents -F append / after directories, * after executables Examples ls # list all files ls * # list all files ls *.c # list all files ending with ".c" ls –l main.c # list main.c in long format ls –l -t main.c func.c # list files in long format by time ls –lt main.c func.c # list files in long format by time ls dir1 # list contents of directory dir1 ls –ld dir1 # list attributes of dir1 rm Remove files or directories. Syntax rm [option]... file... Options -R remove the contents of directories recursively -r equivalent to –R -f ignore non-existant files, never prompt -i interactive, prompt before removing Examples rm main.c func.c # delete files main.c and func.c rm *.o # delete all object files rm –i *.c # remove all c files with prompt rm –r dir1 # remove directory dir1 cp Copy files or directories. Syntax cp [option]... file... Options -R copy the contents of directories recursively -r similar to –R, does not include special files -p preserve file attributes -i interactive, prompt before overwrite Examples cp a.c b.c # copy a.c to b.c cp a.c b.c dir1 # copy files a.c, b.c to directory cp –pr dir1 dir2 # recursively copy dir1 to dir2 # if dir2 exists cp to dir2/dir1 # else create dir2 mv Rename files or directories. Syntax mv [option]... file... Options -i interactive, prompt before overwrite Examples mv a.c b.c # rename a.c to b.c mv a.c dir1 # move a.c to dir1/a.c mv a.c dir1/b.c # move a.c to dir1/b.c chmod Change file access permissions. Syntax chmod [option]... mode file... Options r read permission w write permission x execute permission Examples chmod +x main # give file execute permission chmod –x main # remove execute permission chmod –w foo.c # make foo.c read-only Notes File permissions may be granted for three categories of users: user, group, and everyone. Within each category there are three access permissions: read, write, and execute. This makes a total of 9 permission settings. When you list files in long format, the output includes the following: drwxrwxrwx ... # directory with all permissions -rw-r--r-- ... # file writeable by user only -rw-rw-r-- ... # file writeable by user & group -rw-rw-rw- ... # file writeable by everyone The d will display if the file is a directory. This is followed by rwx entries for the user, group, and everyone. Note that execute permission, for a directory, must be enabled to view or go to that directory. echo Display string on stdout. Syntax echo [option]... string Options -n do not output trailing newline Examples echo hello world # display "hello world" on console echo hello world > foo # output "hello world" to file foo cat Concatonate files and print on stdout. Syntax cat [option]... [file]... Options -t show nonprinting characters Examples cat main.c # display main.c on stdout cat –t Makefile # show nonprinting characters cat *.c > foo # concatonate all c files to foo grep Print lines matching a pattern. Syntax grep [option]... pattern [file]... Options -i ignore case -l just display name of file containing match Examples grep main *.c # search for 'main' in all c files grep –i bagel *.c # case insensitive search bagel grep '[bB]agel' *.c # search for bagel or Bagel grep '^int' * # all lines beginning with 'int' grep –l '^int' * # just display file names grep ';$' *.c # all lines that end with ';' diff Compare files line by line. Syntax diff [option]... file1 file2 Options -i ignore case Examples diff a.c b.c # compare files a.c and b.c diff –i a.c b.c > foo # case insensitive, output to foo which Search PATH for executable and print location. Syntax which [option]... file Options -a display all occurrences Examples which cat # displays /usr/bin/cat which –a cat # displays /usr/bin/cat # /bin/cat tar Tape archiver. Backup/restore files and directories. Syntax tar [option]... [file]... Options -c create archive -x extract from archive -t table of contents -v verbose -z compress -p preserve permissions on extraction -f file specifies location of tar file Examples tar –cvf x.tar *.c # archive all .c files tar –tf x.tar # table of contents tar –tvf x.tar # verbose table of contents tar –xvf x.tar foo.c # extract file foo.c tar –xvf x.tar # extract all files tar –cvzf x.tar.gz . # archive & compress all files tar –xvzpf x.tar.gz # extract all files w/permissions tar –tzf x.tar.gz # table of contents wc Print the number of bytes, words, or lines in a file. Syntax wc [option]... [file]... Options -c characters -w words -l lines Examples wc main.c # count lines, words, chars wc –l main.c # count lines wc –l *.c # count lines, all c files kill Stop a process. Examples ps # find process ID (pid) kill –9 pid # kill process touch Update date/time stamp to current date/time. Examples touch main.c # update main.c touch *.c # update all .c files Pipes You can use pipes to channel the output of one program to the input of another program. For example: $ ls | wc –l will tell you how many files are in your directory. If we omit the file list for wc, then it counts the number of lines on stdin. The pipe channels stdout from the ls command to stdin for wc. Redirection You can redirect the output of any command to a file. Consider the following. $ ls # list current directory to monitor $ ls > foo # redirect listing to file foo There are two output streams, stdout and stderr. Stream stdout is for normal console output, and stderr is intended for console error output. The standard console input stream is stdin. > redirect stdout to file &> redirect stdout and stderr to file >> append stdout to file < fetch stdin from file If you have a lot of compilation errors, you may want to redirect the error output from the compiler to a file. $ gcc –c x.c &> err # redirect stdout and stderr to file 'err' What if you wanted the output of a command to be used on the command line of another command. For example... $ which cat # where is ls? /usr/bin/cat $ ls –l /usr/bin/cat # display attributes We can't just type $ ls –l which cat It will try to list attributes of files named which and cat in the current directory! For this task you can use back quotes: $ ls –l `which cat` Code within back quotes is executed, and the output is inserted at that point. Environment You can configure the environment to your liking. When you first login, a bash command file in your home directory, called .profile, automatically executes. Here is a sample profile. #!/bin/bash # change commands alias more="less" alias ls="ls -F" alias e="exit" alias path="echo \$PATH" alias gdb="gdb –q" # change prompt export PS1="$ " # marks # mrk <label> associate a directory with <label> # go <label> goto directory marked <label> # mrks display all marks mrk() { export MRK_$1="$PWD"; echo "$PWD"; } go() { eval cd \"$`echo "MRK_$1"`\"; echo "$PWD"; } mrks() { env | grep MRK_*; } if [ "$TMPDIR" = "" ] ; then export TMPDIR="$HOME/tmp" fi if [ ! -e $TMPDIR ] ; then mkdir $TMPDIR fi export PATH=".:$HOME/bin:$PATH" export EDITOR="vi" set -o vi The alias command substitutes text for a command entered at the console.