Linux : command output to a file


In this series of mini posts I will teach you a Linux lesson every day, it can be command , tricks , how to. The post will cover for beginner to advanced level. Welcome to Linux terminal.

Follow us and start learning Linux today

how to output a command result to a file?

In Linux we can output command result directly to a file using output operator(>). Suppose we want to list directory and redirect to to it a file called mydir, here is the trick

ls > mydir
cat mydir

You may love to learn following Linux commands and tutorials

How to place custom route links in Reactjs component


Suppose you have a React application which uses link to navigate another route/page. How do we place the link ? . Let’s look at the post example.

const PostTitles= props=>(
  <div>
    <div>Date: {props.post.createdAt} </div>    
    <a href=`/post/${props.post._id}` >{props.post.title} </a>
  </div>
)

In the above code is a part of blogging web app, which consists of posts,uers,comments and tags components. In PostTitles component which shows title link of posts we used back tick ( `) to add variable. The back tick inside the render part may not work perfectly as in JavaScript.

Solution

So to make it work we have to add pair of extra curly braces ({}) as follows

const PostTitles= props=>(
  <div>
    <div>Date: {props.post.createdAt} </div>    
    <a href={`/post/${props.post._id}`} >{props.post.title} </a>
  </div>
)

Following React posts may help you

Linux cat


In this series of mini posts I will teach you a Linux lesson every day, it can be command , tricks , how to. The post will cover for beginner to advanced level. Welcome to Linux terminal.

Follow us and start learning Linux today

cat

cat is used to create ,view files in Linux. It is useful when dealing with scripts and output some command result. It can used to view multiple files as follows

cat file1 file2

To create file we can use the cat with an input file name as follows

cat> file name
Ctrl+Z

You may love to learn following Linux commands and tutorials

For copying content of a file into another we can use

cat myfile>file4
Ctrl+Z

How to filter MongoDB object list in express-node app


MongoDB objects are object Id of another document stored in the mongo database. We can place multiple objects using list of object fields. A sample mongo object list as follows.

 "tags": [
        {
            "posts": [
                "5fef1f5087a69346300003a7"
            ],
            "_id": "5fef1f5187a69346300003a8",
            "tagName": "Test",
            "createdAt": "2021-01-01T13:10:41.799Z",
            "updatedAt": "2021-01-01T13:10:41.855Z",
            "__v": 1
        },
        {
            "posts": [
                "5fef1f5087a69346300003a7"
            ],
            "_id": "5fef1f5187a69346300003a9",
            "tagName": "VM",
            "createdAt": "2021-01-01T13:10:41.804Z",
            "updatedAt": "2021-01-01T13:10:41.896Z",
            "__v": 1
        },
        {
            "posts": [
                "5fef1f5087a69346300003a7",
                "5fef1f7287a69346300003af"
            ],
            "_id": "5fef1f5187a69346300003aa",
            "tagName": "JS",
            "createdAt": "2021-01-01T13:10:41.810Z",
            "updatedAt": "2021-01-01T13:11:15.561Z",
            "__v": 2
        }
    ],

We want to lookup at the above list of tags and make sure no more duplicate tags shown up in the future. A loop will do the job, using a filter we can do the same.

if (post.tags.filter(xtag => xtag == 'ABCD') == false) {
console.log(`No Tag found`);
} else {
console.log(`Tag already added`);
}

The filter uses a quick arrow function to filter result and return false when it failed to match the tag.

Following Mongo posts may help you

Linux : arbitrary calculator language


In this series of mini posts I will teach you a Linux lesson every day, it can be command , tricks , how to. The post will cover for beginner to advanced level. Welcome to Linux terminal.

Follow us and start learning Linux today

bc

bc is a command line arithmetic calculator. It is a an arbitrary calculator language which means that it is an interactive calculator programming language. We can use expressions,variable,print,decision make statement etc

a=13
print a
a<1
0
a>12
1

A comparison with >,< return 0 if it fails or 1 if it it true, I think that make sense.

We can also use the bc with output statement as follows

echo '100*9'|bc

You may love to learn following Linux commands and tutorials

How to fix mongo object Type error in Reactjs


MongoDB objects are object Id of another document stored in the mongo database. Following is our sample mongo document we want to render in our React front end.

{    
    "_id": "5fef1f5087a69346300003a7",
    "title": "Title post",
    "content": "It is a good practice to keep styles for different pages  in separate style sheets. Larger project this becomes more applicable. What you do when need to combine them in another ?",
    "user": {    
        "_id": "5fe4ab1e69b8d525c44ec293",
        "name": "admin",
        "createdAt": "2020-12-24T14:52:14.364Z",
        "updatedAt": "2021-01-01T13:12:59.084Z",
        "__v": 106
    },
    "createdAt": "2021-01-01T13:10:40.468Z",
    "updatedAt": "2021-01-01T13:10:41.907Z",
    "__v": 3
}

In the above document we want to render the user object and the following code will cause an type error

For simplicity I skip the rest of the component code.

render() {
    return (
      <div>
        <div className="card m-2 p-2">
          <h3>{this.state.post.title}</h3>
          <div>{this.state.post.content} </div>
          <div> Posted By: {(this.state.post.user).name}            
     </div>
        </div>
      </div>
    );

TypeError: Cannot read property ‘name’ of undefined

Error Fix

React recognize sub document as filed, not as an object.To fix the error we have to convert the user to Object as follows.

render() {
    return (
      <div>
        <div className="card m-2 p-2">
          <h3>{this.state.post.title}</h3>
          <div>{this.state.post.content} </div>
          <div>  Posted By: {Object(this.state.post.user).name}            
     </div>
        </div>
      </div>
    );

Following React posts may help you

How to create document using mongoose in express-node app


Mongoose is MongoDB driver module which provide REST features to build faster MongoDB apps with Nodejs.

MongoDB

MongoDB is a NoSQL database which store data as JSON document and collections. In JavaScript applications we can utilize the mongoose driver to perform Mongo operations. First up all we need to install mongoose to your project and then import the module.

npm install mongoose

Saving the document

mongoose uses the modal class to perform REST operations. If you are not familiar have a look at the mongoose modal post given below

Tag.create({
tagName: tagNa}, function(error, stag) {
if (error) return handleError(error)
 console.log(new tag saved : ${stag.tagName});
})

The Tag is the Modal and the create method is used to save the document to database and it also use a callback function to return the result.

We can also use an asynchronous method find documents in Mongoose as follows

const tag=new Tag({tagName:'SQL'})
await tag.save()

That’s it. Following mongoose posts deserve your attention

How to create Modal for CURD using Mongoose in express-node app


Mongoose is MongoDB driver module which provide REST features to build faster MongoDB apps with Nodejs.

Modal

Mongoose allow us to perform CURD operation using REST full Modals. A Modal is basically is an object/class which define all fields with validation and also connected to your MongoDB.

To perform operation ,create variables of the modal and call the save/ create method. Following is a sample Modal class

const mongoose = require('mongoose');
 const UserSchema = new mongoose.Schema({
     name :{
         type:String,
         required: '{PATH} is required!'
     },
     bio: {
         type:String
     },
     email:{
         type:String
     },
     website:{
         type:String
     },
     password:{type:String,required: '{PATH} is required!',default:"123"},
 posts : [     {type: mongoose.Schema.Types.ObjectId,ref:'Post'} ]
 },{
     timestamps: true
 })
 module.exports = mongoose.model('User',UserSchema);

In the modal class you can store another document using ObjectID .Here is an example for the create document using the modal class

const user = await User.create({
             name,
             email,
             bio,
             website
         })

That’s it. Following mongoose posts deserve your attention

How to find document byId using mongoose in express-node app


Mongoose is MongoDB driver module which provide REST features to build faster MongoDB apps with Nodejs.

Mongo Object Id

MongoDB object ID is an automatically generated multipurpose number. In our case we can use it to show up the document. In fact the Object ID consist of date and a unique random number.

Find records using MongoDB ObjectId

  Post.findById(pid,function (error, post) {
       if (error) return handleError(error)
          if (post) {
               console.log("Full post :" + post);
          }
        });  

Using the findById method of mongoose model class allows us to find the document using MongoDB object Id and return result to the call back function in JavaScript style.

We can also use an asynchronous method find documents in Mongoose as follows

const post=await Post.findById(id)

and the resulted document will be

That’s it. Following mongoose posts deserve your attention

Linux ASCII character art with banner


In this series of mini posts I will teach you a Linux lesson every day, it can be command , tricks , how to. The post will cover for beginner to advanced level. Welcome to Linux terminal.

Follow us and start learning Linux today

banner

banner is a command for fun, it print first 10 character in a string with big ASCII character. Yo can install the banner using the following command

sudo get install sysvbanner
banner LINUX

You may love to learn these Linux commands and tutorials