Basic Files Processing: Professor Dumont Csc119 - Introduction To Unix/Linux

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

BASIC FILES PROCESSING

Professor DuMont CSC119 - Introduction to UNIX/Linux

Absolute Vs Relative files path

Absolute path is the path to a file on the file system starting from the root (/) of the file system

E.g. /home/jdumo002/files/CSC119/lab2/lab2answers.txt

Relative path is the path to the file from your current working directory

Assuming my Current Working Directory was my home directory

E.g. files/CSC119/lab2/lab2answers.txt

Assuming my Current Working Directory was /home/jdumo002/files/CSC121

E.g. ../CSC119/lab2/lab2answers.txt

.. Refers to Parent Directory . Refers to Current Working Directory

Displaying Text Files

Text files can be opened with several programs as discussed before, but text files can also be sent directly to standard output so the can be displayed in the terminal cat is a program that has only standard input connected to standard output, as such it can dump a text file to standard output as an entire file nl is a program that displays a text file on standard output but add line numbers to each line for reference pr is a program that displays a text file on standard output but paginates the file and places file header at the beginning of each page. This is useful for printing the file but redirecting to the printer

File Head & Tail

When working with large text files it can be useful to adjust the output display
more/less programs that paginate the text file into pages and allow the user to more forward when ready head program displays the head of the file, or the first 10 lines of the file. Can be passed an option to adjust the number of lines displayed

Commonly used to take a quick look at a file to see if it is the file that is needed

tail program displays the tail of the file, or the last 10 lines of the file. Can be passed an option to adjust the number of lines displayed

Commonly used to keep checking files that keep getting appended to, such as log files.

Copying, Moving & Renaming Files

Files can be copied used the cp command


cp [SOURCE] [DESTINATION] e.g. cp lab1/text.txt lab3/text.txt

Files can be moved from one location to another with mv command


mv [SOURCE] [DESTINATION] e.g. mv lab1/text.txt lab3/text.txt

To rename a file the mv command is used

mv text.txt file.txt rm file.txt

Files can be removed using the rm command

File Sizes

File sizes varies from file to file based on how much data is stored within the file To determine the size of the file the ls command can be used with the l option

This will display the file size in bytes as the 4th column By using the h option along with the l option (-lh) we can see files sizes in human readable formats which will be shown in Kilobytes, megabytes & gigabytes

File size can also be checked by the wc command which will display the number of lines, number of words & number of characters

A single character is a byte long, therefore the number of characters is equal to the number of bytes in a text file

Combining Files

Often multiple files may want to be combined together to be in one file, this can be done by using the cat command By using the cat command and providing multiple arguments of files, the files would all be displayed on the standard output one after the other Using this command and redirecting standard output to a new file we would have a single file contains the contents of all the files cat memo1.txt memo2.txt memo3.txt > memos.txt

Comparing Files

Files can be compared by using the diff command

e.g. diff [file1] [file2]

Diff will display the lines in the files that do not match

It will show the line number followed by showing if the lines needs to be added, deleted or changed It will then show the line from each file and you can see how they differ It is possible to have a script out of diff that can be used in the ed text editor program to make the 2 files match

Removing Repeated Lines

If a file has a multiple lines that are duplicated in a row they can be easily removed using the uniq command The uniq command will only consider a line to be a duplicate if it matches exactly and is consecutive If a line is duplicated multiple times consecutively then it will remove all but 1 copy of the line By using the c option uniq will show how many duplicates of each line are being removed from the display

This can be useful for log files where you want a count of how many times an error occurred b counting number of duplicate lines

Printing

Printing on Linux/Unix can often be done by using the lp command The lp command takes either a file name or standard input and sends it to the designated printer When a file or standard output is requested to be printed, the Linux system buffers the data to be printed into a spool file which is stored on the system hard drive The printer demon then sends spool files to the printer as it becomes available When a request to print is sent, a print job is created and you will receive a job number which can be used to check on the status of the request

e.g. lp file.txt e.g pr file.txt|lp

Questions?

Questions?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy