syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

You are here: Linux101 > linux101_cclud_file
Question:
In Linux101 how to CCLUD Linux Files?

To do Machine Learning on Linux I need to be skilled at CCLUD of files.

To create a single file I can use a carrot.

Actually it is a caret not a carrot.

Below is a demo of using caret to create a file. Also I show the ls command to list the outside of the file and cat to list the inside of the file:
dan@feb ~ $ 
dan@feb ~ $ 
dan@feb ~ $ cd /tmp/
dan@feb /tmp $ 
dan@feb /tmp $ echo To create a file, a caret is better than a stick. 
To create a file, a caret is better than a stick.
dan@feb /tmp $ 
dan@feb /tmp $ echo To create a file, a caret is better than a stick. > carrot.txt
dan@feb /tmp $ 
dan@feb /tmp $ ls -l carrot.txt 
-rw-rw-r-- 1 dan dan 50 Feb 11 07:37 carrot.txt
dan@feb /tmp $ 
dan@feb /tmp $ cat carrot.txt 
To create a file, a caret is better than a stick.
dan@feb /tmp $ 
dan@feb /tmp $ 
Another simple way to create a file is the Linux touch command.

I can use the touch command to create a file which has nothing in it.

Why would I create a completely empty file? I can use an empty file to signal that my software is busy doing a task.

When the task is done, I remove the file:
dan@feb /tmp $ 
dan@feb /tmp $ touch imbusy.txt
dan@feb /tmp $ echo I am done now
I am done now
dan@feb /tmp $ 
dan@feb /tmp $ ls -la imbusy.txt 
-rw-rw-r-- 1 dan dan 0 Feb 11 09:24 imbusy.txt
dan@feb /tmp $ 
dan@feb /tmp $ rm imbusy.txt 
dan@feb /tmp $ 
dan@feb /tmp $ 
So now we know how to create and list a file which are two verbs out of five.

I use the cp command to copy a file:
dan@feb /tmp $ 
dan@feb /tmp $ ls -l carrot.txt 
-rw-rw-r-- 1 dan dan 50 Feb 11 07:37 carrot.txt
dan@feb /tmp $ 
dan@feb /tmp $ cat carrot.txt 
To create a file, a caret is better than a stick.
dan@feb /tmp $ 
dan@feb /tmp $ 

The above commands list carrot.txt.
I know what it looks like now.

Now, I copy the carrot.txt file:

dan@feb /tmp $ 
dan@feb /tmp $ cp carrot.txt caret.txt
dan@feb /tmp $ 

That was easy.
Are they the same?

dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ ls -l carrot.txt caret.txt
-rw-rw-r-- 1 dan dan 50 Feb 11 07:54 caret.txt
-rw-rw-r-- 1 dan dan 50 Feb 11 07:37 carrot.txt
dan@feb /tmp $ 
dan@feb /tmp $ cat caret.txt 
To create a file, a caret is better than a stick.
dan@feb /tmp $ 
dan@feb /tmp $ 

Yes, they are the same.
Here is another way to copy a file.
I use the cat command and a ... caret!

dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ cat caret.txt > caret2.txt
dan@feb /tmp $ 


That was easy.
Are they the same?

dan@feb /tmp $ 
dan@feb /tmp $ ls -l carrot.txt caret.txt caret2.txt 
-rw-rw-r-- 1 dan dan 50 Feb 11 07:55 caret2.txt
-rw-rw-r-- 1 dan dan 50 Feb 11 07:54 caret.txt
-rw-rw-r-- 1 dan dan 50 Feb 11 07:37 carrot.txt
dan@feb /tmp $ 
dan@feb /tmp $ cat caret2.txt 
To create a file, a caret is better than a stick.
dan@feb /tmp $ 
dan@feb /tmp $ 

Now we know how to create, copy, and list a file.

I skip update for now and demo delete. Linux has no command named delete. Instead it has a command named rm which is short for remove:
dan@feb /tmp $ 
dan@feb /tmp $ cat caret2.txt 
To create a file, a caret is better than a stick.
dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ rm caret2.txt 
dan@feb /tmp $ 
dan@feb /tmp $ cat caret2.txt 
cat: caret2.txt: No such file or directory
dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ 
Of the five cclud operations I can do on a file, update is the most complex. I have many ways to update file. The demo below shows two ways to update a file with a caret:
dan@feb /tmp $ 
dan@feb /tmp $ echo update me > updateme.txt
dan@feb /tmp $ 
dan@feb /tmp $ ls -l updateme.txt 
-rw-rw-r-- 1 dan dan 10 Feb 11 09:30 updateme.txt
dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ date
Wed Feb 11 09:31:00 UTC 2015
dan@feb /tmp $ 
dan@feb /tmp $ 

Oh, by the way,
I can update the timestamp of a file using the touch command:

dan@feb /tmp $ 
dan@feb /tmp $ touch updateme.txt 
dan@feb /tmp $ 
dan@feb /tmp $ ls -l updateme.txt
-rw-rw-r-- 1 dan dan 10 Feb 11 09:31 updateme.txt
dan@feb /tmp $ 
dan@feb /tmp $ 

