Lab # 2 File Creation and Listing Commands: GNU Nano
Lab # 2 File Creation and Listing Commands: GNU Nano
When you create a new file, there are some rules governing the naming of the file. These rules
are related to the length of the file name and the characters allowed in naming a file.
Generally a file name in LINUX can be as long as 256 characters. The rules that apply to the file
names are as follows:
1. A file name can be a combination of letters, numbers and special characters.
2. All letters, both upper (A-Z) and lower case (a-z) can be used.
3. Numbers from 0 – 9 can be used.
4. Special characters like plus (+), minus (-), underscore (_), or dot (.) can be used.
5. LINUX is case sensitive and uppercase and lowercase letters are treated separately. So
myfile, Myfile, MyFile, and myFilE are different names.
The most common operations on files are creating new files, deleting unnecessary files, and
displaying the contents of a file.
CAT command:
A file is a named area on the disk(s) where you can store information. The cat command is a
basic command used for creating new files containing text. Cat command allows us to create
single or multiple files, view contain of file, concatenate files and redirect output in terminal or
files.
General Syntax
cat [OPTION] [FILE]...
<CTRL+d> or <CTRL+Z>
Note: Press the Control and d or z keys simultaneously to end the text entry process and save the
file.
Patterns for CAT command:
View Contents of Multiple Files in terminal:
Syntax: cat filename1 filename2
Display Line Numbers in File: With -n option you could see the line numbers of a file in the
output terminal.
Syntax: cat –n filename
Display Multiple Files at Once: Three files test, test1 and test2 and able to view the contents of
those file as shown above. We need to separate each file with ; (semi colon).
Syntax: cat filename1; cat filename2; cat filename3
Use Standard Output with Redirection Operator: We can redirect standard output of a file
into a new file else existing file with ‘>‘ (greater than) symbol. Careful, existing contents
of test1 will be overwritten by contents of test file.
Syntax: cat filename1 > filename2 (copies the content of filename1 and remove the previous
content of filename2)
Appending Standard Output with Redirection Operator: Appends in existing file with ‘>>‘
(double greater than) symbol. Here, contents of test file will be appended at the end of test1 file.
Syntax: cat filename1 >> filename2 (copies the content of filename1, append with filename2
content)
Redirecting Multiple Files Contain in a Single File: Create a file called TestFile and all output
will be redirected in a newly created file.
Syntax: cat filename1 filename2 filename3 > TestFile
Exercise:
1. Create a filename called ‘Peoples’ and fill five names of a person in that file and create
another filename called ‘Animals’ and fill five names of an animal in that file. After create two
files then perform all the patterns of CAT command.
3. Create three file with ‘.txt’ extension and fill some data on each file and redirect all three file
data in single file.