Presentation on theme: "Working with Files How to create, view, copy, rename and print files."— Presentation transcript:
Working with Files How to create, view, copy, rename and print files.
clear clear - clear the terminal screen When working on the terminal use clear to get a fresh screen so you can work from the top. Example: grid: clear
File Extensions No set rules for assigning file extensions. However, some extensions allow their associated program to access them. – Ex..cc,.cpp are C++ programming extensions. – Ex..out,.exe are executable extensions. Unix allows files with no extensions. They are useful as a way of identifying the type of file. Ex..txt,.cpp,.dat,.bac
Create a file - touch touch - change file timestamps ‘create’ Can create a file basic empty file using the ‘touch’ command. touch [file to be created] Example: grid: touch file1.txt This creates an empty file named file1.txt. If file exists it will NOT overwrite it.
Create a file - cat cat - concatenate and display file(s). Can be used to create or read a file. Also can be used to append or copy a file to an existing file. Or can be used to re-direct output from a program to a file. Viewing the contents of a file Usage: cat [file to view] – Example: grid: cat file1.txt
more A second command to view file contents is more Usage: more [file to view] – Example: grid: more note cat will display the entire file. more allows you to scroll through the file a few of lines at a time. Use spacebar to page forward Use enter key to move forward line by line
cat and the ‘>’ operator (Writing to a file) Use with the redirection operator ‘>’ to create a file with text inside. If file does not exist, it will create it. If one already exists, it will overwrite it! After executing command, prompt will go to next line where you can begin typing your note. After note, hit return, then control-c to close the file. – Example: grid: cat > note – Just a quick note or reminder –
cat and the ‘>>’ operator (Appending two files) You can add the contents of one file onto the back of another file using the appending operator ‘>>’ with the cat command. Usage: cat [source file] >> [target File] – Example: grid: cat note >> note2 Executing this command would result in the contents of note being appended to note2. Alternatively: – Example: grid: cat >> note2 – Then type text to be appended
cat (Copying contents of file) Works with the redirection operator Redirecting the contents of one file into another file. Assumes the source file exists. Good way of making a backup of a file. Usage: cat [source file] > [target file] – Ex. Cat temp1 > temp1.backup
cp copy - copy files and directories Copies the contents of one file to another. – cp [file to copy] [new file name] – If file to copy is not local, then you must supply the pathname. Example: grid: cp /home/CS/cs140/Hwk/file.txt. The period at the end is necessary if you do not specify a new filename for the copied file. Otherwise, provide a new file name – Flags: The two most common flags for copying entire directories (and subdirectories) -r - copy recursively, non-directories as files -R - copy directories recursively
cp EXAMPLES……. – Copying a file using an absolute path cp [absolute path of file TO BE COPIED] grid: cp /home/CS/cs140/001/test.txt mytest.txt – Copying a file using a relative path cp [relative path of file TO BE COPIED] grid: cp../Test/final.txt exams/myfinal.txt The period is necessary if you do not supply a filename or path for the copied file (refers to the working directory). grid: cp../Test/final.txt.
mv mv - move (or rename) files Takes existing filename and renames it to a new filename. – mv [old file name] [new file name] – Example: grid: mv test1.txt final.txt Works with directories as well – Mv [old directory name] [new directory name] – Example: grid: mv TEST TEST2 Moves files – Mv [file name] [new path and name] – Example: grid: mv lab1.txt labs/lab1.txt
rm rm - remove files or directories Deletes the specified file or files. Remember, once a file/directory is removed, it is not recoverable! – rm [file name] – Example: grid: rm test.txt Note: this does not generally work with directories. Use rmdir for directories.
lpr (print command) lpr - submit print requests (miller man listing) This prints the file(s) you specify to the printer you specify. – lpr [flags] [file name to print] We will need to use the –P flag when printing. – -P: specifies the destination of the printer to print to. A destination will be a room number which has a printer that is on the particular system. Example: grid: lpr –Pe270 File.txt
lpq lpq - display the content of a print queue If you made a print request (lpr) and nothing is happening, check the print queue to make sure the printer received it. lpq –P[printer destination] – Example: grid: lpq –Pe270
lprm lprm - remove print requests from the print queue If you made several print requests and wish to cancel them use lprm with your login name to delete the print jobs from the queue lprm –P[printer destination] [user id] – Example: grid: lprm –Pe270 car.txt
Misc. ! [Letter] – repeats the last command used that began with that letter. – Example: miller: clear miller: !c * is a wildcard character – Example: miller: ls web* – Example: miller: rm * THIS COMMAND WILL REMOVE ALL FILES IN THE WORKING DIRECTORY. – BE CAREFUL! | is called the pipe. It will connect the output of one command to the input of another. – Example: miller: ls –l /users/st/ugrad | more