Unix Basics:
Common Commands
|
Note: You need to know all of this material for exams.
Conventions:
- Unless otherwise noted, every command
line should be completed with a RETURN (<-| Enter on keyboard).
- All Unix commands must be typed in lower case.
- Unix commands often have options, signaled
by a "-" followed by the name of the option.
- Unix commands below are in
bold green.
man # The on-line manual with a complete
list of options available for each command.
pwd # Print working directory: tells absolute
path from root to where you currently are in the file system.
cd # Change directory.
cd path/directory #
where path/directory is either an absolute or relative pathname to the desired
directory.
cd ~ # takes you to your home
directory.
cd .. # takes you
to the parent directory of the current directory.
mkdir # Make directory.
rmdir # Remove directory.
(Note: The directory must be empty to be removed.)
ls # List files in the current directory
in a column format, with "/" after directories, and "*" after executable files.
ls -F #
places "/" after directories, and "*" after executable files.
ls -l #
gives a long listing, showing permissions, size, date last changed, etc.
ls -a #
include hidden files (filenames starting with a ".")
ls -Fla #
do all three options above at once.
cp # Copies a file, with
name either absolute or relative.
cp source-file destination-file
rm # Removes a file,
but not a directory
mv # moves a file. This can
be a dangerous command if you make typo!
mv path/oldname path/newname
# can be used to rename a file if the path is the same.
more # Lists the contents of a file and
pauses at each screenful.
more filename
# A space gives the next
screen, or q to quit.
cat #
Concatenates files and prints to the screen.
cat filename #
prints the contents of filename to the screen.
cat -n filename #
adds line numbers.
cat filel file2 > file3 #
Append file2 to the end of filel and save as file3.
script # Records all interactions in a
window and saves in a file.
script filename #
To stop recording, use Ctrl-D.
history #
Lists previous commands by number.
!n #
executes the nth command when n is an integer, !4
!v #
exectues the last command that started with the character v
!! #
executes the last command
Ctrl-c # Emergency stop for a process.
Ctrl-d # End of file.
Sample Interactive Session (on Linux):
User input in green, comments in orange,
system responses in black. (Instead of using % to
show the Unix prompt, here I show the actual prompts.)
neal@linux:~> pwd # Current directory at home
/home/neal
neal@linux:~> ssh wagner@gateway19.cs.utsa.edu # secure shell to UTSA
wagner@gateway19.cs.utsa.edu's password:******** # type password
[wagner@gateway19 ~]$ mkdir example # create a new directory
[wagner@gateway19 ~]$ ls -F # list current directory contents
example/ # actually, there's a lot more junk here
[wagner@gateway19 ~]$ cd example # change to the new directory
[wagner@gateway19 ~/example]$ pwd # print the current path
/home/wagner/example
[wagner@gateway19 ~/example]$ vi goodmen.txt # use vi to create new file
iNow is the time for all good men # initial "i" is for "insert"
to come to the aid of their party.
[Esc] ZZ # type "Esc" key, followed by "ZZ"
[wagner@gateway19 ~/example]$ ls -l # long listing
total 1 # 1 file in this directory
-rw-r--r-- 1 wagner news 69 Dec 30 17:24 goodmen.txt # 69 chars in file
[wagner@gateway19 ~/example]$ cat -n goodmen.txt # print to terminal with line numbers
1 Now is the time for all good men
2 to come to the aid of their party.
3
[wagner@gateway19 ~/example]$ rm goodment.txt # remove the file. Oops, misspelled!
rm: cannot remove `goodment.txt': No such file or directory
[wagner@gateway19 ~/example]$ rm goodmen.txt # remove the file.
[wagner@gateway19 ~/example]$ ls -l # check listing
total 0
[wagner@gateway19 ~/example]$ cd .. # back to parent directory
[wagner@gateway19 ~]$ rmdir example # remove the directory
[wagner@gateway19 ~]$ logout # terminate secure shell connection
Connection to gateway19.cs.utsa.edu closed.
neal@linux:~> # Ahhh. Back at home!
Redirection of input and output:
At the command line, Unix allows redirection operators:
< ,
> , and
>>
< filename #
directs "filename" in as if it were the standard input from the terminal.
> filename #
directs standard output to "filename" rather than the terminal.
>> filename #
append standard output to "filename".
cat file1 file2 > combinedfile #
"combinedfile" is just "file1" followed by "file2"
Pipes on the command line: Two commands can be
connected with a "pipe" by putting
|
between them:
who | sort #
produces a sorted list of users of your computer
ls -l | more #
a long listing that lets you look one screen at a time
Copyright © 2011, Neal R. Wagner, Permission is granted
to access, download, share, and distribute, as long as this notice remains.