agenda basic unix commands (chapters 2 & 3) miscellaneous commands: whereis, which, whoami,...

24
Agenda Basic Unix Commands (Chapters 2 & 3) Miscellaneous Commands: whereis, which, whoami, finger, passwd, cal, date Working with Files: cat, more, less ls, cp, mv, rm, cd, mkdir, rmdir Filename Expansion (*, ?, [] )

Upload: elaine-roberts

Post on 14-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Agenda Basic Unix Commands (Chapters 2 & 3)

Miscellaneous Commands: whereis, which, whoami, finger, passwd,

cal, date Working with Files:

cat, more, less ls, cp, mv, rm, cd, mkdir, rmdir

Filename Expansion (*, ?, [] )

Textbook Coverage These slides are based on Chapter

3 of “A Practical Guide to Linux”

Read pages 41 to 57

Miscellaneous Commands

whereis which whoami finger passwd date cal

whereis / which whereis is a utility that lists all directory

paths that contain binary files for a command and directory paths for manuals for a command.

For Example: whereis mv

which is a utility that lists only one

directory path of the command that the shell will run when the user issues the command from the shell prompt.

For Example: which cp

whoami / finger whoami is a utility that displays the name

of the user currently logged onto the server.

finger is a utility that displays information regarding another user on the current server. You can edit the .project file to have messages / art displayed when the finger command is used for your account!

passwd A user can change their account

password by issuing passwd command at shell prompt. The user will be prompted for their current password and then be prompted for new password and confirmation.

Options:

-f changes the user information that is stored in the /etc/passwd file along with user's password

date The date command is used to display

the current date and time.

Example: date

Note: If you have superuser or "root" access, you can change the computer system's date and time.

cal The cal command without options

displays only the current month. The cal command with year as an argument displays the entire year's calendar. The cal command with arguments month followed by year displays the specific month for specific year.

Examples: cal 2002 (displays all months for year 2002)

cal 9 1947 (displays only month of September 1947)

Working with Files cat, more, less ls, cp, mv, rm, cd, mkdir, rmdir

cat The cat command is used to combine the

contents of multiple files (“catenate” means to join together). This command can be used to display contents of small files (that will fit on your terminal's display screen).

Example: cat hello.c (displays contents of c program

"hello.c")

more The more command displays a file, one

screenful at a time.

Example: more large_file

Movement keys (within more command): <spacebar> Move to next screen

b Move to previous screen<enter> Move to next line/car Search for pattern "car"q Exit to shell

less The less command is similar to more

command, but "less" command contains more movement and search commands than "more" command.

Example

less large_file

Note: You are not required to learn "less" command for this course, but if you are interested in using this command, you can refer to online manual.

ls The ls command is used to display

information regarding a file or directory.

Example: ls (Displays a compact listing (i.e. filenames only)

contained in current directory) Options:

-a short display of all files (incl. hidden files)-l detailed display of files (excl. hidden files)-al detailed display of all files-F displays / after directory, * after executable file

Making Directories

What are Directories?

Directories are “storage locations” that are used to store regular files in an efficient manner.

You will need to learn how to navigate throughout existing directories to access files.

You will also be required in the future to build directories to copy, move and remove directories!

Where do we want to build directory?

We want to build a directory called tmp that branches-off of your home directory

Verify which directory you are located (either look at directory from command prompt or issue the command pwd )

Type mkdir tmp at the Unix prompt, followed by ENTER

Always verify that directory has been created (e.g. use ls or ls -ld command, or change to that directory and issue pwd)

Removing Directories

Removing directories is reverse order of building directories

Issue command rmdir directory rmdir cannot remove directories

containing files or other subdirectories.

rmdir cannot remove directories that are anyone's current directory.

Need to step back to at least parent directory to remove an empty directory.

Removing Sub-trees To remove a sub-tree (a directory and all of

its contents including sub-directories) userm -r directory (or rm -R directory). The command rm –rf removes both subdirectories and files contained in a directory-path.

Caution! Back-up your files before removing. You can use the ftp application to transfer files to another server or your diskette. Refer to next set of notes to learn how to use command-line ftp.

Caution! rm -r can erase large numbers of files very quickly. Use with extreme care!

cp The cp command is used to copy one or

more files between directories, or to make backup copies of files within the same directory.

Example: cp /public/ipc144/hw.c /home/msaul/

(copies file "hw.c" from “/public/ipc144" directory to msaul's home directory)

Options: -R subdirectories & contents are copied -i prompts user to overwrite existing file

mv The mv command is used to move or

rename files. Examples:

mv hw.c work(moves file "hw.c" from current directory to subdirectory called "work”, assuming directory “work” exists) mv hw.c hello.c(renames file "hw.c" in current directory to "hello.c”) Options:-i prompts user to overwrite existing file

Ambiguous File References

(Wildcard Searches) You may need to access or process a file,

but forget what is the exact spelling (syntax) of the filename.

You may also want to run a command that accesses many files that share a similar characteristic (eg all files with extension "txt"). Files that match a pattern (as opposed to being referenced by exact filenames) are called ambiguous file references

The * character

This wildcard character is used to represent any characters. It is useful when searching for file names sharing common characteristics.

For Example: ls file* (list all files beginning with

"file") ls a .* (list all hidden files)

The ? character

The wildcard character ? will match any single character.

ls file? (list all files with only 1 character after "file")

ls file?? (list all files with only 2 characters after "file")

ls ??45? (list all files containing 5 characters that contain a 4 in 3rd position and a 5 in the 4th position)

Character Class [ ] Used to match a particular group or

"class" of a single character. Similar to using "?" but the user can determine what "?" is allowed or not allowed to represent.

Examples:

ls file[abc].txt displays filea.txt, fileb.txt, filec.txt ls file[1-9].txt displays a range file1.txt to file9.txt cat file[!abd].doc displays all files matching

file?.doc other than filea.doc, fileb.doc or filed.doc

Globbing Ambiguous filenames are "expanded"

to multiple arguments by the shell (called "globbing").

This can cause strange effects with commands that do not expect a file argument or expect only one file argument.

e.g., echo *