Sorry for the interruption.

So far I showed how to use a caret to create and copy a file.
I can also use caret to update a file.

If I use single-caret I overwrite the file which is sometimes a bad thing.
Be careful with single-caret.

dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ echo Dan was here > dan.txt
dan@feb /tmp $ 
dan@feb /tmp $ cat dan.txt
Dan was here
dan@feb /tmp $ 
dan@feb /tmp $ echo Evil Dan was here > dan.txt
dan@feb /tmp $ 
dan@feb /tmp $ cat dan.txt
Evil Dan was here
dan@feb /tmp $ 
dan@feb /tmp $ 

If I use a double-caret I add text to the end of the file
instead of 'clobbering' the file.

dan@feb /tmp $ 
dan@feb /tmp $ echo Dan was here >> dan.txt
dan@feb /tmp $ echo Dan was here >> dan.txt
dan@feb /tmp $ echo Dan was here >> dan.txt
dan@feb /tmp $ 
dan@feb /tmp $ cat dan.txt
Evil Dan was here
Dan was here
Dan was here
Dan was here
dan@feb /tmp $ 
dan@feb /tmp $ echo Evil Bob was here > dan.txt
dan@feb /tmp $ 
dan@feb /tmp $ cat dan.txt
Evil Bob was here
dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ 
So, a caret is useful for 'clobbering' a file and placing text at the end of a file.

If I want more control, an option is a command called sed.

I can use sed to do CCLUD operations on lines inside a file.

First I show how to use sed to create a line of text in the middle of a file:
dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ echo line 1 > sedme.txt
dan@feb /tmp $ echo line 2 >> sedme.txt
dan@feb /tmp $ echo line 3 >> sedme.txt
dan@feb /tmp $ echo line 4 >> sedme.txt
dan@feb /tmp $ 
dan@feb /tmp $ cat sedme.txt 
line 1
line 2
line 3
line 4
dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ 


I use sed to create some text at line 3.

dan@feb /tmp $ 
dan@feb /tmp $ sed -i '3i sed was here' sedme.txt 
dan@feb /tmp $ 
dan@feb /tmp $ cat sedme.txt 
line 1
line 2
sed was here
line 3
line 4
dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ 


Next, I show how to use sed to copy a line of text in the middle of a file:
dan@feb /tmp $ 
dan@feb /tmp $ sed -i '3p' sedme.txt 
dan@feb /tmp $ 
dan@feb /tmp $ cat sedme.txt
line 1
line 2
sed was here
sed was here
line 3
line 4
dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ 
Here, I use sed to list the second line of text in a file:
dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ cat sedme.txt 
line 1
line 2
sed was here
sed was here
line 3
line 4
dan@feb /tmp $ 
dan@feb /tmp $ 

Now that I know what the file looks like,
I want to use sed to list the second line in the file:

dan@feb /tmp $ 
dan@feb /tmp $ sed -n '2p' sedme.txt
line 2
dan@feb /tmp $ 
dan@feb /tmp $ 

So yes, it did list the second line.
I hope it did not change the file:

dan@feb /tmp $ 
dan@feb /tmp $ cat sedme.txt 
line 1
line 2
sed was here
sed was here
line 3
line 4
dan@feb /tmp $ 
dan@feb /tmp $ 

Yay! the file is unchanged.
Next, I use sed to update the third line in the file.
dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ cat sedme.txt 
line 1
line 2
sed was here
sed was here
line 3
line 4
dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ sed -i '3s/here/here, I am actually line 3 now/' sedme.txt 
dan@feb /tmp $ 
dan@feb /tmp $ cat sedme.txt 
line 1
line 2
sed was here, I am actually line 3 now
sed was here
line 3
line 4
dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ 
So far I have shown how to create, copy, list, and update lines in sedme.txt using sed.

Now I show how to delete the fifth line in sedme.txt
dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ cat sedme.txt 
line 1
line 2
sed was here, I am actually line 3 now
sed was here
line 3
line 4
dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ sed -i '5d' sedme.txt
dan@feb /tmp $ 
dan@feb /tmp $ cat sedme.txt
line 1
line 2
sed was here, I am actually line 3 now
sed was here
line 4
dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ sed -i '5d' sedme.txt
dan@feb /tmp $ 
dan@feb /tmp $ cat sedme.txt
line 1
line 2
sed was here, I am actually line 3 now
sed was here
dan@feb /tmp $ 
dan@feb /tmp $ 
dan@feb /tmp $ 


So those are some fine demos showing how to CCLUD lines inside a file using sed.

Your homework is to create a file with names of weekdays inside. Then use sed to do an operation called 'search and replace'. I want you to search for a string called Monday and change it to July.

CCLUD of Directories (Folders) is the next topic of Linux101:
linux101_cclud_folder


You are here: Linux101 > linux101_cclud_file
syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me